How to layout using float? Example of floating layout
How to layout using float? This article will introduce to you how to use float to layout content. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
In the previous article [What is float? Detailed explanation of float attribute ] introduces what the float attribute is, why we need to know about floating, etc. If you are interested, you can refer to it. Let's take a look at how float layouts content through an example of floating layout.
1. Use float to wrap content
html code:
<div class="demo"> <h1 id="浮动-文字环绕">浮动--文字环绕</h1> <div class="box_left">向左浮动</div> <p>此文本位于浮动盒子(.box_left)之后的正常段落中。该段可以看作是浅绿色的盒子。请注意,此轮廓延伸 到浮动div的 后面。这很重要,因为这意味着段落的左侧实际上位于页面的左侧。只是该段落的内容已经向右移动,以“绕过”浮动的DIV。如果你试图操纵与浮点相邻的段落一侧的边距或 填充,则会产生影响。</p> <div class="box_right">向右浮动</div> <p>现在我们添加第二段并用内容充实它。你应该看到,一旦覆盖了div的高度,此段落文本将环绕它,以便文本不再缩进。类似的效果可在html通过设置来实现。</p> <p>float属性的一个复杂因素是Internet Explorer将在浮动的div盒子和后面的文本之间添加一些填充像素。这在标准中没有位置,只是他们“弱化”CSS和HTML的方式。在其他浏览器中,段落文本将与浮动的DIV对接。如果要在所有浏览器中填充,则需要为浮动元素指定边距。</p> </div>
css code:
.demo { width: 520px; overflow: hidden; border: 2px solid paleturquoise; padding: 10px; margin: 100px auto; } .box_left { float: left; margin-right: 15px; width: 150px; height: 100px; border-radius: 5px; background-color: rgb(207, 232, 220); padding: 1em; } .box_right { float: right; margin-left: 15px; width: 150px; height: 100px; border-radius: 5px; background-color: rgb(207, 232, 220); padding: 1em; }
Rendering:
2. Use 'clear' to force the content to float
Common mistakes when using float to layout content I forgot to add the clear. This is an instruction to the element after the floated content to move it down far enough so that it is not affected by the floated content in front of it.
The problem of elements moving up occurs when the "normal" content behind a float element is not enough to clear it.
In this case, you may see subsequent section titles taking effect, or even content overflowing the "Content" section of the page. This is because the floated content is "taken out of the document flow" and therefore does not force any containing boxes to expand downwards on the page.
The best way to force the container to expand around the floated content when no other elements are present is to insert an empty div with clear set to 'both', like this:
<div style="max-width:90%"></div>
Let’s modify the above example:
<div class="demo"> <h1 id="浮动-文字环绕">浮动--文字环绕</h1> <div class="box_left">向左浮动</div> <p>此文本位于浮动盒子(.box_left)之后的正常段落中。该段可以看作是浅绿色的盒子。请注意,此轮廓延伸 到浮动div的 后面。这很重要,因为这意味着段落的左侧实际上位于页面的左侧。只是该段落的内容已经向右移动,以“绕过”浮动的DIV。如果你试图操纵与浮点相邻的段落一侧的边距或 填充,则会产生影响。</p> <div class="box_right">向右浮动</div> <p>现在我们添加第二段并用内容充实它。你应该看到,一旦覆盖了div的高度,此段落文本将环绕它,以便文本不再缩进。类似的效果可在html通过设置来实现。</p> <div style="clear: both;"></div> <p>float属性的一个复杂因素是Internet Explorer将在浮动的div盒子和后面的文本之间添加一些填充像素。这在标准中没有位置,只是他们“弱化”CSS和HTML的方式。在其他浏览器中,段落文本将与浮动的DIV对接。如果要在所有浏览器中填充,则需要为浮动元素指定边距。</p> </div>
Rendering:
3. Use floats to divide the content into columns
html code:
<div class="demo"> <div class="box_1"> <p>第一列</p> <p>float的一个稍微不常见属性但有非常强大的用途,可以将内容分组为列。这可以通过将div向左浮动以形成左侧列,然后添加第二个div来完成,同时向左浮动以位于其旁边。每个DIV必须具有宽度,你可以向第一个DIV添加右边距以分隔列。在许多情况下,这使得TABLE元素的使用变得不必要。</p> </div> <div class="box_2"> <p>第一列</p> <div style="width: 100px;height: 50px;border: 1px solid #000;"></div> <p>你可以看到此框中的内容与第一个段落相邻,并且与第一个示例不同,一旦清除了第一个浮点的高度,它就不会回绕。</p> </div> <div>使用这种布局,你必须记住在最后一个DIV之后添加一个清除,否则后续内容可以出现在两列中和周围。</div> </div>
css code:
.demo { width: 800px; overflow: hidden; border: 2px solid paleturquoise; padding: 10px; margin: 100px auto; } .box_1 { float: left; margin-right: 15px; width: 200px; height: 310px; border-radius: 5px; background-color: rgb(207, 232, 220); padding: 1em; } .box_2 { float: left; margin-left: 15px; width: 200px; height: 310px; border-radius: 5px; background-color: rgb(207, 232, 220); padding: 1em; }
Rendering:
You can also not be limited to setting up two column floats, and could easily have three or four on the page. There are other options. For a three-column layout, you can float the divs left and right and display "normal" content in between.
4. Using floating “tile” images and text
Now let’s look at something that should be simple but is actually very complex. If you have a lot of small DIVs - for example photos in a photo gallery - you can create a simple page by just floating them all to one side.
This works great when the items to be floated all have the same height:
But it causes a lot of problems when the heights are different, because " Higher" items will block those that subsequently float to the left:
NOTE: Reload this page to see other configurations.
#If this is the case, then the safest option is to use a table for layout. In some cases you can avoid this by inserting clear="left" to every nth element, but this may be difficult to maintain when items are added or removed.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of How to layout using float? Example of floating layout. 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

AI Hentai Generator
Generate AI Hentai for free.

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



In Windows 11, the Start menu has been redesigned and features a simplified set of apps arranged in a grid of pages, unlike its predecessor, which had folders, apps, and apps on the Start menu. Group. You can customize the Start menu layout and import and export it to other Windows devices to personalize it to your liking. In this guide, we’ll discuss step-by-step instructions for importing Start Layout to customize the default layout on Windows 11. What is Import-StartLayout in Windows 11? Import Start Layout is a cmdlet used in Windows 10 and earlier versions to import customizations for the Start menu into

Windows 11 brings a lot to the table in terms of user experience, but the iteration isn't entirely error-proof. Users run into issues from time to time, and changes to icon positioning are common. So how to save desktop layout in Windows 11? There are built-in and third-party solutions for this task, whether it's saving the screen resolution of the current window or the arrangement of desktop icons. This becomes even more important for users who have a bunch of icons on their desktop. Read on to learn how to save desktop icon locations in Windows 11. Why doesn't Windows 11 save icon layout positions? Here are the main reasons why Windows 11 does not save desktop icon layout: Changes to display settings: Typically, when you modify display settings, the configured customizations

Guide to solving misaligned WordPress web pages In WordPress website development, sometimes we encounter web page elements that are misaligned. This may be due to screen sizes on different devices, browser compatibility, or improper CSS style settings. To solve this misalignment, we need to carefully analyze the problem, find possible causes, and debug and repair it step by step. This article will share some common WordPress web page misalignment problems and corresponding solutions, and provide specific code examples to help develop

How to create a responsive carousel layout using HTML and CSS Carousels are a common element in modern web design. It can attract the user's attention, display multiple contents or images, and switch automatically. In this article, we will introduce how to create a responsive carousel layout using HTML and CSS. First, we need to create a basic HTML structure and add the required CSS styles. The following is a simple HTML structure: <!DOCTYPEhtml&g

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

For some users, Windows 11 keeps adding new keyboard layouts even if they don't accept or confirm the changes. The WindowsReport software team has replicated this issue and knows how to prevent Windows 11 from adding a new keyboard layout to your PC. Why does Windows 11 add its own keyboard layout? This usually happens when using a non-native language and keyboard combination. For example, if you are using a US display language and a French keyboard layout, Windows 11 may also add an English keyboard. What to do if Windows 11 adds a new keyboard layout you don't want. How to prevent Windows 11 from adding a keyboard layout? 1. Delete unnecessary keyboard layouts and click "Open"

Syntax usage scenarios of contain in CSS In CSS, contain is a useful attribute that specifies whether the content of an element is independent of its external style and layout. It helps developers better control page layout and optimize performance. This article will introduce the syntax usage scenarios of the contain attribute and provide specific code examples. The syntax of the contain attribute is as follows: contain:layout|paint|size|style|'none'|'stric

When we open multiple windows at the same time, win7 has the function of arranging multiple windows in different ways and then displaying them at the same time, which allows us to view the contents of each window more clearly. So how many window arrangements are there in win7? What do they look like? Let’s take a look with the editor. There are several ways to arrange Windows 7 windows: three, namely cascading windows, stacked display windows and side-by-side display windows. When we open multiple windows, we can right-click on an empty space on the taskbar. You can see three window arrangements. 1. Cascading windows: 2. Stacked display windows: 3. Display windows side by side:
