Lecture 8. CSS Layout

📚 Lecture 8. CSS Layout.pdf

Brief overview

This document details modern and traditional CSS layout techniques essential for web page structure. Understanding these methods is crucial for creating visually appealing and functional websites that adapt to various screen sizes.

Key points

  • Overview of older CSS layout methods including block, inline, inline-block, floats, and positioning.
  • Introduction to Flexbox for one-dimensional content arrangement and its core properties.
  • Explanation of CSS Grid for powerful two-dimensional layout design and content placement.
  • Exploration of responsive web design principles and adaptive layout strategies.
  • Practical application of layout techniques for different screen sizes and devices.

1. Older CSS layout methods

1.1 Basic display modes

CSS <span style="color: #ff9f29; font-weight: 700;">display</span> property controls how elements are shown. Elements are generally either <span style="color: #ff9f29; font-weight: 700;">block</span> or <span style="color: #ff9f29; font-weight: 700;">inline</span> by default.

  • Block-level elements:

    • Start on a new line.
    • Cannot share a line with other block elements.
    • Respect the CSS box model (margins, padding, borders, colors) 1.
  • Inline elements:

    • Do not start new lines.
    • Flow horizontally on the same line.
    • Content wraps to a new line if space is insufficient.
    • Ignore width, height, and top/bottom margins.

1.2 Inline-block display mode

The <span style="color: #ff9f29; font-weight: 700;">inline-block</span> display mode combines features of both block and inline elements.

  • Inline-block elements:
    • Sit on the same line, wrapping as needed.
    • Respect box model properties like width and height.
    • Allow control over margins.

The display property lets you change an element's mode to <span style="color: #ff9f29; font-weight: 700;">block</span>, <span style="color: #ff9f29; font-weight: 700;">inline</span>, or <span style="color: #ff9f29; font-weight: 700;">inline-block</span> 2.

1.3 Floating elements

The <span style="color: #ff9f29; font-weight: 700;">float</span> property moves an element to the far left or right of its container. Surrounding content then reflows around the floated element 3.

📝 Example: A floated image will have text wrap around its right side if floated left, or around its left side if floated right.

1.3.1 Problems with floats

Using floats can lead to common layout issues.

  • Margin collapse: Margins of floated elements do not collapse like they do for non-floated elements.

  • Parent element height: If a parent element contains only floated children, the parent may appear to disappear. The browser assumes it has no height because its children are out of the normal flow 7.

1.4 Positioning elements

The <span style="color: #ff9f29; font-weight: 700;">position</span> property defines how an element is placed on a page. The top, right, bottom, and left properties then control its exact placement.

1.4.1 Relative positioning

With <span style="color: #ff9f29; font-weight: 700;">relative</span> positioning, an element moves from its original spot. It shifts based on where it would have been in the normal flow.

  • Normal flow preservation: Other content retains knowledge of the element's original position. The space it occupied is preserved, and the document flow remains intact 8.

1.4.2 Absolute positioning

Absolute positioning places elements relative to their nearest positioned ancestor. If no ancestor is positioned, it is relative to the initial containing block (usually the viewport).

🔑 Important: Relative positioning is often used to create a positioning context for a subsequent absolutely positioned element.

1.5 Overlapping and hiding elements

CSS allows you to place elements on top of each other and to selectively hide or show content 11.

1.5.1 Hiding elements

There are two primary ways to hide elements:

PropertyEffectSpace Occupied
displayRemoves the element from the flow.No
visibilityHides the element but keeps its space.Yes

🔑 Important: Use display: none; to completely remove an element and its space. Use visibility: hidden; to hide an element while preserving its layout space 12.

🔑 Important: Older CSS layout methods like floats and absolute positioning can be complex. Understanding basic display modes and the impact of position, float, display, and visibility is crucial for predictable layouts.

2. Flexbox layout en Grid layout

2.1 Flexbox layout

Flexbox layout is designed for arranging content in one dimension, either a row or a column. Grid layout is built for two-dimensional arrangements.

Both are display properties that can be applied to any element 13.

The core idea of flexbox involves two types of elements: a <span style="color: #ff9f29; font-weight: 700;">flex container</span> and <span style="color: #ff9f29; font-weight: 700;">flex items</span> within it. Items are positioned in source order along a single main axis.

  • Flex container: The parent element with display: flex.
  • Flex items: The direct children of the flex container.

If more items are added than can fit, they will wrap to a new line by default. This wrapping behavior is controlled by the flex-wrap property 15 16.

Flexbox arranges items along one axis.

2.1.1 Flexbox properties for alignment

Flexbox offers properties to control the alignment of items within the container.

PropertyAxisDescription
justify-contentMain axisAligns flex items along the main axis of the container.
align-itemsCross axisAligns flex items along the cross axis of the container.

2.1.2 Flexbox for card layouts

Flexbox is useful for creating responsive card components. A common use case is ensuring a card's footer stays at the bottom, even with variable content height.

Flexbox allows the content area to grow, pushing the footer down 18.

2.2 Grid layout

Grid layout is a powerful and relatively easy-to-learn system for arranging content in two dimensions: rows and columns. It allows you to divide any container into a grid of cells.

  • Child elements are automatically placed into available cells.
  • You can also explicitly define which content goes into which cell.

2.2.1 Specifying the grid structure

You can define the grid's structure by setting display: grid on the parent container. Child block-level elements will then automatically populate these grid cells 19.

You can specify row heights using the grid-template-rows property. Similar to columns, the fr unit can be used.

The repeat() function helps define repeating patterns of columns or rows 21 22.

2.2.2 Explicit grid placement

By default, children are placed implicitly. However, you can explicitly control their placement.

This is done using properties like grid-column-start, grid-column-end, grid-row-start, and grid-row-end 24.

  • You can make an item span multiple cells by defining its start and end lines.
  • Items can be explicitly moved to different rows and columns than their default position.
  • If an item needs more space than available rows, the grid can automatically add a new row based on its content or grid-template-rows 25.

Grid layout defines a structure of rows and columns.

2.2.3 Cell and container alignment in Grid

Grid layout provides properties for aligning content both within individual cells and across the entire grid container 26.

Alignment TypePropertyDescription
Within grid celljustify-itemsAligns grid items horizontally within their cells.
Within grid cellalign-itemsAligns grid items vertically within their cells.
Entire grid containerjustify-contentAligns the grid along the row axis within the container.
Entire grid containeralign-contentAligns the grid along the column axis within the container.

2.2.4 Nested grids

Any container element can have its display property set to grid. This allows for grids to be nested within other grids, enabling complex layouts 27.

Flexbox is for 1D, Grid is for 2D.

🔑 Important: Understanding how to explicitly place items in Grid is crucial for creating complex, non-standard layouts and for overriding auto-placement when needed.

3. Responsive web design

3.1 Introduction to responsive design

Responsive web design adapts layouts to screen size changes. This goes beyond simple percentage scaling.

Elements like navigation may change or shrink on smaller screens 28.

3.2 Key components of responsive design

Four main elements enable responsive design. These work together to create adaptable web pages.

ComponentDescription
Flexible gridsLayouts that adjust based on available space.
Viewport meta tagControls the initial size and zoom level of the browser window.
Media queriesApply CSS rules based on device characteristics.
Scaling imagesImages resize to fit the screen.

3.3 Setting viewports

The viewport is the visible area of the browser window. Developers can control this area using a <meta> tag.

This tag tells the browser how to size the initial viewport 29 30.

  • width attribute: Controls the size of the viewport in pixels.
  • initial-scale attribute: Sets the zoom level when the page first loads.

This ensures the viewport matches the device's screen width. A 320-pixel wide screen will result in a 320-pixel viewport 31.

📝 Example: <meta name="viewport" content="width=device-width, initial-scale=1.0"> This tag sets the viewport width to the device's width and prevents initial zooming.

3.4 Customizing with media queries

Media queries are a core part of responsive design. They allow CSS rules to be applied based on the display medium.

You can target specific device capabilities.

  • Media queries are Boolean expressions.
  • They can be added to CSS files.
  • They can also be used with <link> elements.

This enables conditional use of external CSS files. Older Internet Explorer versions do not support media queries 32.

3.4.1 Browser features examined by media queries

Media queries can check many device features. Most features have min- and max- versions 33.

FeatureDescription
WidthViewport width.
HeightViewport height.
OrientationDevice orientation (portrait or landscape).
ResolutionScreen resolution.
Aspect-ratioRatio of viewport width to height.
HoverWhether the device supports a hover state.
PointerType of primary pointing device (mouse, pen).
ColorNumber of bits per color component.
GridWhether the device uses a grid display.
max-widthMaximum viewport width.
min-widthMinimum viewport width.
max-heightMaximum viewport height.
min-heightMinimum viewport height.
max-resolutionMaximum screen resolution.
min-resolutionMinimum screen resolution.
max-aspect-ratioMaximum viewport aspect ratio.
min-aspect-ratioMinimum viewport aspect ratio.

3.5 Scaling images

Making images scale to fit the screen is simple. A single CSS rule handles this 36.

  • img {max-width: 100%;}: This rule ensures images never exceed their container's width. If the container shrinks, the image shrinks with it.

🔑 Important: This technique prevents images from overflowing their containers on smaller screens, maintaining layout integrity.

🔑 Important: Responsive design ensures a good user experience across all devices by adapting content to screen size. The key components are flexible grids, viewport meta tags, media queries, and scalable images.

© 2026 Maeve. All rights reserved. This content is protected by copyright.

See also