- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set length to set the gap between the columns in CSS?
CSS is a powerful tool in web development, which allows developers to create multi-column layouts for text content on a web page. Columns are an efficient way to break up long blocks of text into more manageable chunks. One important aspect of CSS columns is the gap between them.
Different Methods to Set Column Gap in CSS
Below are some of the many ways to set the gap between columns in CSS.
1. Using the column-gap Property
The column-gap property is the most common way to set the gap between columns. This property sets the space between the columns in a multi-column layout. It is a shorthand property that combines the column-rule-width and column-rule-style properties. For example −
.column-container { column-count: 3; column-gap: 20px; }
In the above CSS code, to create three columns, we have set the column-count property to 3 and set the column-gap property to 20 pixels to set the center gap.
Example 1
Setting a fixed pixel value for the column gap.
<!DOCTYPE html> <html> <head> <style> body{ background-color: lightgray; } h2 { text-align:center; } .column-container { column-count: 3; column-gap: 20px; column-rule: 3px solid; } </style> </head> <body> <h2>Setting the column gap using the column-gap Property</h2> <div class="column-container"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut quam mauris. Donec euismod, velit eu gravida tempus, massa turpis venenatis velit, a lacinia tortor risus vel risus.</p> <p>Quisque elementum bibendum diam, vel ullamcorper justo laoreet quis. Nulla facilisi. In viverra porttitor dapibus. Vivamus gravida erat ac libero congue convallis.</p> <p>Quisque elementum bibendum diam, vel ullamcorper justo laoreet quis. Nulla facilisi. In viverra porttitor dapibus. Vivamus gravida erat ac libero congue convallis.</p> </div> </body> </html>
2. Using the Gap Property
The gap property is a new CSS property, which was introduced in CSS Grid. We can also use it to set the gap between columns. This is a shorthand property that allows developers to combine row-gap and column-gap properties. For example −
.column-container { column-count: 4; gap: 15px; }
In the above CSS code, to create four columns, we have set the column-count property to 4 and set the column-gap property to 15 pixels to set the center gap.
Example 2
Setting a fixed pixel value for the column gap using gap property.
<!DOCTYPE html> <html> <head> <style> body { background-color: lightgreen; } h2 { text-align: center; } .column-container { column-count: 4; gap: 15px; column-rule: 3px dotted; } </style> </head> <body> <body> <h2>Setting the column gap using the gap Property </h2> <div class="column-container"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut quam mauris. Donec euismod, velit eu gravida tempus, massa turpis venenatis velit, a lacinia tortor risus vel risus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut quam mauris. Donec euismod, velit eu gravida tempus, massa turpis venenatis velit, a lacinia tortor risus vel risus.Lorem ipsum dolor sit amet, consectetur adipiscing elit. </div> </body> </body> </html>
3. Using CSS Variables
CSS variables are custom properties, which provide a flexible way to set the gap between columns in a multi-column layout. It allows developers to define reusable values. For Example −
:root { --column-gap: 20px; } .column-container { column-count: 3; column-gap: var(--column-gap); }
In the above CSS code, we have defined a CSS variable called --column-gap and set its value to 20 pixels, and called it in the var() function. To create three columns, we have set the column-count property to 3.
Example 3
Setting a fixed pixel value for the column gap using CSS variable.
<!DOCTYPE html> <html> <head> <style> body{ background-color: lightgray; } h2 { text-align:center; } .my-container { column-count: 3; column-gap: var(--column-gap); column-rule: 3px dotted; } </style> </head> <body> <h2>Setting the column gap using the CSS variable</h2> <div class="my-container" style="--column-gap: 20px";> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut quam mauris. Donec euismod, velit eu gravida tempus, massa turpis venenatis velit, a lacinia tortor risus vel risus.</p> <p>Quisque elementum bibendum diam, vel ullamcorper justo laoreet quis. Nulla facilisi. In viverra porttitor dapibus. Vivamus gravida erat ac libero congue convallis.</p> <p>Quisque elementum bibendum diam, vel ullamcorper justo laoreet quis. Nulla facilisi. In viverra porttitor dapibus. Vivamus gravida erat ac libero congue convallis.</p> </div> </body> </html>
Conclusion
In the above article, we have discussed several methods to set the column gap between columns, including the column-gap property, the gap property, and CSS variables. Overall, setting the gap between columns in CSS is a useful technique for organizing content on a web page.