HTML (Hypertext Markup Language) is used to structure the content of a web page. To create columns, we use HTML elements such as <div>
which are used as containers. These <div>
elements act as the building blocks for our layout. For example, we can create a main container <div>
and then nested <div>
elements inside it to represent columns and sub - columns.
CSS (Cascading Style Sheets) is used to style the HTML elements. To create columns, we use CSS properties like display
, flexbox
, grid
, and float
. Each method has its own advantages and use - cases.
To use the float method, we first create the HTML structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.main - container {
width: 100%;
overflow: auto;
}
.column {
float: left;
width: 50%;
}
.sub - column {
float: left;
width: 50%;
}
</style>
</head>
<body>
<div class="main - container">
<div class="column">
<div class="sub - column">Sub - column 1</div>
<div class="sub - column">Sub - column 2</div>
</div>
<div class="column">
<div class="sub - column">Sub - column 3</div>
<div class="sub - column">Sub - column 4</div>
</div>
</div>
</body>
</html>
In this example, we float the columns and sub - columns to the left and set their widths. The overflow: auto
on the main container is used to clear the floats.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.main - container {
display: flex;
}
.column {
flex: 1;
display: flex;
flex - direction: column;
}
.sub - column {
flex: 1;
}
</style>
</head>
<body>
<div class="main - container">
<div class="column">
<div class="sub - column">Sub - column 1</div>
<div class="sub - column">Sub - column 2</div>
</div>
<div class="column">
<div class="sub - column">Sub - column 3</div>
<div class="sub - column">Sub - column 4</div>
</div>
</div>
</body>
</html>
Here, we use display: flex
to turn the main container and columns into flex containers. The flex: 1
property distributes the available space equally among the child elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<style>
.main - container {
display: grid;
grid - template - columns: repeat(2, 1fr);
}
.column {
display: grid;
grid - template - columns: repeat(2, 1fr);
}
</style>
</head>
<body>
<div class="main - container">
<div class="column">
<div>Sub - column 1</div>
<div>Sub - column 2</div>
</div>
<div class="column">
<div>Sub - column 3</div>
<div>Sub - column 4</div>
</div>
</div>
</body>
</html>
In this example, we use display: grid
to create grid containers. The grid - template - columns
property defines the number and size of the columns.
Regardless of the method used, it is important to make the layout responsive. This can be achieved using media queries in CSS. For example, when the screen size is small, we can change the layout to a single column.
@media (max - width: 768px) {
.main - container {
display: block;
}
.column {
width: 100%;
}
.sub - column {
width: 100%;
}
}
Use semantic HTML elements like <section>
, <article>
, and <aside>
instead of just using <div>
elements. This makes the code more readable and accessible.
If you need to create a complex nested column layout, CSS Grid is the best choice. It provides more control over the layout and is easier to manage compared to the float method.
Avoid using inline styles as much as possible. Instead, use external or internal CSS files to separate the structure (HTML) from the presentation (CSS).
Adding columns within columns in HTML and CSS is a powerful technique that allows you to create complex and organized web page layouts. We have explored three different methods: float, flexbox, and CSS Grid. Each method has its own advantages, and the choice depends on the complexity of the layout and the browser compatibility requirements. By following common and best practices, you can create responsive and accessible layouts that are easy to maintain.