CSS中如果实现元素浮动和清除浮动,看这篇文章就足够了

时间:2019-12-02 10:18:01   收藏:0   阅读:69
原文:CSS中如果实现元素浮动和清除浮动,看这篇文章就足够了

浮动基本介绍#

属性值 描述
left 设置元素向左浮动。
right 设置元素向右浮动。

右浮动实践#

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:right; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

左浮动实践#

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

技术分享图片

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>

技术分享图片

将行内元素设置浮动#

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <span class="box1">微笑是最初的信仰1</span> <span class="box2">微笑是最初的信仰2</span> <span class="box3">微笑是最初的信仰3</span> </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <span class="box1">微笑是最初的信仰1</span> <span class="box2">微笑是最初的信仰2</span> <span class="box3">微笑是最初的信仰</span> </div> </body> </html>

技术分享图片


设置浮动总结#


为什么要清除浮动呢?#

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>清除浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> <h1>清除浮动</h1> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> <h1>清除浮动</h1> </body> </html>

技术分享图片

清除浮动有3种方式#

第一种方式#

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>清除浮动</title> <style> .box{ width: 600px; height: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> <h1>清除浮动</h1> </body> </html>

技术分享图片

第二种方式#

属性值 描述
left 清除左侧浮动元素。
right 清除右侧浮动元素。
both 清除左右侧浮动元素。
Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>清除浮动</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } .clear{ clear: both; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="clear"></div> </div> <h1>清除浮动</h1> </body> </html>

技术分享图片

第三种方式#

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>溢出内容进行隐藏</title> <style> div{ width: 100px; height: 50px; border: 1px solid #000; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>溢出内容进行隐藏</title> <style> div{ width: 100px; height: 50px; border: 1px solid #000; overflow: hidden; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>清除浮动</title> <style> ul{ list-style: none; } ul li{ float: left; border: 1px solid red; } </style> </head> <body> <ul> <li>微笑是最初的信仰1</li> <li>微笑是最初的信仰2</li> <li>微笑是最初的信仰3</li> <li>微笑是最初的信仰4</li> <li>微笑是最初的信仰5</li> <li>微笑是最初的信仰6</li> <li>微笑是最初的信仰7</li> <li>微笑是最初的信仰8</li> </ul> </body> </html>

技术分享图片

Copy
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>清除浮动</title> <style> ul{ list-style: none; overflow: hidden; } ul li{ float: left; border: 1px solid red; } </style> </head> <body> <ul> <li>微笑是最初的信仰1</li> <li>微笑是最初的信仰2</li> <li>微笑是最初的信仰3</li> <li>微笑是最初的信仰4</li> <li>微笑是最初的信仰5</li> <li>微笑是最初的信仰6</li> <li>微笑是最初的信仰7</li> <li>微笑是最初的信仰8</li> </ul> </body> </html>

技术分享图片

原文:https://www.cnblogs.com/lonelyxmas/p/11968747.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!