


Example code sharing for displaying multiple div boxes side by side in CSS
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>
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>
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}
Html code snippet:
<div class="div-inline">第一个div盒子</div> <div class="div-inline">第二个盒子</div> <div class="div-inline">第三个盒子</div>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
