Introduction to Modern Web Layout

Modern web layout combines HTML5 semantic elements with CSS techniques like Flexbox and Grid to create responsive, accessible, and maintainable website structures.

HTML5 Semantic Layout Elements

1. Basic Semantic Structure

HTML5 introduced semantic elements that describe their meaning to browsers and developers.

Code Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Semantic Layout Example</title>
</head>
<body>
    <header>
        <nav>
            <!-- Navigation content -->
        </nav>
    </header>
    
    <main>
        <article>
            <!-- Main article content -->
        </article>
        
        <aside>
            <!-- Sidebar content -->
        </aside>
    </main>
    
    <footer>
        <!-- Footer content -->
    </footer>
</body>
</html>

Visual Representation:

Header

Main Article

Primary content area

Footer

Footer content

2. Semantic Elements Reference

<header>

Container for introductory content or navigation

<nav>

Navigation links section

<main>

Main content of the document

<article>

Self-contained composition

<section>

Thematic grouping of content

<aside>

Content indirectly related to main content

<footer>

Footer for its nearest section

<figure> & <figcaption>

Self-contained content with caption

CSS Layout Techniques

1. CSS Flexbox

One-dimensional layout model for flexible content distribution.

Code Example:

/* Flexbox Container */
.flex-container {
  display: flex;
  flex-direction: row; /* or column */
  justify-content: space-between; /* main axis alignment */
  align-items: center; /* cross axis alignment */
  flex-wrap: wrap; /* allow wrapping */
  gap: 20px; /* space between items */
}

/* Flexbox Items */
.flex-item {
  flex: 1; /* grow and shrink equally */
  min-width: 200px; /* minimum width */
}

/* Responsive Flexbox */
@media (max-width: 768px) {
  .flex-container {
    flex-direction: column;
    gap: 15px;
  }
  
  .flex-item {
    min-width: 100%;
  }
}

/* Practical Example */
<header class="flex-container">
  <div class="logo">Logo</div>
  <nav class="flex-container">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </nav>
</header>

Output:

2. CSS Grid

Two-dimensional layout system for complex layouts.

Code Example:

/* Grid Container */
.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr; /* three columns */
  grid-template-rows: auto; /* automatic row sizing */
  gap: 20px; /* space between items */
  grid-template-areas: 
    "header header header"
    "sidebar main aside"
    "footer footer footer";
}

/* Grid Items */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }

/* Responsive Grid */
@media (max-width: 1024px) {
  .grid-container {
    grid-template-columns: 1fr 3fr;
    grid-template-areas: 
      "header header"
      "sidebar main"
      "aside aside"
      "footer footer";
  }
}

@media (max-width: 768px) {
  .grid-container {
    grid-template-columns: 1fr;
    grid-template-areas: 
      "header"
      "sidebar"
      "main"
      "aside"
      "footer";
  }
}

/* Practical Example */
<div class="grid-container">
  <header class="header">Header</header>
  <aside class="sidebar">Sidebar</aside>
  <main class="main">Main Content</main>
  <aside class="aside">Additional Info</aside>
  <footer class="footer">Footer</footer>
</div>

Output:

Header
Main Content
Footer

Responsive Design Fundamentals

1. Viewport Meta Tag

Essential for responsive design on mobile devices.

Code Example:

<!-- Essential viewport meta tag -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- Additional viewport options -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<!-- For specific device handling -->
<meta name="viewport" content="width=1200"> <!-- Fixed width -->

2. CSS Media Queries

Apply different styles based on device characteristics.

Code Example:

/* Basic media query structure */
@media media-type and (media-feature) {
  /* CSS rules */
}

/* Common breakpoints */
@media (max-width: 1200px) {
  /* For large tablets and small laptops */
}

@media (max-width: 992px) {
  /* For tablets */
}

@media (max-width: 768px) {
  /* For mobile devices */
}

@media (max-width: 576px) {
  /* For small mobile devices */
}

/* Orientation-based queries */
@media (orientation: landscape) {
  /* Landscape mode styles */
}

@media (orientation: portrait) {
  /* Portrait mode styles */
}

/* Resolution-based queries */
@media (min-resolution: 2dppx) {
  /* High-resolution displays */
}

/* Combined queries */
@media (max-width: 768px) and (orientation: portrait) {
  /* Mobile portrait specific styles */
}

/* Mobile-first approach (min-width) */
@media (min-width: 576px) {
  /* Small devices and up */
}

@media (min-width: 768px) {
  /* Medium devices and up */
}

@media (min-width: 992px) {
  /* Large devices and up */
}

@media (min-width: 1200px) {
  /* Extra large devices */
}

Practical Responsive Layout

1. Complete Responsive Example

A fully responsive layout using semantic HTML and modern CSS.

Code Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Layout Example</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        /* Header Styles */
        header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 0;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
        }

        .nav-menu {
            display: flex;
            list-style: none;
            gap: 2rem;
        }

        .nav-menu a {
            color: white;
            text-decoration: none;
            transition: opacity 0.3s;
        }

        .nav-menu a:hover {
            opacity: 0.8;
        }

        /* Main Content */
        main {
            padding: 2rem 0;
        }

        .hero {
            text-align: center;
            padding: 3rem 0;
            background: #f8f9fa;
            margin-bottom: 2rem;
        }

        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
            margin-bottom: 2rem;
        }

        .feature-card {
            background: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }

        /* Footer */
        footer {
            background: #2c3e50;
            color: white;
            padding: 2rem 0;
            text-align: center;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 1rem;
            }

            .nav-menu {
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }

            .hero {
                padding: 2rem 0;
            }

            .features {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 480px) {
            .container {
                padding: 0 15px;
            }

            .feature-card {
                padding: 1.5rem;
            }

            .hero h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <div class="header-content">
                <div class="logo">MyWebsite</div>
                <nav>
                    <ul class="nav-menu">
                        <li><a href="#home">Home</a></li>
                        <li><a href="#about">About</a></li>
                        <li><a href="#services">Services</a></li>
                        <li><a href="#contact">Contact</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>

    <main>
        <div class="container">
            <section class="hero">
                <h1>Welcome to Our Website</h1>
                <p>Discover amazing features and services</p>
            </section>

            <section class="features">
                <article class="feature-card">
                    <h3>Feature One</h3>
                    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
                </article>
                <article class="feature-card">
                    <h3>Feature Two</h3>
                    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
                </article>
                <article class="feature-card">
                    <h3>Feature Three</h3>
                    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
                </article>
            </section>
        </div>
    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024 MyWebsite. All rights reserved.</p>
        </div>
    </footer>
</body>
</html>

Best Practices

Semantic HTML

  • Use appropriate semantic elements
  • Maintain proper heading hierarchy
  • Use landmarks for accessibility
  • Provide meaningful alt text for images
  • Ensure proper document structure

Responsive Design

  • Always include viewport meta tag
  • Use relative units (em, rem, %) instead of pixels
  • Implement mobile-first design approach
  • Test on multiple devices and screen sizes
  • Use CSS Grid and Flexbox for layouts

Performance

  • Optimize images for different screen sizes
  • Use responsive images with srcset
  • Minimize CSS and JavaScript
  • Implement lazy loading for images
  • Use efficient CSS selectors

Accessibility

  • Ensure proper color contrast
  • Maintain keyboard navigation
  • Use ARIA landmarks when needed
  • Test with screen readers
  • Provide text alternatives for media

Common Breakpoints

Device TypeBreakpointUsage
Mobile (small)≤ 576pxSmartphones portrait
Mobile (large)≤ 768pxSmartphones landscape, small tablets
Tablet≤ 992pxTablets portrait
Desktop (small)≤ 1200pxTablets landscape, small laptops
Desktop (large)≥ 1200pxLarge screens, desktops

Tools and Resources

Chrome DevTools

Device simulation and responsive testing

CSS Frameworks

Bootstrap, Tailwind CSS, Foundation

Testing Tools

BrowserStack, LambdaTest, Responsive Design Checker

Performance

Lighthouse, PageSpeed Insights, WebPageTest