Detailed graphic and text explanation of float in css
This time I will bring you a detailed graphic and text explanation of float in CSS. What are the precautions for the detailed graphic and text explanation of float. The following is a practical case, let’s take a look.
float and margin
Two adjacent floating elements, when the width of the first floating element (whether floating left or right) is 100% , the second floating element will be squeezed below, and it can be brought back to the first line by adding a negative margin-right value (the absolute value is at least equal to its own width).
When writing html code, our usual habit is to write code from left to right according to the UI style, but sometimes the content on the right is more important, so its html structure needs to be placed on top of the content on the left , let it load earlier, for example:
left fixed-width flow layout
<p class="comment"> <!-- 右侧重要内容 --> <p class="content"> <p class="author"> <span class="name">哇哈哈</span> <span class="date">2016-78-55</span> </p> <p class="text">吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!</p> <p class="meta"> <span class="msg-tag">赞</span> <span class="msg-tag">回复</span> </p> </p> <!-- 左侧内容 --> <a href="#" class="avatar"><img src="images/header.jpg" alt="头像"></a> </p>
* {margin:0; padding:0;} li {list-style: none;} a {text-decoration: none;} body {font-family: '微软雅黑';} .wrap { width: 800px; margin: 50px auto; } .content { float: right; margin-left: 100px; } .date { font-size: 14px; color: #666; } .text { margin: 20px 0; } .avatar { float: left; margin-right: -80px; } .avatar img { width: 80px; height: 80px; border-radius: 50%; }
As shown in the above picture , although in the UI, the .content element is to the right of .avatar, we still need to put the .content element in front of the .avatar element in the html structure. At this time, we can set the .content element to right-floating, and then give Set the .avatar element to float left or right, and then add a negative margin-right value to bring it back to the top.
1. The left and right sides are not of varying width
Rendering:
html code:
<p class="comment"> <a href="#" class="avatar"><img src="images/header.jpg" alt="头像"></a> <p class="content"> <p class="author"> <span class="name">哇哈哈</span> <span class="date">2016-78-55</span> </p> <p class="text">吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!吃的再多也不长胖,好愁人啊,怎么能快速长胖呢,在线等,急!</p> <p class="meta"> <span class="msg-tag">赞</span> <span class="msg-tag">回复</span> </p> </p> </p>
Core point:
. The avatar element floats left, and the display attribute of the .content element is set to table-cell. In fact, the .content element here does not necessarily have to set the display to table-cell, as long as it can trigger BFC/haslayout, for example:
float:left/right position:absolute/fixed overflow:hidden/scroll(IE7+) display:inline-block/table-cell(IE8+)
However, because the .content element here is adaptive and cannot have a fixed width, and it contains block-level elements, the overflow attribute can only be set.
css code:
* {margin:0; padding:0;} li {list-style: none;} a {text-decoration: none;} body {font-family: '微软雅黑';} .wrap { width: 800px; margin: 50px auto; } .avatar { float: left; margin-right: 20px; } .avatar img { width: 80px; height: 80px; border-radius: 50%; } .content { display: table-cell; } .date { font-size: 14px; color: #666; } .text { margin: 20px 0; }
2. Fixed-width flow layout on the right side
Rendering:
##html code:
<p class="wrap"> <ul class="list"> <li class="item"> <p class="content-wrap"> <p class="content"> <p class="author"> <a href="#" class="avatar"><img src="images/header.jpg" alt="头像"></a> <span class="name">李荣浩</span> <span class="date">2016-01-22</span> </p> <a href="#" class="title">不将就</a> <p>互相折磨到白头 悲伤坚决不放手 开始纠缠之后 才又被人放大了自由 你的暴烈太温柔 感情又痛又享受 如果我说不吻你不罢休 谁能逼我将就</p> <p class="meta"> <span class="category-tag">歌曲</span> <span class="msg-tag">喜欢·5000</span> </p> </p> </p> <a href="#" class="thumbnail"><img src="images/pic.jpg" alt="图片"></a> </li> </ul> </p>
Core point:
1: When the width of the first floating element is 100%, the second element will automatically wrap next to the first element, then you can add a negative margin value to the second floating element to make it go up 2: There is a wrapping element outside the .content of the first floating element, which is convenient for addingto the .content element The padding-right value leaves a gap between the content on the left and the picture on the right
In fact, the idea of the double-flying wing layout also includes the above two points.css code:
* {margin:0; padding:0;} li {list-style: none;} a {text-decoration: none;} body {font-family: '微软雅黑';} .wrap { width: 800px; margin: 50px auto; } .item { padding-bottom: 15px; border-bottom: 1px solid #ccc; overflow: hidden; } .content { float: left; padding-right: 180px; } .avatar { display: inline-block; width: 32px; height: 32px; border-radius: 50%; vertical-align: middle; overflow: hidden; } .avatar img { width: 100%; height: 100%; } .name { vertical-align: middle; } .date { font-size: 14px; color: #666; vertical-align: middle; } .title { display: block; padding: 10px 0; font-size: 18px; font-weight: bold; line-height: 1.5; color: #333; } .thumbnail { float: left; margin-left: -120px; } .thumbnail img { width: 120px; height: 120px; } .meta { margin-top: 15px; font-size: 14px; } .category-tag { display: inline-block; padding: 0 8px; margin-right: 10px; border: 1px solid #ea6f5a; border-radius: 3px; color: #ea6f5a } .msg-tag { color: #999; }
3. Fixed three-column layout on both sides and adaptive in the middle
There are very few three-column layouts now. If you want to use them, please go directly to Baidu Double Flying Wing Layout or Holy Grail Layout. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:CSS implementation of first-level navigation bar
The above is the detailed content of Detailed graphic and text explanation of float 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.

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

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.

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.

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.

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-
