CSS在悬停时通过转换扩展边界底部

时间:2017-10-03 13:35:31

标签: css css3

我正在努力让h1标签的以下转换适用于div的border-bottom。这是我为h1标签设置的内容:

    Alias /phpmyadmin "D:/xampp/phpMyAdmin/"
    <Directory "D:/xampp/phpMyAdmin">
        AllowOverride AuthConfig
        Require local
        allow from ip 10.10.10.10 ( not working )
        Require 101.10.10.110 ( still not working
        ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
    </Directory>
h1 {
  text-align: center;
  color: #666;
  position: fixed;
  display: inline-block;
 
}

h1:after {
  position: absolute;
  left: 50%;
	content: '';
	height: 5px;
  background: blue;
  transition: all 0.5s linear;
  width: 0;
  bottom: 0;  
}

h1:hover:after {
	width: 100%;
  margin-left: -135px;
}

如何在div边界底部进行此操作?这就是我想出来的,我明白为什么它不起作用(除此之外'宽度'不适用于边框),但我无法弄清楚如何解决它。

<div style="height: 100px; width: 300px">
  <h1>CSS IS AWESOME</h1>
</div>
h1 {
  text-align: center;
  color: #666;
  position: fixed;
  display: inline-block;
}

div:after {
  border-bottom: 5px solid red;
  left: 50%;
	content: '';
  transition: all 0.5s linear;
  width: 0;
  bottom: 0;
}

div:hover:after {
	width: 300px;
  margin-left: -135px;
}

提前致谢。

3 个答案:

答案 0 :(得分:1)

试试这个:

&#13;
&#13;
div {
  position: relative;
  display: inline-block;
}

h1 {
  color: #666;
  margin: 0;
}

div::after {
  border-bottom: 5px solid red;
  position: absolute;
  left: 50%;
  content: ' ';
  transform: translateX(-50%);
  transition: width 0.5s linear;
  width: 0;
  height: 0;
  bottom: 0;
}

div:hover::after {
  width: 100%
}
&#13;
<div style="height: 100px; width: 300px">
  <h1>CSS IS AWESOME</h1>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用此

div{
position:fixed;}

并删除margin-left它将起作用

h1 {
  text-align: center;
  color: #666;
  position: fixed;
  display: inline-block;
}

div{
position:fixed;}

div:after {
  position: absolute;
  left: 50%;
	content: '';
	height: 5px;
  background: blue;
  transition: all 0.5s linear;
  width: 0;
  bottom: 0;  
}

div:hover:after {
	width: 100%;
  left:0;
}
<div style="height: 100px; width: 300px">
  <h1>CSS IS AWESOME</h1>
</div>

答案 2 :(得分:1)

试试这个:

div {
  position: relative;<---Added
}

div:after {
  position: absolute;<----Added
  //more code....
}

&#13;
&#13;
h1 {
  text-align: center;
  color: #666;
  position: fixed;
  display: inline-block;
}

div {
  position: relative;
}

div:after {
  border-bottom: 5px solid red;
  left: 50%;
  content: '';
  transition: all 0.5s linear;
  width: 0;
  bottom: 0;
  position: absolute;
}

div:hover:after{
  width: 300px;
  margin-left: -155px;
}
&#13;
<div style="height: 100px; width: 300px">
  <h1>CSS IS AWESOME</h1>
</div> 
&#13;
&#13;
&#13;