CSS Grid Layout: 5 Practical Projects Copyright © 2018 SitePoint Pty. Ltd. Ebook ISBN: 978-1-925836-17-2 Project editor: Craig Buckler Cover Design: Alex Walker Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: [email protected] About SitePoint SitePoint specializes in publishing fun, practical, and easy-to- understand content for web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums. You’ll find a stack of information on JavaScript, PHP, design, and more. Preface CSS has grown from a language for formatting documents into a robust language for designing web applications. Its syntax is easy to learn, making CSS a great entry point for those new to programming. Indeed, it’s often the second language that developers learn, right behind HTML. One of CSS's new features is the Grid Layout Module, which enables complex layout designs that previously would have been very difficult to achieve. In this book, we'll examine five layout projects that use grid layout. Conventions Used CODE SAMPLES Code in this book is displayed using a fixed-width font, like so: <h1>A Perfect Summer's Day</h1> <p>It was a lovely day for a walk in the park. The birds were singing and the kids were all back at school.</p> Where existing code is required for context, rather than repeat all of it, ⋮ will be displayed: function animate() { ⋮ new_variable = "Hello"; } Some lines of code should be entered on one line, but we’ve had to wrap them because of page constraints. An ➥ indicates a line break that exists for formatting purposes only, and should be ignored: URL.open("http://www.sitepoint.com/responsive-web- ➥design-real-user-testing/?responsive1"); You’ll notice that we’ve used certain layout styles throughout this book to signify different types of information. Look out for the following items. TIPS, NOTES, AND WARNINGS Hey, You! Tips provide helpful little pointers. Ahem, Excuse Me ... Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information. Make Sure You Always ... ... pay attention to these important points. Watch Out! Warnings highlight any gotchas that are likely to trip you up along the way. Chapter 1: Redesigning a Site to Use CSS Grid Layout BY ILYA BODROV CSS Grid is a new hot trend in web development these days. Forget about table layouts and floats: a new way to design websites is already here! This technology introduces two- dimensional grids which define multiple areas of layout with a handful of CSS rules. Grid can make third-party frameworks such as 960gs or Bootstrap grid redundant, as you may easily do everything yourself! This feature is supported by all major browsers, though Internet Explorer implements an older version of the specification. In this article we are going to see CSS Grid in action by creating a responsive multi-column website layout. What We Are Going to Build So, we were asked to create a typical website layout with a header, main content area, sidebar to the right, a list of sponsors, and a footer: Another developer has already tried to solve this task and came up with a solution that involves floats, display: table, and some clearfix hacks. We are going to refer to this existing layout as "initial". Live Code See the Pen Multi-Column Layout With Floats. Until recently, floats were considered to be the best option to create such layouts. Prior to that, we had to utilize HTML tables but they had a number of downsides. Specifically, such table layout is very rigid, it requires lots of tags (table, tr, td, th etc), and semantically these tags are used to present table data, not to design layouts. But CSS continues to evolve, and now we have CSS Grid. Conceptually it is similar to an old table layout but can use semantic HTML elements with a more flexible layout. Planning The Grid First things first: we need to define a basic HTML structure for our document. Before that, let's briefly talk about how the initial example works. It has the following main blocks: .container is the global wrapper that has small margins to the left and to the right. .main-header is the header that contains the .logo (occupies 20% of space, floats to the left) and the .main-menu (occupies 79% of space, floats to the right). The header is also assigned with a hacky fix to clear the floats. .content-area-wrapper wraps the main .content-area (occupies 66.6% of space minus 1rem reserved for margin, floats to the left) and the .sidebar (occupies 33.3% of the space, floats to the right). The wrapper itself is also assigned with a clearfix. .sponsors-wrapper contains the logos of the sponsors. Inside, there is a .sponsors section with the display property set to table. Each sponsor, in turn, is displayed as a table cell. .footer is our footer and spans to 100% of space. Our new layout will be very similar to the initial one, but with one exception: we won't add the .main-header and .content-area-wrapper wrappers because the clearfixes won't be required anymore. Here is the new version of the HTML: <div class="container"> <header class="logo"> <h1><a href="#">DemoSite</a></h1> </header> <nav class="main-menu"> <ul> <li class="main-menu__item"><a href="#">Our clients</a></li> <li class="main-menu__item"><a href="#">Products</a></li> <li class="main-menu__item"><a href="#">Contact</a></li> </ul> </nav> <main class="content-area"> <h2>Welcome!</h2> <p> Content </p> </main> <aside class="sidebar">