块元素独占一行 div
行内元素可以一行显示多个 span
浮动(了解,现在基本不用)
让块元素水平排列
属性名:float
属性值:left:左对齐 right:右对齐
<title>浮动-基本使用</title> <style> /* 特点:顶对齐,具备行内块特点,浮动的盒子会脱标,不占标准流的位置 */ .one { width: 100px; height: 100px; background-color: brown; float: left; } .two { width: 200px; height: 200px; background-color: orange; /* float: left; */ float: right; } </style> </head> <body> <div class="one">one</div> <div class="two">two</div> </body>
<title>浮动-产品区域布局</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } li { list-style: none; } .produce { /* margin实现版心居中 */ 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> <!-- 版心:左右结构,右面:8个产品->8个li --> <div class="produce"> <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>
清除浮动
浮动元素会脱标,若父级没有高度,子级无法撑开父级高度(可能导致页面布局错乱)
解决方法:清除浮动(清除浮动带来的影响)
方法一:额外标签法
在父元素内容的最后添加一个块级元素,设置css属性clear:both
一般的用来清除浮动的选择器都叫 .clearfix
<title>清除浮动-额外标签法</title> <style> .top { margin: 10px auto; width: 1200px; /* height: 300px; */ background-color: pink; } .left { float: left; width: 200px; height: 300px; background-color: skyblue; } .right { float: right; width: 950px; height: 300px; background-color: orange; } .bottom { height: 100px; background-color: brown; } /*一般的用来清除浮动的选择器都叫 .clearfix*/ .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>
方法二:单伪元素法
伪元素必须要配合content
在父元素内容的最后添加一个块级元素,设置css属性clear:both
<title>清除浮动-单伪元素法</title> <style> .top { margin: 10px auto; width: 1200px; /* height: 300px; */ background-color: pink; } .left { float: left; width: 200px; height: 300px; background-color: skyblue; } .right { 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 class="clearfix"></div> --> </div> <div class="bottom"></div> </body>
方法三:双伪元素(推荐)
/* 双伪元素法 */ /* before 解决外边距塌陷问题 */ .clearfix::before, .clearfix::after { content: ""; display: table; } .clearfix::after { clear: both; }
<title>清除浮动—双伪元素</title> <style> .top { margin: 10px auto; width: 1200px; /* height: 300px; */ background-color: pink; } .left { float: left; width: 200px; height: 300px; background-color: skyblue; } .right { float: right; width: 950px; height: 300px; background-color: orange; } .bottom { height: 100px; background-color: brown; } /* 双伪元素法 */ /* before 解决外边距塌陷问题 */ .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 class="clearfix"></div> --> </div> <div class="bottom"></div> </body>
方法四:overflow(溢出隐藏)
给父级标签添加css'属性 overflow:hidden
<title>清除浮动</title> <style> .top { margin: 10px auto; width: 1200px; /* height: 300px; */ background-color: pink; overflow: hidden; } .left { float: left; width: 200px; height: 300px; background-color: skyblue; } .right { 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 class="clearfix"></div> --> </div> <div class="bottom"></div> </body>
Flex布局(重点,企业)
Flex模型不会产生浮动布局中的脱标现象。
描述 | 属性名 |
---|---|
创建flex容器 | display:flex |
主轴对齐方式 | justify-content |
侧轴对齐方式 | align-items |
某个弹性盒子侧轴对齐方式 | align-self |
修改主轴方向 | flex-direction |
弹性伸缩比 | flex |
弹性盒子换行 | flex-wrap |
行对齐方式 | align-content |
Flex-组成
创建flex容器 display:flex
设置方式:给父级标签设置display:flex,子元素可以自动挤压或拉伸
组成部分:
弹性容器
弹性盒子
主轴:默认在水平方向
侧轴/交叉轴:默认在垂直方向
<title>Flex</title> <style> .box { /* 变成flex模型 */ display: flex; height: 300px; border: 1px solid #000; } /* 弹性盒子:沿着主轴方向排列 */ .box div { /* 浮动 */ /* float: left; margin: 50px; */ width: 200px; height: 100px; background-color: pink ; } </style> </head> <body> <div class="box"> <div>1</div> <div>2</div> <div>3</div> </div> </body>
弹性盒子的主轴对齐方式 justify-content
属性值 | 效果 |
---|---|
flex-start | 默认值,弹性盒子从起点开始依次排列 |
flex-end(不常用) | 弹性盒子从终点开始依次排列 |
center | 弹性盒子沿主轴居中排列 |
space-between | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子之间 |
space-around | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子两侧 |
space-evenly | 弹性盒子沿主轴均匀排列,弹性盒子与容器之间间距相等 |
<title>flex-主轴对齐方式</title> <style> .box { display: flex; /* flex-start是默认值,弹性盒子从起点开始依次排列 */ /* justify-content: flex-start; */ /* 弹性盒子从终点开始依次排列,不常用 */ /* justify-content: flex-end; */ /* 弹性盒子沿主轴居中排列 */ /* justify-content: center; */ /* 父级剩余的尺寸分配成间距 */ /* space-between 弹性盒子之间的间距相等,两侧没有 */ /* justify-content: space-between; */ /* space-around 间距出现在弹性盒子两侧,弹性盒子之间的间距是两端间距的2倍 */ /* justify-content: space-around; */ /* 不论是弹性盒子两侧的间距,还是弹性盒子之间的间距都相等 */ justify-content: space-evenly; height: 300px; border: 1px solid #000; } .box div { width: 200px; height: 100px; background-color: pink; } </style> </head> <body> <div class="box"> <div>1</div> <div>2</div> <div>3</div> </div> </body>
弹性盒子的侧轴对齐方式
属性名:
-
align-items :当前弹性容器内所有弹性盒子的侧轴对齐方式(给弹性容器设置)
-
align-self :单独控制某个弹性盒子的侧轴对齐方式 (给弹性盒子设置)
属性值 | 效果 |
---|---|
stretch | 弹性盒子沿着侧轴线被拉伸至铺满容器(弹性盒子没有设置侧轴方向尺寸则默认拉伸) |
center | 弹性盒子沿侧轴居中排列 |
flex-start | 弹性盒子从起点开始依次排列 |
flex-end | 弹性盒子从终点开始依次排列 |
<title>flex-侧轴对齐方式</title> <style> .box { display: flex; /* 若弹性盒子在侧轴方向没有尺寸才能拉伸 */ /* align-items: stretch; */ /* align-items: center; */ /* align-items: flex-start; */ align-items: flex-end; height: 300px; border: 1px solid #000; } /* 找出第二个div,侧轴居中对齐 */ .box div:nth-child(2n) { align-self: center; } .box div { width: 200px; height: 100px; background-color: pink; } </style> </head> <body> <div class="box"> <div>1</div> <div>2</div> <div>3</div> </div> </body>
修改主轴方向 flex-direction
主轴默认在水平方向,侧轴默认在垂直方向
属性值 | 效果 |
---|---|
row | 水平方向,从左向右(默认) |
column(只记这个,其他基本不用) | 垂直方向,从上向下 |
row-reverse | 水平方向,从右向左 |
column-reverse | 垂直方向,从下向上 |
<title>flex-修改主轴方向</title> <style> .box { display: flex; /* 修改主轴方向 垂直方向,侧轴自动变换到水平方向 */ flex-direction: column; /* 主轴在垂直方向,视觉效果:垂直居中 */ justify-content: center; /* 侧轴在水平方向,视觉效果,水平居中 */ align-items: center; width: 150px; height: 120px ; background-color: pink ; } img { width: 32px; height: 32px; } </style> </head> <body> <div class="box"> <img src="./images/1.png" alt=""> <span>媒体</span> </div> </body>
弹性伸缩比 flex
控制弹性盒子的主轴方向的尺寸
属性值 :整数数字,表示占用父级剩余尺寸的份数
<title>flex-弹性伸缩比</title> <style> /* 默认情况下,主轴方向尺寸是靠内容撑开,侧轴默认拉伸 */ .box { display: flex; height: 300px; border: 1px solid #000; } .box div { /* height: 100px; */ background-color: pink; } .box div:nth-child(1) { width: 200px; } .box div:nth-child(2){ flex: 1; } .box div:nth-child(3){ flex: 2; } </style> </head> <body> <div class="box"> <div>1</div> <div>2</div> <div>3</div> </div> </body>
弹性盒子换行flex-wrap
弹性盒子可以自动挤压或拉伸,默认情况下,所有弹性盒子都在一行显示
属性值: wrap:换行 nowrap:不换行
写在父级标签中
flex-wrap: wrap; flex-wrap: nowrap;
行对齐方式 align-content
属性值 | 效果 |
---|---|
flex-start | 默认值,弹性盒子从起点开始依次排列 |
flex-end(不常用) | 弹性盒子从终点开始依次排列 |
center | 弹性盒子沿主轴居中排列 |
space-between | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子之间 |
space-around | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子两侧 |
space-evenly | 弹性盒子沿主轴均匀排列,弹性盒子与容器之间间距相等 |
写在父级标签中
<title>flex-行对齐.html</title> <style> .box { display: flex; flex-wrap: wrap; /* flex-wrap: nowrap; */ /* align-content: flex-start; */ align-content: space-evenly; height: 300px; border: 1px solid #000; } .box div { width: 200px; height: 100px; background-color: pink; } </style> </head> <body> <div class="box"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> </div> </body>