Home > Web Front-end > CSS Tutorial > How can I create a three-column horizontal layout with CSS only, without modifying the HTML structure?

How can I create a three-column horizontal layout with CSS only, without modifying the HTML structure?

Linda Hamilton
Release: 2024-11-12 07:06:02
Original
518 people have browsed it

How can I create a three-column horizontal layout with CSS only, without modifying the HTML structure?

3 Column Layout with CSS Only

In HTML, you have a layout with three columns arranged vertically:

<div class="container">
   <div class="column-center">Column center</div>
   <div class="column-left">Column left</div>
   <div class="column-right">Column right</div>
</div>
Copy after login

You want to arrange these columns horizontally, similar to the grid below, without altering the HTML structure:

----------------------------------------------------
|              |                   |               |
| Column left  |   Column center   | Column right  |
|              |                   |               |
----------------------------------------------------
Copy after login

To achieve this, use CSS properties:

.column-left{ float: left; width: 33.333%; }
.column-right{ float: right; width: 33.333%; }
.column-center{ display: inline-block; width: 33.333%; }
Copy after login

See the live demo here.

For a more flexible grid system, create a grid system with a larger number of columns:

.column {
    float: left;
    position: relative;
    width: 20%;
    
    /*for demo purposes only */
    background: #f2f2f2;
    border: 1px solid #e6e6e6;
    box-sizing: border-box;
}

.column-offset-1 {
    left: 20%;
}
.column-offset-2 {
    left: 40%;
}
.column-offset-3 {
    left: 60%;
}
.column-offset-4 {
    left: 80%;
}

.column-inset-1 {
    left: -20%;
}
.column-inset-2 {
    left: -40%;
}
.column-inset-3 {
    left: -60%;
}
.column-inset-4 {
    left: -80%;
}
Copy after login

And use HTML like this:

<div class="container">
   <div class="column column-one column-offset-2">Column one</div>
   <div class="column column-two column-inset-1">Column two</div>
   <div class="column column-three column-offset-1">Column three</div>
   <div class="column column-four column-inset-2">Column four</div>
   <div class="column column-five">Column five</div>
</div>
Copy after login

The above is the detailed content of How can I create a three-column horizontal layout with CSS only, without modifying the HTML structure?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template