Layout and Positioning

Section 2: Layout and Positioning

Lesson 1: CSS Display Property

The CSS display property determines how elements are rendered on the page. This lesson covers various display types, including block, inline, and inline-block, as well as more advanced layout options using flexbox and grid.

1.1 Block, Inline, and Inline-Block Display Types

Block:

div {

    display: block;

}

Inline:

span {

    display: inline;

}

Inline-Block: 

li {

    display: inline-block;

}

1.2 CSS Flexbox and Grid for Layout

Flexbox and grid are powerful layout tools in CSS, enabling efficient arrangement of elements in both one-dimensional and two-dimensional layouts.

Flexbox:

.container {

    display: flex;

    justify-content: space-between;

}

Grid:

.grid-container {

    display: grid;

    grid-template-columns: repeat(3, 1fr);

}

Lesson 2: Positioning Elements

CSS positioning allows precise control over the placement of elements on a page. This lesson explores various positioning options, including static, relative, absolute, and fixed, along with concepts like z-index for stacking.

2.1 Static, Relative, Absolute, and Fixed Positioning

Static: 

p {

    position: static;

}

Relative:

.relative-box {

    position: relative;

    top: 10px;

    left: 20px;

}

Absolute:

.absolute-box {

    position: absolute;

    top: 0;

    right: 0;

}

Fixed:

.fixed-box {

    position: fixed;

    bottom: 10px;

    left: 10px;

}

2.2 Z-Index and Stacking Context

Z-index determines the stacking order of positioned elements. Higher z-index values appear above elements with lower values. 

.header {

    position: relative;

    z-index: 100;

}


.modal {

    position: fixed;

    z-index: 200;

}

Lesson 3: Responsive Design with Media Queries

Responsive design ensures that web pages look good on a variety of devices and screen sizes. This lesson introduces media queries for creating responsive layouts.

3.1 Introduction to Media Queries

Media queries allow you to apply CSS rules based on characteristics like screen width, height, and device orientation. 

@media screen and (max-width: 600px) {

    /* Styles for screens up to 600px wide */

    body {

        font-size: 14px;

    }

}

3.2 Creating Responsive Layouts for Different Devices

Adjusting layout and styling based on media queries ensures a seamless user experience across various devices.

@media screen and (max-width: 768px) {

    .sidebar {

        display: none;

    }


    .content {

        width: 100%;

    }

}

Understanding display properties, positioning, and responsive design principles is essential for crafting visually appealing and functional web layouts. In the upcoming sections, we'll explore advanced layout techniques and CSS animations.