gpt4 book ai didi

css - 填充使元素变大而不是清除元素周围的空间

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:39 24 4
gpt4 key购买 nike

这是我的代码

    <style>
body{
width:100%;
height:100%;
overflow:hidden;
}

.mouse{
background-color: rgb(128,64,0);
border-radius:100px;
height:100%;
width:10%;
position:absolute;
top:100%;
box-sizing:border-box;
}

.left_ear{
background-color: red;
border-radius:100px;
position:absolute;
float:left;
width:30%;
padding:100%;

}

</style>

</head>

<body>
<div class="mouse" id="mouse1">
<div class="left_ear"></div>
<div class="right_ear"></div>
</div>
<!--<div class="mouse" id="mouse2">
<div class="left_ear"></div>
<div class="right_ear"></div>
</div>
<div class="mouse" id="mouse3">
<div class="left_ear"></div>
<div class="right_ear"></div>
</div>-->
</body>

</html>

这是我得到的: Padding: 100%

Padding: 30%

那么填充是如何工作的呢?我的意思是我知道它会增加元素周围区域的基础知识,但绝不会增加元素的大小。

最佳答案

假设您有这样的元素:

.parent{
padding:20px;
background:blue;
display:inline-block;
}
.parent div{
padding:20px;
height:80px;
width:80px;
}
.child1{
background:green;
box-sizing:border-box;
}
.child2{
background:purple;
}
<div class="parent">
<div class="child1"></div><br>
<div class="child2"></div>
</div>

如您所见,第一个 child 比第二个 child 小,尽管它们具有完全相同的大小和填充

在第一个 child 身上,我使用了属性 box-sizing:border-box。这是做什么的,它包括元素宽度/高度中的填充。

如果您不这样做,填充将添加到元素宽度。

在您的示例中,您在父元素上得到了 box-sizing:border-box 。因此,如果您向该元素添加边距,它将保持相同的大小(除非您设置的填充比元素大)。在您的 child 身上,您没有此属性,因此在使用填充时它们的大小会增加。

关于css - 填充使元素变大而不是清除元素周围的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com