💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
>### float ~~~ 目的:为了让元素并排显示 ~~~ #### 1.子元素float,父元素的高度会坍塌 ![](https://box.kancloud.cn/55ba16b28efc5bcfc4c200f5e61b2a50_500x500.png) ![](https://box.kancloud.cn/b29da7867212e417c5bce2f117ebd8ac_465x455.png) #### 2.如何清除float (1)给下面的兄弟元素给clear:both; (2)给父级加overflow:hidden; ~~~ .parent{ width:200px; background: red; overflow: hidden; } .child{ width:50px; height:50px; background: blue; float: left; } ~~~ ~~~ <style> /* 如何让下面元素不受上面浮动元素的影响 1.clear:both; 2.给float元素父元素设置overflow:hidden */ .parent>div { width: 100px; height: 100px; float: left; } .one { background: red; } .two { background: blue; } .three { background: pink; } .four { width: 200px; height: 200px; background: gray; /* 清除float clear: both; */ } .parent{ overflow: hidden; } </style> </head> <body> <!-- float目的 让元素并排显示 --> <div class="parent"> <div class="one"></div> <div class="two"></div> <div class="three"></div> </div> <p class="four"></p> </body> ~~~ demo实现导航 ~~~ <style> *{margin:0;padding:0} .nav{ overflow: hidden; line-height: 50px; background: pink; } a{ display: block; float: left; text-decoration: none; width: 100px; text-align: center; color: #fff; } a:hover{ color: green; border-bottom: 2px solid blue; } </style> </head> <body> <div class="nav"> <a href="#">手机</a> <a href="#">平板</a> <a href="#">电脑</a> </div> </body> </html> ~~~ ![](https://box.kancloud.cn/19ed517f397eb3cc1ae38d350dc93baf_354x56.png) 内联块也可以实现导航,但是有缺点,不推荐使用 ~~~ <style> *{margin:0;padding:0} .nav{ line-height: 50px; background: pink; } a{ display:inline; text-decoration: none; width: 100px; text-align: center; color: #fff; } a:hover{ color: green; border-bottom: 2px solid blue; } </style> </head> <body> <div class="nav"> <a href="#">手机</a> <a href="#">平板</a> <a href="#">电脑</a> </div> </body> ~~~