search
HomeWeb Front-endCSS TutorialExample 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!

Statement
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
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools