布局
弹性盒子,横向布局:
-
display:flex
// html文件代码 <body> <div class="continer"> <div class="first">num1</div> <div class="second">num2</div> <div>num3</div> <div>num4</div> <div>num5</div> <div>num6</div> <div>num7</div> </div> </body>// css文件代码 div{ height: 100px; width: 500px; background-color: red; display: flex; } div .first{ height: 40px; width: 100px; background-color: blue; } div .second{ height: 40px; width: 100px; background-color: green; }展示效果:

说明
1、不使用display时,div为块级元素,会上下布局
2、想使哪些元素(如.first,.second)横向布局,找父级元素(如.continer)并设置display:flex
3、弹性盒子类似一个松紧带,当子盒子相加超过父盒子时,父级会自动压缩子盒子的大小
弹性盒子的配套属性
// html代码
<body>
<div class="continer">
<div class="first">num1</div>
<div class="second">num2</div>
<div class="third">num3</div>
<div class="fourth">num4</div>
</div>
</body>
-
1、flex-direction: 作用在弹性盒子上,设置弹性盒子内子元素的排列方式:row(从左到右,默认)、column(从上到下)、row-reverse(从右到左)、column-reverse(从下往上)
// css文件代码 div.continer{ height: 200px; width: 500px; background-color: red; display: flex; flex-direction: column-reverse; // 子元素竖向排列,从下往上 } // 子元素仅设置了宽、高和背景颜色的属性,再次不再多写了展示效果:

-
2、flex-wrap 作用在弹性盒子上,设置弹性盒子内子元素换行方式,其值:nowrap(不换行,默认)、wrap(换行)
// 使用方式为: div.continer{ height: 200px; width: 500px; background-color: red; display: flex; flex-wrap: wrap; // 子元素横向排列,失去弹性盒子的作用 // flex-wrap: nowrap; // 效果同文章第一张效果图,子元素不换行,挤在一起 }展示效果:

-
3、order 作用在弹性盒子的子元素上,设置弹性盒子内该自己元素的排列顺序,值越小越靠前
div.continer{ height: 200px; width: 500px; background-color: red; display: flex; } div .first{ height: 40px; width: 200px; background-color: blue; order: 10; // 4个元素中,10最大,排在最后 } div .second{ height: 40px; width: 200px; background-color: green; order: 1; // 4个元素中,1最小,排在最前 } div .third{ height: 40px; width: 200px; background-color: yellow; order: 6; // 4个元素中,6是次大的,排在倒数第二位 } div .fourth{ height: 40px; width: 200px; background-color: pink; order: 3; // 4个元素中,3是次小的,排在第二位 }展示效果:

-
4、flex 作用在弹性盒子的子元素上,设置弹性盒子内该元素的缩放比例。使用时是一个复合属性,由flex-shrink(压缩因子),flex-grow(增长/放大因子),flex-basis(基准宽度)
父盒子宽度为530px,子元素宽度均为80px且不设置flex属性时:

div.continer{ height: 200px; width: 600px; // 600px-50px*4 = 400px background-color: red; display: flex; flex-wrap: wrap; } .continer .first{ height: 20px; width: 50px; background-color: blue; flex: 2; // [600-50px]+50px= } .continer .second{ height: 40px; width: 50px; background-color: green; } .continer .third{ height: 50px; width: 50px; background-color: yellow; flex: 1; } .continer .fourth{ height: 30px; width: 50px; background-color: pink; flex: 2; }效果图

说明
- flex属性是将剩余的宽度按照设置的值分给子元素
- 盒子宽为600px,num2未设置,默认基准宽度为当前设置的宽度不变。
- num1值为2,num3值为1,num4值为2,剩余的550px会被分为(2+1+2)份,每份值为110px
- 所以,width[num1]=110px2=220px;width[num3]=110px1=110px;width[num4]=110px*2=220px;
-
5、justify-content 作用在弹性盒子上,设置的弹性盒子内子元素在排列方向上的分布方式
- flex-start(弹性盒子开始的方向,都挤着放左边)
- flex-end(弹性盒子结束的方向,即都挤着放右边)
- center(居中,即左右留空,都挤在中间)
- space-between(空白在中间的方式)
- space-around(空白在周围,每个元素的左右都有)
div.continer{ height: 200px; width: 600px; background-color: red; display: flex; flex-wrap: wrap; justify-content: flex-end; }
说明
- 子元素仅设置了宽、高和背景颜色的属性
- 使用时子元素不要设置flex属性(flex会把空余的都填充满,不会出现环绕的效果)
-
6、align-items 作用在弹性盒子上,设置弹性盒子内子元素在排列方向的垂直方向的对齐方式:
- flex- start(弹性盒子开始的方向,都挤着放左边)
- flex-end(弹性盒子结束的方向,即都挤着放右边)
- center(居中,即左右留空,都挤在中间)
div.continer{ height: 200px; width: 600px; background-color: red; display: flex; flex-wrap: wrap; align-items: flex-end; } // 子元素仅设置了宽、高和背景颜色的属性
说明
- end时子元素会粘在盒子底部;start时子元素会粘在盒子顶部;center时会居中展示
- 子元素在垂直方向上还会有空隙
-
7、align-content 作用在弹性盒子上,设置弹性盒子行的分布方式(多行展示时):
- flex-start(弹性盒子开始的方向,都挤着放左边)
- flex-end(弹性盒子结束的方向,即都挤着放右边)
- center(居中,即左右留空,都挤在中间)
- space-between(空白在中间的方式)
- space-around(空白在周围,每个元素的左右都有)
div.continer{ height: 200px; width: 600px; background-color: red; display: flex; flex-wrap: wrap; /* align-items: flex-end; */ align-content: flex-end; } .continer .first{ height: 20px; width: 200px; background-color: blue; } .continer .second{ height: 40px; width: 200px; background-color: green; } .continer .third{ height: 50px; width: 200px; background-color: yellow; } .continer .fourth{ height: 30px; width: 200px; background-color: pink; }
说明
- end时子元素会粘在盒子底部;start时子元素会粘在盒子顶部;center时会居中展示
- 子元素高度不一致时,会按照该行中最高的展示,行与行之间没有空隙
备注
1、弹性盒子可以设置横向,也可以设置竖向布局
2、配套属性:没有“display:flex”,即使写上“flex-direction:row”也是不起作用的。flex-direction可以去掉,display不能省掉
3、第一步考虑flex-direction,如果flex-direction:column,则flex-wrap:nowrap也是不起作用的。flex-direction的优先级要高于flex-wrap
4、当父盒子的高度放不下所有的子元素时,flex-wrap:warp也是会换行的,只不过是竖着换行
5、align-items:flex-end 和 align-content:flex-end的区别。items是盒子底部对齐,content是内容对齐方式,效果图如下
定位 - position
// 使用定位主要是为了使用方位属性:left、right、top、bottom以及层属性 z-index
// 上述属性有效前提是position的值不为static
1、static(静态模式,默认)
2、relative(相对模式)
3、absolute(绝对模式)
4、fixed(固定模式)
//html代码
<body>
<div class="box"></div>
<div class="second"></div>
</body>
// css代码
.box{
width: 200px;
height: 200px;
background-color: red;
}
.second{
width: 200px;
height: 200px;
background-color: blue;
}

-
1、纯使用relative [相对模式]
特征:依然保留自己的物理空间,以自己原位置为参考点定位,随着浏览器的滚动而滚动
.box{ width: 200px; height: 200px; background-color: red; position: relative; // 有人也敢下走 left: 100px; // 距离原来位置向左动50px top: 100px; }
注意:
如果是 position:static,元素之间是不会重叠的。relative不关心是否影响其他元素。值可以是负数 -
2、纯使用absolute [绝对模式]
特征:不保留自己的物理空间,以浏览器位置为参考点定位。随着浏览器的滚动而滚动
.box{ width: 200px; height: 200px; background-color: red; position: absolute; right: 50px; bottom: 50px; }

注意:
- 浏览器缩放后,相对浏览器的位置不变。
- 缩放浏览器时,该元素也会与其他元素重叠
- 如果有滚动条,绝对位置是以滚动条在最顶部的时候的浏览器为参考点
-
3、纯使用fixed [固定模式]
特征:不保留自己的物理空间,以浏览器位置为参考点定位,不随着浏览器的滚动而滚动(如,回到顶部等按钮)
注意:
- 浏览器缩放,不影响该元素的位置
- 如果有滚动条,滑动滚动条也不影响该元素的位置
-
4、组合使用
使用:祖先元素使用相对定位,子元素使用绝对定位
特征:子元素不保留自己的物理空间,以使用了相对模式的祖先元素为参考点定位,随着浏览器的滚动而滚动
// html代码 <body> <div class="container"> <div class="box"> <div class="abc"></div> <div class="ef"></div> </div> </div> </body>// css代码 .container{ width: 500px; height: 500px; background-color:red; } .box{ width: 300px; height: 300px; background-color: blue; } .abc{ width: 200px; height: 200px; background-color: green; } .ef{ width: 200px; height: 200px; background-color: yellow; }

-
对祖先(红色)元素设置相对模式。子(绿色和黄色)元素设置绝对定位
// css部分代码 .abc{ width: 200px; height: 200px; background-color: green; position: absolute; right: 10px; bottom: 10px; } .ef{ width: 200px; height: 200px; background-color: yellow; position: absolute; right: 20px; bottom: 20px; }
-
子元素中,想让绿色盖住红色,可用z-index属性来设置(值越大,在z轴方向上越靠前)
.abc{ width: 200px; height: 200px; background-color: green; position: absolute; right: 10px; bottom: 10px; z-index:100; } .ef{ width: 200px; height: 200px; background-color: yellow; position: absolute; right: 20px; bottom: 20px; z-index: 99; }
拓展
-
颜色表示方式
- 1、 颜色单词,如 red、yellow、blue、green、pink等
- 2、#000000(#+6位16进制数)。每两位代表一个颜色,光的三原色(红绿蓝)
- 如果两位的数值一样,可简化。如#ff0011 = # f01
- 3、rgb(0-255,0-255,0-255)
- 每个数的范围是[0,255] ,rgb(0,0,0)代表黑色
- #是用16进制来表示,rgb是使用十进制来表示的
- 4、rgba(0-255, 0-255,0-255,0-1)
- a代表透明度,如果是0的话,该内容完全看不到
- 作用:设置后可以透过看到下层的内容
-
其他属性
-
width: 24%
父级盒子(上图红色框 class=container)宽度为1200px,对于4个子元素(上图绿色框 class=item)来平均分配:
1、固定尺寸,设置每个item的width = 1200/4=300px
2、百分数设置,将container整体想象成1,则4个元素每个为25%
// 说明:这里设置24%是因为元素之间需要留空隙
-
object-fit
使用背景:添加的图片,设置固定 高/宽度后,图片会有变形
- object-fit:cover
作用:响应式改变图片尺寸,设置cover后图片不会变形,内容会有相应的裁剪
- object-fit:contain
作用:响应式改变图片尺寸,设置cover后图片会有压缩
- object-fit:cover
-
cursor: pointer;
作用:鼠标悬浮时,鼠标样式会变为小手状
-
box-shadow: 1px 2px 3px #000;
作用:沿着盒子的轮廓增加阴影效果,效果是黑色
其中 - 1px代表横向偏移量,越大越往右走,越小越往左走;
2px代表纵向偏移量,越大越往下走,越小越往上走;
3px代表模糊度,越小越清晰,越大越模糊

-



4330

被折叠的 条评论
为什么被折叠?



