First, take the picture above,
Add a picture to li, and then set the first float, the effect is as above,,,, but,,
After adding width and height to li, it becomes like the above, Why is this, please explain?
If you want to implement two columns, then ul sets the width of two li, and all li float Just
li is a block tag, which is meant to occupy one line. For the first one, you used float:left, which floats to the left to make way for the right position. The second li takes up the remaining space. , the second one does not float, it just occupies one line, so the last one starts on another line. The second case: li does not float, each occupies one line, so they all need to be wrapped.
After float is set, it breaks away from the normal flow, and the second li element moves up
After li sets the width, because #one has the same width as the following li, it is equivalent to the second li block-level element The leftmost part is occupied by #one, that is, the width of the first line is full, and the content in it can only be wrapped and displayed on the second line; when the width is not set, the width of #one is only the width of the a character, and the second The default width of each li is 100% UL width
After the float is set, it breaks away from the normal flow, and the second li element moves up
After the li width is set, because #one and the following The li width is the same, which is equivalent to the leftmost part of the second li block-level element being occupied by #one, that is, the first line of width content is full, and the content in it can only be wrapped and displayed on the second line; and when the width is not set , the width of #one is only the width including the a character, and the width of the second li defaults to 100% UL width
After float is set, it breaks away from the normal flow, and the second li element moves up
After li sets the width, because #one has the same width as the following li , which is equivalent to the leftmost part of the second li block-level element being occupied by #one, that is, the first line of width content is full, and the content in it can only be wrapped and displayed on the second line; and when the width is not set, #one's The width is only the width including the a character. The second li width defaults to 100% UL width
More often than not, there is no need to worry too much about why this is the case, because doing so itself has compatibility issues. For example, the effect of text surrounding images is inconsistent in different browsers. There is no solution. The only way is to avoid similar layouts when doing it
Another example, two block levels If elements want to be displayed in the same row, many people will use the method of setting float first and pushing the second one up. However, this method will produce inexplicable white gaps under IE6/7 when certain conditions are met, and there is no way to solve it. Floats can only be set uniformly
If you really want to know why this is the case, I think you have to understand it from the underlying rendering mechanisms of major browsers. Even if you understand it, you will avoid similar layouts.