 Hi, I'm Yuna, a developer advocate on the Chrome team focusing on CSS and Web UX. Thank you for joining me today. I am super excited to get started and talk about some magical lines of CSS that do some serious heavy lifting and let you build robust modern layouts. Before we dive into that, there are a couple of key terms that will help you in your styling journeys and are super good to know about as we walk through these techniques. Most of the items I'm mentioning today are used in conjunction with Grid or Flexbox layout. And you can denote those as Display Grid or Display Flex on the parent element. The first key terms are FR and Auto. These are used with Grid layouts to denote fractional units of space, that's FR, or automatic space, that's Auto, based on the minimum content size of the items within that element. For Grid layout, we also have the MinMax function, which lets us set a minimum and maximum value with our layout bounds to enable responsive design without media queries. We also have separate Min, Max, and Clamp functions available in some browsers that bring logic to CSS. The browser determines which value to choose based on the function provided for Min and Max. And for Clamp, we'd set both a Min and Max with a relative value in between. We'll definitely be covering these within the video with a demo. And within these layouts, we can also use AutoFit or AutoFill to automatically place child elements into a parent grid. This is another tool for dynamic responsive layouts without media queries. We're going to be going over all of those terms and more with demos galore. I'm a really big nerd. I don't know why I made that rhyme and put that in this video, but I did so here. So let's just dive into the demos and one line layouts. I made a handy little site called one line layouts at onelinelayouts.glitch.me so that you can follow along or play on your own and have a reference for the power that CSS can bring to your layouts. For our first single line layout, let's solve the biggest mystery in all of the CSS land, centering things. I want you to know that it's easier than you think with place item center. I call this the definitely centered layout. What we need to do is first specify the layout method, which is display grid here, and then we are going to write place item center. This is that one magical line of code and I have these highlighted under the titles here. So what happens here is no matter what you put in here, it is going to stay centered to that parent element. So if we look at the HTML, we have this parent and it's just getting background blue and then this child with a coral background and it's content editable. So we can actually just type in here, we can have vertical content, hello world. And even as I'm typing, this child element is staying centered within the parent box. So I think that this is a really cool technique, place item center will solve all of your centered dilemmas. Next we have the deconstructed pancake. This is something that we see all the time on marketing sites, you see this row of three items and usually on mobile we'll want that to be stacked, which is why I called this the pancake stack but deconstructed because as you increase the size of the viewport, those items will start spanning into the same line. So it starts when they're all on top of each other and then they start to deconstruct as you increase the size of the viewport. So the way that we're going to be doing this one is using the shorthand for flex box, flex 01 base width is what we will be using for this look that you're seeing right now without the stretching. But if we did want it to stretch, we would set that to flex 11 and then the flex basis. And the reason why we do this is because the flex shorthand stands for flex grow, flex shrink and then flex basis. So again, here we have flex 01, 150 pixels. That is the basis here. So as we increase the size, it is not gonna be growing much. Also, when we decrease it, it's gonna be staying within that 150 pixel size. When we do change that to one, I'm just gonna delete the sign of code here. Now we see that this is going to be stretching. So here you can see that it stretches to fill the space even as it wraps. And as I increase it to be even larger of a viewport size, these items are gonna be filling that space. So this is the deconstructed pancake, a really common technique that we see in marketing sites. This is usually gonna be like an image with some text about the product. And you can write it by using the flex shorthand. I like to call this one sidebar says, and it takes advantage of that minmax function for grid layouts. So that is the line here. We have grid template columns, minmax, and then min and max value. That's pretty straightforward with minmax. But what this is doing is essentially as we are increasing the size of the viewport, it's going to be taking that 25% size, that max size. And as we decrease it, it hits this point where it's 150 pixels and where 25% is smaller than 150 pixels. So it's going to clamp it at that minimum size. So it's going to be increasing when it can take up that viewport space. But if we have content in here that we don't want squeezed or we want it to stop being 25% and at a minimum be 150 pixels or whatever value we set here, then that is exactly what minmax is doing for us. And in the grid template columns here, we are writing grid template columns, minmax, that base value, and then the relative value, so 25%. And that second element is getting 1fr. So if we look at the HTML, we have these two elements, this yellow sidebar section and then this purple content section. And they are taking up the units of space that we are specifying. In this specific case, we could actually even set this to auto and it would be looking the same because we are only setting the size to that first element. We're going to go over auto versus 1fr in the next example. But I think that this is pretty neat, a great way to set a minimum size but then let your element stretch on larger viewports to fit those layouts a little bit better based on how your user is seeing your website. Next we have the pancake stack. Unlike the deconstructed pancake, this one does not shift when the screen changes sizes. This is a very common layout that we see for both websites and applications across mobile and desktop functions. So it looks like this as we're increasing and decreasing the size, this content is not changing. And what we are doing to create this is we are writing the grid template rows to be auto, 1fr and auto. So what we are doing is essentially telling the first and last row to take up the space that the internal elements allow. So if this header was two lines, then it's going to increasingly take up more space within this vertical layout. With this auto section, I could have more content here but it's not going to increase the size because what we're doing is setting the auto placed row first to take up that specific size based on the content within it. And then the remaining space is the remaining fractional units. So as we increase this also vertically, you could see that the first and last row are not growing. They're still only going to be taking the size that they need, but if I decrease the horizontal space as footer content moves on to the next line, it is going to be taking space up within that layout. So with grid template rows, auto, 1fr, auto, you can create the pancake stack. You could even have an application toolbar down there. Again, this is the one that we see really commonly and grid template rows, it's a great one to know. Another very common layout is the holy grail layout. Does this look familiar? I think we've all seen a website or two that looks exactly like this at some point on our web journey. We've got your header, your footer, your left sidebar over here, your main content and then a right sidebar. And we can write all of this in one line of code using grid template. So grid template allows us to write the grid template rows and grid template columns at the same time. So that's pretty neat. It's also super responsive. Again, you see this auto content taking up what we are specifying here internally and then this main content taking up the 1fr. And so what we are specifying here is grid template with auto, 1fr for this middle space and then auto for the footer. And then we've got our columns of auto, 1fr and auto. So you get the whole holy grail layout with grid template and auto, 1fr, auto, slash, auto, 1fr, auto. This slash is what denotes our rows versus columns when we are writing the grid templates. So I think that this one is great to know. Grid template is something that I use all the time. This is not like exactly one line. I mean, all of these, you know, you have a little bit more to add. But with this, when you're already in grid template, you can then specify grid columns and grid rows for each of these elements. So here I'm specifying what grid column I'm placing the header in. I'm going all the way across all three, so one slash four to get those grid tracks from the first track to the last track. And then the left side, we have it going inside the first. So from one to two, this main section going from two to three. So that's gonna be taking up the middle section. And then the right side is from three to four. So that's going all the way to the end of this grid that we've created. And then for that footer, it's going all the way across just like the header. So you do specify where items are placed when it is a little bit more of a complex UI. And in here, if we look at the HTML, I have this parent, then I have header, then I have left sidebar, then I have the main section, right section, and then the footer. So you can write all of your layouts in one line using grid template property. Next we have another classic, the 12 by or 12 span grid. You can quickly write grids in CSS using the repeat grid function. And here we're setting a repeat of 12 columns. So that looks like this grid template columns, repeat the number that we wanna repeat and then 1FR. So this would be the same as writing grid template columns, 1FR, 1FR, 1FR 12 times. But because I don't want to do that, I'm just gonna write repeat 12 and then 1FR. And then we have a 12 by grid. So now we can place our items within this grid, however we want, if we want it to span all the way across the 12 columns, we would use this span 12 element. I just gave it a span 12 class and have it going from one all the way to 13. So it's taking up the full space of the 12 columns ending at the grid track of 13, which would be the end of that 12th column line. For the span six, this is going from one to seven. For span four, this is going from four to nine. It can go anywhere. I could start this at the first line and then make it go all the way to nine. I could have this go to five and have it still span four. But the cool thing is you can just place this wherever you want inside of your UI. So if I wanted to have this start from six and go to 10, I could do that. And then we have this span two and this is just going from three to five. Again, that could be placed anywhere. And then a peep at the HTML, we just have this parent elements. And then the inside of here, we're just giving it classes based on those span elements that we were just adjusting within that grid template. So the repeat function is very, very useful when you don't want to keep typing out one of our auto multiple times. It just lets you quickly write information. We're going to expand on the repeat function for number seven. And this technique is super cool, super useful. If you take away anything from this video, I think this would be a great one to keep in your repertoire. I like to call this the RAM technique, which stands for repeat auto and min max. And the line of code here looks like this. You have the grid template columns, repeat auto fit or auto fill, min max the base value, and then one FR for a fractional unit. And here we have that base value as 150. I'll show you exactly what's going on. So as we increase the size here, these elements are going to fit to take up the space, these four boxes. As I decrease it, they're going to hit this 150 base value and then they're going to wrap onto the next line. But here we have them auto fitting. So they're going to be spanning to take up as much space as they can. There's some really cool algorithms at play here. Now, if we change this to auto fill, this will look a little bit different. So let me just update that to auto fill. Here now, as I increase the size, it's not going to be spanning and stretching to take up the remainder of the space. It's actually going to be using that 150 pixels as a baseline and stay within that. At smaller sizes, there's no difference here, but you really see the difference at larger sizes as you have additional space. And I use this technique on the page that you're looking at here itself, this auto fill, so that these two segments would stretch and shrink but not exceed a specific space that I wanted them to. And then I have them centered. So you can center this within the parent as it spreads to a larger size. That's always an option. And a really great technique to know. Remember, R-A-M, RAM, repeat, auto, fill or fit, and then min max. And you can get these responsive boxes. You can use these for images. You can use them for a lot of things. It's something that we see all the time for cards. And this is a great use of all of these fancy new grid capabilities in CSS. For our next layout, we're heading back to Flexbox Land. I wanted to include this one because I just find it so useful and I use this all the time. I call it the lineup. Why? I don't know. I'm tired. If you have a better name, please let me know. Leave a comment. But the main thing that I wanted to demonstrate here is justify content for placement of items. And here, specifically, I wanted to highlight justify content space between to place items at their edges. And so in this example, we have these three cards and you can see that as I'm stretching or shrinking the size of this viewport element, they are maintaining the same height as each other. And in fact, we are having them fit to the top and bottom and this interior content. So this, this description, like I can keep typing here. This is then going to be centered within the remaining space. And so the reason that this is happening is because for these cards, we're giving them this flex direction of column, this display flex, the Flexbox mode. And then we're justifying content space between. So because this is a column for the flex direction, the space between is going to be right here in between these three elements, this little box here, the description and the title. And so here, as I'm having additional space added or removed vertically, they are centering themselves. And this just makes for a much neater layout. I've used this a lot here without that justification, it looks a lot more messy. But because of the stretching here because the Flexbox, because this justify content space between, I use this all the time. And I think it's really important to know that with Flexbox, you can change the direction and you can justify your content in unique ways. It doesn't have to be just centered. You can also do center. You can do a space around as well or space evenly. But in this case, I think that the best way to justify is space between because this way we are ensuring that the first and last element here in our layout, which is going to be this H3 and this little visual box, remain flush to the top and bottom of our cards. Here's where we get into some techniques with a little less current browser support. I like to call this one clamping my style and it's a really neat trick. Remember at the beginning of this video, we talked about min, max and clamp. Well, here's where it can come into play for layout and element style. Here I'm specifying the width using this clamp function and I'm setting clamp 23 CH, 50% 46 CH. What does this do? Let me show you exactly what's going on here. As I increase the size of this element, the parent container here, it is going to increase the size of this card. And as I decrease it, it decreases it. But what we are setting here is a minimum and maximum size for it to clamp to as it reaches that 50% size. This card wants to be at 50% width, but if that 50% means that it's bigger than 46 CH which stands for 46 characters, then it's going to stop getting bigger. The same thing happens with the smaller viewport size. It's going to stop decreasing in size when 50% of its parent size means that it is smaller than 23 CH, which is 23 characters. And these specific character units can be used to make legibility a little bit easier. So in this case, I don't want this card to get any smaller than that because then the paragraph gets harder to read. And same thing with getting bigger than 46 characters, then it gets too long and hard to read. So you can use width, clamp, minimum size, the actual size and the max size to create some really cool responsiveness within this element itself. And you can also use clamp for font size. That's a really great use case too. You can have this responsive flexible typography. You can have like clamp 1.5 REMS and then a like say 10 VW for the viewport width and then three REMS as the biggest size. And that way, as you resize your window, you're going to be having a minimum value of 1.5 REMS, maximum value of three REMS and then you're going to have that grow and shrink into that remaining space. So it's pretty cool to see this actually working in a browser. Again, this doesn't have full browser supports but it is a really great technique. So if you're using this, make sure that you have fallbacks and do your browser testing. And finally, we are at the end and this is the last layout tool. And this layout tool is the most experimental of the bunch. This was recently introduced to Chrome Canary and Chrome 84 plus. And there is an active effort from Firefox in getting this implemented in browser but it is currently not in any stable browsers at the time of this recording. I do want to mention it though because it is such a frequently met problem and it's just simply maintaining the aspect ratio of an image or a video or of an eye frame. And so what this is, is respecting the aspect. Respect for aspect, that's the name I gave it and it is the aspect ratio property. Oh my gosh, I'm going to be so excited when this is implemented in all browsers. What this is doing is, as I am resizing this parent element, the image here, so this is just like, I gave it a class of visual for this box here, that is right here. This is getting an aspect ratio of 16 to nine and no matter how I increase it or decrease it, it is going to keep that aspect ratio. This is something that is so needed for when you are pulling in content from a CMS or otherwise and you have a specific dimension that you have that media at. And this, for example, in the previous example, if I scroll up, as I resize here, since I'm only setting a height and it's just getting the 100% width of whatever remaining space that I'm providing here, is actually changing the aspect ratio of the sole visual box and that's not what we want. That is going to make you force a decision on if you want to either fit the content inside of there so it's smaller and fits it in the space or if you stretch it out and you have it fill the content and then you're only seeing a piece of that media and not the full image and that's also going to cause all sorts of problems. So having this aspect ratio is very exciting. You can also set this to a square if you do one over one. You could have this be one over two where you have this actually longer than it is wide within this parent here. So that's going to be maintaining that aspect ratio and it's just exciting. So I wanted to mention it and let you know that it's coming down the pipeline. And those are the 10 really powerful lines of CSS that I wanted to talk about in this one line layout video. I hope that you all learned something new and if you're looking for these demos, check out onelinelayouts.glitch.me. Thank you all for watching. If you want more CSS content and to dive deeper into layout techniques, including all the bells and whistles of CSS Grid and Flexbox, check out the CSS podcast that I do with my co-host Adam Argyle. It is at pod.link slash the CSS podcast. Thank you again. Enjoy the rest of your web day.