问题
在进行flex布局时,如果子元素中的内容过宽导致其他同级元素宽度失效 。
//css代码
html,
body {
width: 100%;
height: 100%;
}
.container {
display: flex;
width: 100%;
height: 100%;
}
.left {
width: 350px;
background-color: red;
}
.center {
width: 200px;
background-color: skyblue;
}
.lContent {
width: 350px;
}
.right {
flex: 1;
background-color: pink;
}
.rContent {
width: 2000px;
background-color: rebeccapurple;
}
//布局代码
<div class="container">
<div class="left">
<div class="lCOntent">leftContent</div>
</div>
<div class="center">centerContent</div>
<div class="right">
<div class="rContent">rightContent</div>
</div>
</div>
提示💡
给宽度失效的元素添加flex-grow:0;flex-shrink:0,或flex: 0 0 auto; flex-grow:属性定义项目的放大比例,默认为
0
,即如果存在剩余空间也不放大
flex-shrink:属性定义项目的缩小比例,默认为1
,即如果空间不足该项目缩小。
flex-basis:属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto
,即项目的本来大小
flex:是flex-grow
, flex-shrink
和 flex-basis
的简写,默认值为0 1 auto
。后两个属性可选。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
html,
body {
width: 100%;
height: 100%;
}
.container {
display: flex;
width: 100%;
height: 100%;
}
.left {
width: 350px;
background-color: red;
flex: 0 0 auto;
}
.center {
width: 200px;
background-color: skyblue;
flex: 0 0 auto;
}
.lContent {
width: 350px;
}
.right {
flex: 1;
background-color: pink;
}
.rContent {
width: 2000px;
background-color: rebeccapurple;
}
</style>
</head>
<body>
<div class="container">
<div class="left">
<div class="lCOntent">leftContent</div>
</div>
<div class="center">centerContent</div>
<div class="right">
<div class="rContent">rightContent</div>
</div>
</div>
</body>
</html>