有没有办法动态更改子元素的填充以填充父元素?
Is there a way to dynamically change padding of child element to fill parent element?
我需要子元素在悬停时表现良好,因为(子元素的)红色边框应该与顶部、左侧、底部和右侧的蓝色边框(父元素)在同一行。我可以通过填充来做到这一点吗?有几个结构相同但宽度和高度不同,文本长度不同的框(其中一些是两行或三行)。
如果padding不行,怎么用别的方法达到同样的效果?
代码在这里
* {
box-sizing: border-box;
}
.out {
display: -webkit-box;
display: flex;
-webkit-box-pack: center;
justify-content: center;
width: 200px;
height: 200px;
margin: 2rem auto;
border: 1px solid blue;
}
.out .in {
font-size: 20px;
line-height: 1;
display: inline-block;
text-align: center;
border: 1px solid red;
padding: 20px;
margin: auto;
transition: padding .7s;
}
.out:hover .in {
padding-top: 90px;
padding-bottom: 90px;
padding-left: 40px;
padding-right: 40px;
background: rgba(0,0,0,.2);
}
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend this is a new text
</p>
</div>
<div class="out">
<p class="in">
Hello
</p>
</div>
谢谢
如果你可以添加一个额外的包装器,你可以通过嵌套 flexbox 容器和动画来实现 flex-grow
* {
box-sizing: border-box;
}
.out {
display: flex;
flex-direction: column;
width: 200px;
height: 200px;
margin: 2rem auto;
justify-content: center;
border: 1px solid blue;
}
.out > div {
display: flex;
justify-content: center;
}
.out .in {
display: flex;
font-size: 20px;
justify-content: center;
align-items: center;
text-align: center;
border: 1px solid red;
padding: 20px;
margin: 0;
}
.out * {
flex-grow: 0;
transition: flex-grow .5s;
}
.out:hover * {
flex-grow: 1;
}
.out:hover .in {
background: rgba(0, 0, 0, .2);
}
<div class="out">
<div>
<p class="in">
Hello Friend
</p>
</div>
</div>
<div class="out" style="width:auto">
<div>
<p class="in">
Hello Friend
</p>
</div>
</div>
<div class="out" style="height:auto;min-height:300px;">
<div>
<p class="in">
Hello Friend this is a new text
</p>
</div>
</div>
<div class="out">
<div>
<p class="in">
Hello
</p>
</div>
</div>
您可以在父级悬停时添加子级 100% 的高度和宽度。
* {
box-sizing: border-box;
}
.out {
display: -webkit-box;
display: flex;
-webkit-box-pack: center;
justify-content: center;
width: 200px;
height: 200px;
margin: 2rem auto;
border: 1px solid blue;
}
.out .in {
font-size: 20px;
line-height: 1;
display: inline-block;
text-align: center;
border: 1px solid red;
padding: 20px;
margin: auto;
transition: padding .7s;
}
.out:hover .in {
padding-top: 90px;
padding-bottom: 90px;
padding-left: 40px;
padding-right: 40px;
background: rgba(0,0,0,.2);
width: 100%;
height: 100%;
}
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend this is a new text
</p>
</div>
<div class="out">
<p class="in">
Hello
</p>
</div>
如果您只需要背景动画并且 - 您可以使用类似这样的东西。这里的元素 ::after
与文本分开缩放。
* {
box-sizing: border-box;
}
.out {
display: -webkit-box;
display: inline-flex;
vertical-align: top;
-webkit-box-pack: center;
justify-content: center;
width: 200px;
height: 200px;
margin: 2rem auto;
border: 1px solid blue;
overflow: hidden; /* to hide parent exapnded background */
}
.out .in {
font-size: 20px;
line-height: 1;
display: inline-block;
text-align: center;
padding: 20px;
margin: auto;
position: relative;
}
.out .in::after {
content: '';
background: rgba(0,0,0,.2);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
transform: scale(1);
transition: all .7s;
}
.out:hover .in::after {
transform: scale(5);
}
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend this is a new text
</p>
</div>
<div class="out">
<p class="in">
Hello
</p>
</div>
我需要子元素在悬停时表现良好,因为(子元素的)红色边框应该与顶部、左侧、底部和右侧的蓝色边框(父元素)在同一行。我可以通过填充来做到这一点吗?有几个结构相同但宽度和高度不同,文本长度不同的框(其中一些是两行或三行)。
如果padding不行,怎么用别的方法达到同样的效果?
代码在这里
* {
box-sizing: border-box;
}
.out {
display: -webkit-box;
display: flex;
-webkit-box-pack: center;
justify-content: center;
width: 200px;
height: 200px;
margin: 2rem auto;
border: 1px solid blue;
}
.out .in {
font-size: 20px;
line-height: 1;
display: inline-block;
text-align: center;
border: 1px solid red;
padding: 20px;
margin: auto;
transition: padding .7s;
}
.out:hover .in {
padding-top: 90px;
padding-bottom: 90px;
padding-left: 40px;
padding-right: 40px;
background: rgba(0,0,0,.2);
}
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend this is a new text
</p>
</div>
<div class="out">
<p class="in">
Hello
</p>
</div>
谢谢
如果你可以添加一个额外的包装器,你可以通过嵌套 flexbox 容器和动画来实现 flex-grow
* {
box-sizing: border-box;
}
.out {
display: flex;
flex-direction: column;
width: 200px;
height: 200px;
margin: 2rem auto;
justify-content: center;
border: 1px solid blue;
}
.out > div {
display: flex;
justify-content: center;
}
.out .in {
display: flex;
font-size: 20px;
justify-content: center;
align-items: center;
text-align: center;
border: 1px solid red;
padding: 20px;
margin: 0;
}
.out * {
flex-grow: 0;
transition: flex-grow .5s;
}
.out:hover * {
flex-grow: 1;
}
.out:hover .in {
background: rgba(0, 0, 0, .2);
}
<div class="out">
<div>
<p class="in">
Hello Friend
</p>
</div>
</div>
<div class="out" style="width:auto">
<div>
<p class="in">
Hello Friend
</p>
</div>
</div>
<div class="out" style="height:auto;min-height:300px;">
<div>
<p class="in">
Hello Friend this is a new text
</p>
</div>
</div>
<div class="out">
<div>
<p class="in">
Hello
</p>
</div>
</div>
您可以在父级悬停时添加子级 100% 的高度和宽度。
* {
box-sizing: border-box;
}
.out {
display: -webkit-box;
display: flex;
-webkit-box-pack: center;
justify-content: center;
width: 200px;
height: 200px;
margin: 2rem auto;
border: 1px solid blue;
}
.out .in {
font-size: 20px;
line-height: 1;
display: inline-block;
text-align: center;
border: 1px solid red;
padding: 20px;
margin: auto;
transition: padding .7s;
}
.out:hover .in {
padding-top: 90px;
padding-bottom: 90px;
padding-left: 40px;
padding-right: 40px;
background: rgba(0,0,0,.2);
width: 100%;
height: 100%;
}
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend this is a new text
</p>
</div>
<div class="out">
<p class="in">
Hello
</p>
</div>
如果您只需要背景动画并且 - 您可以使用类似这样的东西。这里的元素 ::after
与文本分开缩放。
* {
box-sizing: border-box;
}
.out {
display: -webkit-box;
display: inline-flex;
vertical-align: top;
-webkit-box-pack: center;
justify-content: center;
width: 200px;
height: 200px;
margin: 2rem auto;
border: 1px solid blue;
overflow: hidden; /* to hide parent exapnded background */
}
.out .in {
font-size: 20px;
line-height: 1;
display: inline-block;
text-align: center;
padding: 20px;
margin: auto;
position: relative;
}
.out .in::after {
content: '';
background: rgba(0,0,0,.2);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
transform: scale(1);
transition: all .7s;
}
.out:hover .in::after {
transform: scale(5);
}
<div class="out">
<p class="in">
Hello Friend
</p>
</div>
<div class="out">
<p class="in">
Hello Friend this is a new text
</p>
</div>
<div class="out">
<p class="in">
Hello
</p>
</div>