Home > Article > Web Front-end > Xinxing completely helps you solve the floating problem in CSS_html/css_WEB-ITnose
Floating is a hurdle that must be passed in CSS layout. If you are not familiar with floating, then the layout of CSS is like a castle in the air. When it comes to floating, it is more combined with div, which is a block level Elements, I have introduced this in my previous blog post. If you like my style, you can search for it.
Now let’s get to the point. The so-called floating can be defined by the CSS attribute float. For example, float:left means floating to the left, and float:right means floating to the right. Let’s first look at an example of non-floating. The first is this html file. We will never touch this file. Its content is as follows:
<html><head> <link rel="stylesheet" type="text/css" href="my.css"></head><div id = "demo1">区块1</div><div id = "demo2">区块2</div><div id = "demo3">区块3</div><div id = "demo4">区块4</div></html>Then we write its corresponding my.css file, the content is as follows :
#demo1{ background-color: #933; height: 100px;width:300px; }#demo2{ background-color: #0F0; height:60px;width:200px;}#demo3{ background-color: #F00; height: 140px;width: 80px;}#demo4{ background-color: #CCC; height: 80px;width: 180px;}Then its interface at this time is as follows:
Next we make it in the second div Float right and add the following information, namely:
#demo2{ float: right; background-color: #0F0; height:60px;width:200px;}I only gave the changes in demo2 above, and then we look at the following effect:
Let’s talk about what “ordinary flow” is. This may be a topic that may make novice friends particularly confused. The so-called ordinary flow is just like a book, laid out from top to bottom, left to right, normal HTML elements are all in the ordinary flow, and once an element floats, it is no longer in the ordinary flow. For example, if our block 2 floats right, it will look for the element above it. The previous element is block 1, and block 1 is an element in the ordinary stream, so block 2 is the same as block 1. The bottom is aligned. Ordinary flow determines its up and down position. Floating can only determine its left and right positions. Once the up and down positions are determined, the next step is its left and right positions. Since it is a right float, it is in right. Block three looks for its previous element and finds that its previous element is an element in the standard stream. Therefore, it will automatically align with the low end of block 1 and arrange downwards.
Someone may ask, what if block 2 floats to the left? We change the float attribute of demo2 to left, and we modify the width of block 3 to make it wider. Why don’t you explain? Clearly, the rendering is as follows:
I think the explanation in the above picture is relatively clear, so I won’t explain it in words. Someone may ask, what if the two blocks float together? Let’s first look at the situation of floating right:
Since the width here is wide enough, this layout can be completely accommodated, but what if we narrow the width? ? Look at the diagram below:
Actually, this is pretty easy to understand, so what if we continue to stretch to the left? The following situation will occur:
In fact, as long as the width is wide enough, block 3 is still to the left of block 2 instead of below it, but the width is too low. This is not the case. It reluctantly ran to the bottom of block 2, then started a new row and started arranging from the right.
In fact, the right float will be positioned, so the left float will be very clear. Its effect is as follows:
In fact, the same thing, if the space is too small , then block 3 will also be squeezed into a new line, as follows:
So, it can be summarized as Such a sentence: "The browser first arranges the blocks in the normal flow. They are very simple, just arrange them from top to bottom. For floating elements, it will check which of the nearest elements above it is in the normal flow. elements in it, and treat its bottom as its top, and then continue to arrange it according to the floating rules. Since the layout of the ordinary flow is carried out first, the floating elements will cover the elements in the ordinary flow. "
This section ends here. . . Let’s start with the floating ones again. . . .