当前位置: 首页 > news >正文

Day21浮动

1.浮动的基本使用

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>浮动的基本使用</title><style>/* 特点:顶对齐,具备行内块显示模式,即可以在同一行一起显示 */.one{width: 100px;height: 100px;background-color: red;float: left;}.two{width: 200px;height: 200px;background-color: orange;/* float: left; *//* float: right; *//* 此时由于只有.one进行了浮动,而浮动会导致该标签脱标 *//* 即浏览器不在会识别他为标签,后面的标签可能会抢占原本浮动标签的位置 *//* 导致标签重叠错乱 */}</style>
</head>
<body><div class="one"></div><div class="two"></div>
</body>
</html>

image

2.产品区域布局

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>产品区域</title><style>*{margin: 0;padding: 0;}li{list-style: none;}.product{margin: 50px auto;width: 1226px;height: 628px;background-color: pink;}.left{float: left;width: 234px;height: 628px;background-color: skyblue;}.right{float: right;width: 978px;height: 628px;background-color: brown;}.right li{float: left;margin-right: 14px;margin-bottom: 14px;width: 234px;height: 300px;background-color: orange;}/* 第四个和第八个li去掉右边的margin */.right li:nth-child(4n){margin-right: 0;}/* 如果父级的宽度不够,即使加上了浮动,浮动的标签也可能因为位置不够而被挤到下一行 */</style>
</head>
<body><div class="product"><div class="left"></div><div class="right"><ul><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul></div></div>
</body>
</html>

image

3.清除浮动异常
image
原理就是标签添加浮动属性之后会导致脱标,即浏览器不在为该标签安排位置,所以下一个标签会挤占浮动标签的位置,造成页面异常
例如:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>清除浮动异常</title><style>.top{margin: 10px auto;width: 1200px;/* 注释掉父级的高度,子集无法撑开父级高度,会导致页面错乱 *//* height: 300px; */background-color: pink;}.right{float: left;width: 200px;height: 300px;background-color: skyblue;}.left{float: right;width: 950px;height: 300px;background-color: orange;}.bottom{height: 100px;background-color: brown;}</style>
</head>
<body><div class="top"><div class="left"></div><div class="right"></div></div><div class="bottom"></div>
</body>
</html>

image

解决浮动异常的四种方法
image
额外标签法,在出现异常的标签的基础上再添加一个标签

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>额外标签法</title><style>.top{margin: 10px auto;width: 1200px;/* 注释掉父级的高度,子集无法撑开父级高度,会导致页面错乱 *//* height: 300px; */background-color: pink;}.right{float: left;width: 200px;height: 300px;background-color: skyblue;}.left{float: right;width: 950px;height: 300px;background-color: orange;}.bottom{height: 100px;background-color: brown;}.clearfix{clear: both;}</style>
</head>
<body><div class="top"><div class="left"></div><div class="right"></div><div class="clearfix"></div></div><div class="bottom"></div>
</body>
</html>

image
单伪元素法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>单伪元素法</title><style>.top{margin: 10px auto;width: 1200px;/* 注释掉父级的高度,子集无法撑开父级高度,会导致页面错乱 *//* height: 300px; */background-color: pink;}.right{float: left;width: 200px;height: 300px;background-color: skyblue;}.left{float: right;width: 950px;height: 300px;background-color: orange;}.bottom{height: 100px;background-color: brown;}.clearfix::after{content: "";display: block;clear: both;}</style>
</head>
<body><div class="top clearfix"><div class="left"></div><div class="right"></div></div><div class="bottom"></div>
</body>
</html>

双伪元素法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>双伪元素法</title><style>.top{margin: 10px auto;width: 1200px;/* 注释掉父级的高度,子集无法撑开父级高度,会导致页面错乱 *//* height: 300px; */background-color: pink;}.right{float: left;width: 200px;height: 300px;background-color: skyblue;}.left{float: right;width: 950px;height: 300px;background-color: orange;}.bottom{height: 100px;background-color: brown;}.clearfix::before,.clearfix::after {content: "";display: table;}.clearfix::after {clear: both;}</style>
</head>
<body><div class="top clearfix"><div class="left"></div><div class="right"></div></div><div class="bottom"></div>
</body>
</html>
http://www.jsqmd.com/news/41364/

相关文章:

  • Spring AI Alibaba 项目源码学习(七)-Agent、BaseAgent、ReactAgent 分析
  • AtCoder Beginner Contest 432 ABCDEG 题目解析
  • fireworks
  • KEYDIY KD ZB28-3 Universal Hyundai Smart Remote Key (5pcs/lot) – Reliable Replacement
  • Yanhua Mini ACDP-2 A303 Volvo 2022+ IMMO License for ACDP-2 Module20
  • 西电TIC带鱼杯新生训练赛复盘
  • 20251115 - 从零到1详细剖析STM32的CAN架构【以STM32F407为例】
  • 2025.11.15 测试
  • 鸿蒙应用审核被拒?常见原因与避坑指南来了
  • C++篇(13)计算器实现 - 指南
  • 20232306 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • ABC432E sol
  • 完整教程:linux离线环境局域网远程ssh连接vscode
  • 01命题逻辑的基本概念
  • 鲜花:记梦4
  • 第26天(简单题中等题 二分查找、贪心算法)
  • invalid literal for int() with base 10: abc中的base 10是什么意思? 另外它是怎么知道abc的?
  • byd秘钥 - MKT
  • NSubstitute之Substitute.ForT
  • DAY1 JAVA PreLearning
  • 【服务器】服务器被攻击植入了挖矿病毒,CPU一直占用100%,@monthly /root/.cfg/./dealer病毒清除 - 实践
  • 动态规划实践:数字三角形问题分析
  • 第4章 AI项目管理新范式:从交付功能到交付价值
  • 牛客101:链表 - 教程
  • LNCPC 2025 游寄
  • 第3章 传统项目管理在AI中的局限
  • Python 异常处理全面详解(附丰富实例)
  • IServiceCollection和IServiceProvider
  • multisim 13 Problem: Accessing the database解决办法
  • 完整教程:Redis 事务机制:Pipeline、ACID、Lua脚本