css flex布局 —— 项目属性 flex-basis
flex-basis
属性定义了在分配多余空间之前,项目占据的主轴空间(main size)
。
浏览器根据这个属性,计算主轴是否有多余空间。
语法
.item {
flex-basis: <length> | auto; /* default auto */
}
它可以设为跟 width
或 height
属性一样的值(比如350px),则项目将占据固定空间。
实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container {
display: flex;
margin: 0px auto;
width: 300px;
height: 100px;
background-color: #e6e6e6;
}
.item {
flex-basis: 50px;
height: 50px;
}
.container div:nth-of-type(1) {background-color:coral;}
.container div:nth-of-type(2) {background-color:lightblue;}
.container div:nth-of-type(3) {background-color:khaki;}
.container div:nth-of-type(4) {background-color:pink;}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
页面效果:
如果元素上同时设置了 width
和 flex-basis
,那么 width
的值就会被 flex-basis
覆盖掉,实例:
.item {
width: 30px;
flex-basis: 50px;
height: 50px;
}
页面效果:
可以看到,设置了 width: 30px;
和 flex-basis: 50px;
, flex-basis
属性的优先级比 width
高,所以元素宽度还是 50px
。
把宽度调整大一些,实例:
.item {
flex-basis: 100px;
height: 50px;
}
页面效果:
可以看到,项目元素被压缩显示了,缩小后项目的宽度是75px,4个刚好为300px,等于容器的宽度。
所以,设置了flex-basis
,如果项目的总宽度超过容器的宽度,那么会缩小到容器范围内。
flex-basis
也可以指定其占据父元素的百分比,如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container {
display: flex;
margin: 0px auto;
width: 300px;
height: 100px;
background-color: #e6e6e6;
}
.item {
height: 50px;
}
.container div:nth-of-type(1) { flex-basis: 10%;background-color:coral;}
.container div:nth-of-type(2) { flex-basis: 30%;background-color:lightblue;}
.container div:nth-of-type(3) { flex-basis: 40%;background-color:khaki;}
.container div:nth-of-type(4) { flex-basis: 10%;background-color:pink;}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
页面效果:
flex-basis
,默认值为 auto(根据内容自动调整大小),如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container {
display: flex;
margin: 0px auto;
width: 300px;
height: 100px;
background-color: #e6e6e6;
}
.item {
height: 50px;
}
.container div:nth-of-type(1) { flex-basis: 10%;background-color:coral;}
.container div:nth-of-type(2) { flex-basis: 30%;background-color:lightblue;}
.container div:nth-of-type(3) { flex-basis: 40%;background-color:khaki;}
.container div:nth-of-type(4) { flex-basis: auto;background-color:pink;}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4 auto</div>
</div>
</body>
</html>
页面效果:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgehgij
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01