Home > Web Front-end > CSS Tutorial > Example code sharing for displaying multiple div boxes side by side in CSS

Example code sharing for displaying multiple div boxes side by side in CSS

黄舟
Release: 2017-07-22 10:32:22
Original
12357 people have browsed it

1. How to implement DIV layout with two columns, one row and two columns in div, solution of DIV layout tutorial with two columns, one row and two columns in div
To realize two DIV boxes side by side, and only display two columns of DIV layout in one row, use width + Float can realize a DIV layout showing two columns in one row.
The first case, the float floats are the same
Make the float values ​​of the two div boxes the same, for example, set float:left or float:right, and set the width appropriately. Here we use a case to implement a one-row and two-column DIV layout.
1), complete example DIV+CSS code:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>两个DIV并排</title> 
<style> 
.div-a{ float:left;width:49%;border:1px solid #F00} 
.div-b{ float:left;width:49%;border:1px solid #000} 
</style> 
</head> 
<body> 
<div class="div-a">第一个DIV盒子</div> 
<div class="div-b">第二个DIV盒子</div> 
</body> 
</html>
Copy after login

2), you need to pay attention to the width. If you want to lay out DIVs in one row and two columns, and avoid the third DIV being side by side, here is what you need to do Set the calculated width (here, set the percentage width to 49%), the sum of the widths of the three DIV boxes is greater than the width of the parent, and the sum of the widths of the two DIVs is less than the width of the parent, you can achieve only 2 DIVs side by side.
3) In the second case, the float values ​​are different.
One is set to float:left; the other is set to float:right.
Complete HTML source code:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>两个DIV并排</title> 
<style> 
.div-c{ float:left;width:49%;border:1px solid #F00} 
.div-d{ float:right;width:49%;border:1px solid #000} 
</style> 
</head> 
<body> 
<div class="div-c">第三个DIV盒子</div> 
<div class="div-d">第四个DIV盒子</div> 
</body> 
</html>
Copy after login

2. Horizontal arrangement of DIVs_CSS How to make multiple div boxes appear side by side, how to make multiple div boxes appear side by side and divs arranged horizontally?
Let’s first set up three div box objects without setting any css styles to see the effect. The code is as follows:
Screenshots of divs that are not displayed in the same row
Three div boxes all occupy one line of display
The default style attribute of the div box itself is to occupy one line. There are usually two ways to solve the problem of divs occupying one line. One is to set Floating, the other is to set the display style. Next, I will introduce how to solve the problem of arranging div box objects side by side horizontally and displaying them side by side through articles + pictures + cases.
1. Use css float for side-by-side display
We can solve the problem of side-by-side display by setting a float attribute on divs. As long as the total width of your side-by-side div boxes is less than or equal to the width of the outermost box, multiple divs can be realized. Objects are displayed side by side.
Add float to realize side-by-side display of div boxes
Add float to realize side-by-side display of multiple divs
Here we set a float for the div. Of course, when actually using it, we need to add the css class to display the div objects in a row. , we will set the floating CSS selector to be displayed in the same row. Avoid other unnecessary settings from being added to the floating style.
2. Use css display to display peers
We can solve the problem by adding display:inline to display div box objects side by side. In order to distinguish other div box objects that do not need to be displayed in a horizontal arrangement, we use a unified bold css class for adjacent div boxes that need to be displayed in the same row, and the css is named ".div-inline".
CSS code is as follows:

.div-inline{ display:inline}
Copy after login


Html code snippet:

<div class="div-inline">第一个div盒子</div> 
<div class="div-inline">第二个盒子</div> 
<div class="div-inline">第三个盒子</div>
Copy after login

3. Summary: Whether it is float floating or display to achieve side-by-side display, you need to For side-by-side display, first of all, the total width must be less than or equal to the width of the object's upper level, so that side-by-side display can be achieved to achieve horizontal layout.

The above is the detailed content of Example code sharing for displaying multiple div boxes side by side in CSS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template