CSS layout is used to control the position, size, alignment, and spacing of elements on a webpage. It helps organize content into different sections such as headers, navigation bars, sidebars, content areas, and footers.
CSS provides several layout techniques, including normal flow, display, positioning, Flexbox, Grid, and multi-column layouts. Each technique is useful for different types of webpage structures.
The main CSS layout techniques are:
Normal flow is the default layout behavior of HTML elements. Block-level elements normally appear one after another, while inline elements appear within the same line when enough space is available.
The following example shows how block-level elements are arranged in the normal document flow.
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 20px;
margin: 5px;
background-color: lightblue;
}
</style>
</head>
<body>
<div>First Element</div>
<div>Second Element</div>
<div>Third Element</div>
</body>
</html>
The display property controls how an element is displayed and how it participates in the layout. Common values include block, inline, inline-block, flex, grid, and none.
The following example shows how the display property can place elements on the same line.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
display: inline-block;
width: 100px;
padding: 20px;
margin: 5px;
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
</body>
</html>
CSS positioning allows you to control the location of an element. The position property supports values such as static, relative, absolute, fixed, and sticky.
The following example shows how to position an element relative to its normal position.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
position: relative;
left: 50px;
width: 150px;
padding: 20px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="box">Positioned Element</div>
</body>
</html>
The position: absolute value removes an element from the normal document flow and positions it relative to its nearest positioned ancestor.
The following example shows how to position an element inside a container using absolute positioning.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
width: 300px;
height: 200px;
background-color: lightgray;
}
.box {
position: absolute;
right: 20px;
bottom: 20px;
padding: 20px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box</div>
</div>
</body>
</html>
Fixed positioning keeps an element in the same position relative to the browser viewport. The element remains visible at that position while the page is scrolled.
The following example shows how to create a fixed element at the bottom of the browser window.
<!DOCTYPE html>
<html>
<head>
<style>
.footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
padding: 15px;
background-color: lightblue;
text-align: center;
}
</style>
</head>
<body>
<p>Scroll the page to see the fixed element.</p>
<div class="footer">Fixed Footer</div>
</body>
</html>
Sticky positioning combines features of relative and fixed positioning. An element behaves normally until it reaches a specified position, after which it remains visible while scrolling.
The following example shows how to create a sticky header.
<!DOCTYPE html>
<html>
<head>
<style>
.header {
position: sticky;
top: 0;
padding: 15px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="header">Sticky Header</div>
<p>Scroll down the page to see the sticky header.</p>
<p>Content of the webpage...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
</body>
</html>
Flexbox is a one-dimensional layout system used to arrange elements in a row or column. It provides properties for controlling direction, alignment, spacing, and the size of flex items.
The following example shows how to arrange elements horizontally using Flexbox.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
gap: 10px;
}
.box {
padding: 20px;
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
</div>
</body>
</html>
The flex-direction property defines the direction in which flex items are placed. The commonly used values are row, row-reverse, column, and column-reverse.
The following example shows how to arrange Flexbox items vertically.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
flex-direction: column;
gap: 10px;
}
.box {
padding: 20px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
</div>
</body>
</html>
Flexbox provides justify-content and align-items properties to control the alignment of flex items along the main and cross axes.
The following example shows how to center an element horizontally and vertically using Flexbox.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
background-color: lightgray;
}
.box {
padding: 20px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Centered Box</div>
</div>
</body>
</html>
CSS Grid is a two-dimensional layout system that arranges elements into rows and columns. It is useful for creating page structures and layouts that require control over both dimensions.
The following example shows how to create a simple three-column grid.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.box {
padding: 20px;
background-color: lightblue;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</body>
</html>
CSS multi-column layout divides text content into multiple columns. The column-count property specifies the number of columns used to display the content.
The following example shows how to divide text into three columns.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.box {
padding: 20px;
background-color: lightblue;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</body>
</html>
Responsive layout allows a webpage to adjust its structure and size according to the available screen space. Flexbox, Grid, relative units, and media queries can be combined to create layouts that work across different screen sizes.
The following example shows how a grid can change from two columns to one column on smaller screens.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
.box {
padding: 30px;
background-color: lightgreen;
}
@media screen and (max-width: 600px) {
.container {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
<div class="box">Box 4</div>
</div>
</body>
</html>
The z-index property controls the stacking order of positioned elements. An element with a higher z-index value is displayed above an element with a lower value.
The following example shows how to control the stacking order of two overlapping elements.
<!DOCTYPE html>
<html>
<head>
<style>
.box1 {
position: absolute;
top: 40px;
left: 40px;
width: 100px;
height: 100px;
background-color: lightblue;
z-index: 1;
}
.box2 {
position: absolute;
top: 80px;
left: 80px;
width: 100px;
height: 100px;
background-color: lightgreen;
z-index: 2;
}
</style>
</head>
<body>
<div class="box1">Box 1</div>
<div class="box2">Box 2</div>
</body>
</html>
We request you to subscribe our newsletter for upcoming updates.