Lesson 3
Mastering the Galactic Art of Fluid Layouts and Responsive Typography
Topic Overview and Actualization

Hello, space cadet! Are you eager to venture further into the galaxy of Responsive Design Land? We're going to master techniques such as fluid grid layouts, flexible images, viewport units, and responsive typography. Together, we'll design a stellar, device-friendly website!

Fluid Grid Layouts

Think of your layout as a chameleon, capable of adapting seamlessly to varied terrains, whether it be a vast desktop or a tiny mobile device. Fluid grid layouts morph in size as the window size fluctuates, thanks to percentage values. In this example, we divide a row into three stackable columns:

CSS
1.container { width: 100%; } /* Full width */ 2.column { width: 33.33%; float: left; } /* Each column consumes one third of the container's width */
Flexible Images

Facing an alien army of screens, both big and small? Your images should be able to resize to fit on any battleground! Flexible images can adapt to any theatre, all thanks to the max-width and height: auto.

The example below rescales images based on screen size:

CSS
1img { max-width: 100%; height: auto; }

In the above example, images scale up or down while preserving their aspect ratio, thanks to height: auto.

Viewport Units: vh, vw

Viewport units are units of measurement in CSS based on the size of the viewport, which is the visible area of a webpage on your screen. They enable sizes to adjust according to the viewport, thereby improving the responsiveness of your webpage.

vh unit: 1vh represents 1% of the viewport's height. So, a value of 50vh would be half the height of the viewport.

vw unit: 1vw represents 1% of the viewport's width. For instance, if you want a button to always be 30% of the screen width, you can give it a width of 30vw.

CSS
1.hero { height: 100vh; text-align: center; padding: 3vw; } /* Full-screen hero, padding is 3% of viewport width */
Viewport Units: vmin and vmax

Viewport units vmin and vmax are based on the smaller and larger dimension of the viewport, respectively.

  • vmin: Represents 1% of the viewport's smaller dimension. If the viewport is 900px wide and 700px tall, 1vmin would equal 7px because the smaller dimension is the height (700px).
  • vmax: Represents 1% of the viewport's larger dimension. For the viewport sizes mentioned above, 1vmax would equal 9px.

They can be particularly useful when trying to create designs that scale dynamically with the viewport size. Here's an example on using vmin and vmax:

CSS
1body { 2 font-size: 3vmin; /* Font size will resize based on smaller dimension of viewport */ 3 line-height: 4vmax; /* line-height will resize based on larger dimension of viewport */ 4}

In this example, the font-size will always be 3% of the viewport's smaller dimension, making the text readable on devices with wide or narrow screens. The line-height is 4% of the viewport's larger dimension, ensuring there's ample vertical space between lines on larger screens.

Responsive Typography

Fonts should be as flexible as your gymnast layout! The em and rem units enable type sizes to be scaled, complementing CSS typography properties. For example, let's scale headings in proportion to the body's font size.

CSS
1body { font-size: 16px; } /* default size */ 2h1 { font-size: 2em; } /* double the body's font size */
Box-sizing

The box-sizing property allows us to include the padding and border in an element's total width and height.

By default, when you set the width and height of an element, it doesn't take into account any border or padding applied to the element. This means that if you have a box with a width of 300px and padding of 20px, the actual render size of that box would be 340px (300px width + 2 * 20px padding). But if you want the width of the box to always remain 300px regardless of padding or border, the box-sizing property can come in handy.

  • content-box: This is the default value. The width and height properties include only the content of the element. Any padding or border added to the element is added to this base width and height, resulting in a larger total size of the element. It does not include padding or borders.
CSS
1.box1 { 2 box-sizing: content-box; 3 width: 300px; 4 padding: 10px; 5 border: 5px solid black; 6}

In the above example, the .box1 element has a content area width of 300px, and the padding and border are added outside of this width, making the actual rendered width of the box 330px (300px width + 2 * 10px padding + 2 * 5px border).

  • border-box: The width and height properties include the content, padding, and border, but do not include the margin. This means that any padding or border you add won't affect the final dimensions of the element as they're contained within the width and height.
CSS
1.box2 { 2 box-sizing: border-box; 3 width: 300px; 4 padding: 10px; 5 border: 5px solid black; 6}

In this second example, the .box2 element has a total width of 300px that includes the content width, padding, and border. It means that the content area width will be less than 300px because the space for padding and border will be taken from the specified width.

Note: The box-sizing property isn't inherited by default, but you can make it inheritable by declaring box-sizing: inherit on an element. This can be useful when you want consistent box-sizing behavior across your project.

Lesson Summary and Practice

Mission accomplished, astronaut! You've wrestled with alien aspects of responsive design and tamed them! Now, let's get our hands dirty and apply these concepts to a fun and engaging real-life project. Buckle up for some captivating hands-on practice in our next assignment, and let's blast those bugs off your code! Have a safe journey!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.