【问题标题】:How to hover background color size from 0 to 100% [duplicate]如何将背景颜色大小从 0 悬停到 100% [重复]
【发布时间】:2021-10-18 07:24:23
【问题描述】:

我想将:hover 伪类添加到h1。首先,background-color 必须等于 = 0%(我添加了一种颜色进行测试,但最后背景颜色将是白色)然后当悬停到 h1 时,background-color 必须是灰色且大小为100% 100%。我不能那样做。 :')

.back{
    border: 1px solid black;
    width: 250px;
    background-color: tomato;
    height: 100px;
    background-size: 0% 100%;
    background-repeat: no-repeat;
    transition: 1s all;
    
}
.back:hover{
  
    background-color: gray;
    background-size: 100% 100%;
    

}
 <h1 class="back"></h1>

【问题讨论】:

  • background-size 属性指定背景图像的大小。
  • 不,我记得!有人这样做是为了输入。但我找不到... :(
  • 请更好地描述你想要什么。正如上面提到的@TechySharnav,background-size 指定背景图像的大小。要更改框大小吗?
  • 好的。 stackoverflow.com/questions/7861648/… 我想为背景颜色做这个。 ^^

标签: html css


【解决方案1】:

您必须使用“div”而不是 h1。另外你应该将 body 的高度设置为 100%,以便在悬停时,你的 div 会得到全屏

HTML, body{
    height:100%
}

.back{
    border: 1px solid black;
    width: 250px;
    background-color: tomato;
    height: 100px;
    background-repeat: no-repeat;
    transition: 1s all;
    
}
.back:hover{
  
    background-color: gray;
    width: 100%;
    height: 100%;

    

}
 <div class="back"></div>

【讨论】:

    【解决方案2】:

    从你的评论看来你想要这样的东西,如果我错了,请纠正我。

    .back{
        border: 1px solid black;
        width: 250px;
        background-color: white;
        height: 100px;
        width: 0%;
        background-repeat: no-repeat;
        transition: 1s all;
        
    }
    .back:hover{
      
        background-color: gray;
        width: 250px;
        
    
    }
    <h1 class="back">Header</h1>

    我在您的 h1 标签中添加了一个文本以便于使用,因为我想您确实有一些文本。

    【讨论】:

    • 是的!我想要的是这样的。但我记得边框和宽度是一样的......无论如何,它也很有用。非常感谢!
    猜你喜欢
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2014-09-09
    • 2014-02-27
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多