<!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>
.box {
display: flex;
justify-content: flex-end;
width: 500px;
height: 200px;
background-color: aqua;
}
.box1 {
width: 50px;
background-color: blue;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
</div>
</body>
</html>

css3的fit-content属性,fit-content_css

问题:

box1没有给高度,但是在flex布局中中高度被拉伸了,flex-grow, flex-shrink,flex-basis三个对着box1一顿操作也不行。其实只要felx的理论知识够了解(自然是不行的,这三者是控制在当前主轴上的占比策略的)
那换个思路,给box1的高度设置 height: unset,auto,initial又是一顿操作,纹丝不动。

发现:

控制台调试竟然发现height可给的属性这么多,尝试给了个 fit-content,竟然成了

css3的fit-content属性,fit-content_javascript_02

效果

<style>
.box {
display: flex;
justify-content: flex-end;
width: 500px;
height: 200px;
background-color: aqua;
}
.box1 {
width: 50px;
height: fit-content;
background-color: blue;
}
</style>
</head>
<body>
<div class="box">
<div class="box1">
5656
</div>
</div>
</body>

css3的fit-content属性,fit-content_fit-content_03

fit-content解释

fit-content 翻译: 适应内容。可以理解为有内容撑开,在此处就是由内容撑开高度
火狐这样用-moz-fit-content,ie浏览器不支持

另一用处

<style>
.box {
width: 100px;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="box">
dfsddasghfgashfgahsffgdsagfdsjhklsdajflafhdsajfhasfhdasfjksadhfjkashfdjka
</div>
</body>

css3的fit-content属性,fit-content_fit-content_04

<style>
.box {
width: fit-content;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="box">
dfsddasghfgashfgahsffgdsagfdsjhklsdajflafhdsajfhasfhdasfjksadhfjkashfdjka
</div>
</body>

css3的fit-content属性,fit-content_fit-content属性_05

另二用处

<!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>
.box {
width: 500px;
height: 200px;
background-color: aqua;
}
.box1 {
margin: 0 auto;
width: 50px;
}
</style>
</head>
<body>
<div class="box">
<div class="box1">
<img src="" alt="5656" width="50px" height="50px">
</div>
</div>
</body>
</html>

上面这样写是可以水平居中的,常用手段

<style>
.box {
width: 500px;
height: 200px;
background-color: aqua;
}
.box1 {
margin: 0 auto;
width: fit-content
}
</style>
</head>
<body>
<div class="box">
<div class="box1">
<img src="" alt="5656" width="50px" height="50px">
</div>
</div>
</body>

但是如上,用fit-content也可以进行居中,看了下此时盒子模型,没看出个所以然,和上述表现完全一致,且宽度不用写死

css3的fit-content属性,fit-content_javascript_06


css3的fit-content属性,fit-content_fit-content_07