Left and right display in CSS
Left-Right Display in CSS
Left-right display is a common layout method in web design, making the page look more neat and beautiful. Through CSS style settings, we can easily achieve the left-right display effect. This article will explain how to achieve left-right display in CSS in detail and provide several sample code examples for reference.
1. Using the float property to achieve left-right display
The float property allows an element to float left or right, thus achieving a left-right display effect. Here is a simple example code:
<!DOCTYPE html>
html>
<head>
<style>
.left {
float: left;
width: 50%;
}
.right {
float: right;
width: 50%;
}
</style>
</head>
<body>
<div class="left">
<h2>Left Content</h2>
<p>This is the content displayed on the left. </p>
</div>
<div class="right">
<h2>Right Content</h2>
<p>This is the content displayed on the right. </p>
</div>
</body>
</html>
Output:

In the above example, we define two div elements, one with class=”left” and the other with class=”right”, and set the float property to left and right, respectively, to achieve the left-right display effect.
2. Using Flexbox Layout to Achieve Left-Right Display
Flexbox layout is a powerful layout method that can easily achieve various complex layout effects. Here’s an example using flexbox layout to implement left and right display:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
}
.left {
flex: 1;
}
.right {
flex: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<h2>Left Content</h2>
<p>This is the content displayed on the left. </p>
</div>
<div class="right">
<h2>Right Content</h2>
<p>This is the content displayed on the right. </p>
</div>
</div>
</body>
</html>
Output:

In the example above, we define a parent element, container , set its display to flex , and then set the flex property of the left and right child elements inside to 1 to achieve the left-right display effect.
3. Use grid layout to achieve left and right display
Grid layout is a new layout method added in CSS3 that can achieve more flexible and complex layout effects. Here’s an example code using a grid layout to implement left and right display:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
grid-template-columns: 1fr 1fr;
}
.left {
grid-column: 1 / 2;
}
.right {
grid-column: 2 / 3;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<h2>Left Content</h2>
<p>This is the content displayed on the left. </p>
</div>
<div class="right">
<h2>Right Content</h2>
<p>This is the content displayed on the right. </p>
</div>
</div>
</body>
</html>
Output:

In the above example, we define a parent element, container , set its display to grid , and define two columns using the grid-template-columns property. Then, we set the grid-column property on the left and right child elements to achieve left-right display.
4. Using the position property to achieve left-right display
In addition to the layout methods described above, you can also use the position property to achieve left-right display. Here’s an example code using the position property to achieve left and right display:
<!DOCTYPE html>
<html>
<head>
<style>
.left {
position: absolute;
left: 0;
width: 50%;
}
.right {
position: absolute;
right: 0;
width: 50%;
}
</style>
</head>
<body>
<div class="left">
<h2>Left Content</h2>
<p>This is the content displayed on the left. </p>
</div>
<div class="right">
<h2>Right Content</h2>
<p>This is the content displayed on the right. </p>
</div>
</body>
</html>
In the example above, we define two div elements, one with class=”left” and the other with class=”right”. We set the position attribute to absolute for each, and use the left and right attributes to control the left and right positions, achieving a left-right display effect.
Through the above example code, we can see several common ways to implement left-right display in CSS. Based on actual needs and layout complexity, choose the appropriate method to achieve left-right display effects, making the page layout more beautiful and neat.