有没有一种方法可以动态更改子元素的填充以填充父元素?

时间:2020-06-29 14:11:48

标签: css hover 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>

谢谢

3 个答案:

答案 0 :(得分:1)

如果可以添加一个额外的包装器,则可以通过嵌套flexbox容器并设置动画1. make sure client is installed 2. install & configure unixodbc driver. (http://www.unixodbc.org/) (configure odbc.ini & odbcinst.ini with your username, password,databasename) 3. goto php iterative mode. ex. php -a 4. below are php sample code. php > $connection = odbc_connect( "DRIVER={NetezzaSQL};Server=localhost;Database=db1", "user1", "password"); php > $sql = "SELECT 1 as test"; php > $rs = odbc_exec($connection,$sql); php > odbc_fetch_row($rs); php > echo "\nTest\n\n” . odbc_result($rs,"test") . “\n"; Test 1 php > odbc_close($connection); php > exit;

来实现。

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);
}

答案 1 :(得分:1)

如果您只需要背景动画,并且-您可以使用类似这样的东西。在这里,元素::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>

答案 2 :(得分:0)

您可以在父悬停时将孩子的身高和宽度增加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>