1. Problem
2. Solution 1. clear: both Add an element with the attribute clear:both; after the last floated element. <style> div.parent>div.fl{ float: left; width: 200px; height: 200px; margin-right: 20px; border: 1px solid red; } .clear{ clear: both; } </style> <div class="parent"> <div class="fl">1</div> <div class="fl">2</div> <div class="fl">3</div> <div class="fl">4</div> <div class="clear">5</div> </div> Add the :after pseudo-element with the clear:both; attribute to the parent element. <style> div.parent>div.fl{ float: left; width: 200px; height: 200px; margin-right: 20px; border: 1px solid red; } .clear:after{ content: ''; display: block; clear: both; } </style> <div class="parent clear"> <div class="fl">1</div> <div class="fl">2</div> <div class="fl">3</div> <div class="fl">4</div> </div> Note: Pseudo-elements are inline by default, so when using pseudo-elements, you must set the attribute display: block;. 2. overflow:auto / overflow:hidden <style> div.parent{ overflow:auto; /*overflow: hidden; also works*/ } div.parent>div.fl{ float: left; width: 200px; height: 200px; margin-right: 20px; border: 1px solid red; } </style> <div class="parent"> <div class="fl">1</div> <div class="fl">2</div> <div class="fl">3</div> <div class="fl">4</div> </div> 3. Floating parent elements <style> div.parent{ float: left; } div.parent>div.fl{ float: left; width: 200px; height: 200px; margin-right: 20px; border: 1px solid red; } </style> <div class="parent"> <div class="fl">1</div> <div class="fl">2</div> <div class="fl">3</div> <div class="fl">4</div> </div> Note: This method is generally not used, as it will cause layout problems for the parent element. This concludes this article on the problems and solutions caused by floating elements. For more information about problems caused by floating elements, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. We hope that everyone will support 123WORDPRESS.COM in the future! |
<<: Three BOM objects in JavaScript
>>: VMware Workstation virtual machine installation operation method
Table of contents TypeScript environment construc...
Today, when I was building a redis environment in...
What is a covering index? Creating an index that ...
Install the latest stable version of MySQL on Lin...
This article shares the specific method of instal...
virtualenv is a tool for creating isolated Python...
The code looks like this: SELECT @i:=@i+1 rowNum,...
background Today, I was browsing CodePen and saw ...
Today, when I was installing CentOS6.2, I couldn&...
Ping www.baidu.com unknown domain name Modify the...
I started learning MySQL recently. The installati...
Table of contents How to install and configure To...
Preface For a long time, the application and lear...
MySQL 8 Windows version zip installation steps (d...
1. Introduction As we all know, in the applicatio...