【问题标题】:I m trying to make a sliding underline with css but its not working for some reason我正在尝试使用 css 制作滑动下划线,但由于某种原因它无法正常工作
【发布时间】:2020-07-13 19:18:08
【问题描述】:

它是我正在尝试构建的登录和登录表单,当我将鼠标悬停在按钮上时,它会立即进行注册。我希望将鼠标悬停在注册按钮上后,下划线会立即移动。请告诉我为什么下划线不滑动。

    #form{
        width: 300px;
        margin: auto;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
    
    .butts{
        display: flex;
        justify-content: center;
    }
    
    #form .butt{
        width: 150px;
        height: 50px;
        font-size: 25px;
        display: inline-block;
    
    }
    
    
    hr{
        height: .25rem;
        width: 150px;
        margin: 0;
        background: tomato;
        border: none;
        transition: 1s ease-in-out;
    }
    
    #butt2:hover ~ hr{
        margin-left: 10px;
    }
    <section id="form">
        <div class="butts">
            <button class="butt" id="butt1">Login</button>
            <button class="butt" id="butt2">Sign Up</button>
        </div>
        <hr/>
    </section>

【问题讨论】:

    标签: html css flexbox underline


    【解决方案1】:

    您的代码不起作用,因为 Css 选择器不适用于更高的节点。 “~”仅适用于相邻元素,但此处“hr”标签与 .butts 类相邻。使用下面的代码来实现你想要实现的目标。

        #form{
            width: 300px;
            margin: auto;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        
        .butts{
            display: flex;
            justify-content: center;
            position: relative;
        }
        
        #form .butt{
            width: 150px;
            height: 50px;
            font-size: 25px;
            display: inline-block;
       }
        
        
        hr{
            height: .25rem;
            width: 150px;
            margin: 0;
            background: tomato;
            border: none;
            transition: 1s ease-in-out;
            position: absolute; 
            left: 0;
            bottom: 0;
        }
        
        #butt1:hover ~ hr{
            left: 0;
        }
        #butt2:hover ~ hr{
            left: 50%;
        }
        <section id="form">
            <div class="butts">
                <button class="butt" id="butt1">Login</button>
                <button class="butt" id="butt2">Sign Up</button>
    <hr/>
     
            </div>
                   </section>

    【讨论】:

    • 非常感谢先生,它成功了!!!但是请告诉我为什么我们必须使类对接的位置相对于它的工作......
    • 因为我做了“hr”位置:绝对,绝对定位元素不占用实际空间,而是浮动在其他元素上,并计算它与最近的相对定位父元素的位置偏移量(左、上等)。
    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    相关资源
    最近更新 更多