【问题标题】:How to stop the elements from growing when hovering over an element?悬停在元素上时如何阻止元素增长?
【发布时间】:2021-08-24 15:45:26
【问题描述】:

我正在尝试为我正在学习 React with SASS 的项目设计标题。标头如下所示:

现在,当我将鼠标悬停在“登录”按钮上时,出现以下问题:

问题可能不清楚,但是当我将鼠标悬停在登录按钮上时,所有其他元素都向左移动了几个像素。但是,这种转变是可见的。

SCSS代码如下:

* {
  box-sizing: border-box;
}
.header {
    height: 120px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-bottom: 25px;
    background-color: #8DBF44;

    .logo {
        height: 40px;
        width: 15%;
        // padding: 25px;
        padding: 10px 30px;
        cursor: pointer;
    }

    .options {
        width: 70%;
        height: 100%;
        // padding-right: 20px;
        display: flex;
        align-items: center;
        justify-content: flex-end;

        .option {
            padding: 20px 30px;
            margin: 0 10px;
            text-decoration: none;
            text-align: center;
            font-size: 24px;
            color: rgb(255, 255, 255);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            transition: 0.2s linear;
            cursor: pointer;
            display: inline-block;

            &:hover {
                // cursor: pointer;
                // height: 80%;
                // padding: 10px 0;
                border-top: 2px solid #F9EA32;
                border-bottom: 2px solid #F9EA32;
                color: #F9EA32;
            }
        }

        #signIn {
            // padding-left: 40px;
            // padding-right: 40px;
            // padding-top: 12px;
            // padding-bottom: 12px;
            // box-sizing: content-box;
            padding: 12px 40px;
            background: #F9EA32;
            color:rgb(5, 78, 23);
            border-radius: 30px;
            border: none;
            display: inline-block;
            transition-duration: 0.3s;

            &:hover {
                border: 2px solid #F9EA32;
                // cursor: pointer;
                background: white;
            }
        }

    }
}

而 JSX 代码是:

import React from 'react';
import logo from '../../images/logo.png';
import './header.styles.scss';

const Header = () => (
    <div className='header'>
        <div className='logo'>
            <img src={logo} alt='Instant Pickup' />
        </div>
        <div className='options'>
            <a className='option'>About Us</a>
            <a className='option'>Become a Driver</a>
            <a className='option'>Blog</a>
            <a className='option'>Support</a>
            <a className='option' id='signIn'>
                Sign In
            </a>
        </div>
    </div>
);

export default Header;

我认为错误出在我的 CSS 逻辑上。我尝试调整填充和边距,但问题仍然存在。我知道这是一个基本问题,但我将不胜感激。

【问题讨论】:

    标签: css reactjs sass flexbox


    【解决方案1】:

    发生这种情况的原因是,您在悬停时为登录按钮添加了 2px 的边框,这使按钮的高度和宽度总共增加了 4 个像素。

    解决方案是让边框也处于非悬停状态,只需将其着色为透明,这样它就不会像这样可见

    #signIn {
        padding: 12px 40px;
        background: #F9EA32;
        color:rgb(5, 78, 23);
        border-radius: 30px;
        //** add this line
        border: 2px solid transparent;
        display: inline-block;
        transition-duration: 0.3s;
    
        &:hover {
            border: 2px solid #F9EA32;
            background: white;
        }
    }
    

    或者,当没有像这样悬停时,您可以将边框着色为与按钮相同的颜色

    #signIn {
        padding: 12px 40px;
        background: #F9EA32;
        color:rgb(5, 78, 23);
        border-radius: 30px;
        //** add this line
        border: 2px solid #F9EA32;
        display: inline-block;
        transition-duration: 0.3s;
    
        &:hover {
            border: 2px solid #F9EA32;
            background: white;
        }
    }
    

    这将使它看起来像按钮的一部分,并且只有在悬停时才会更改为其他边框颜色。

    【讨论】:

    • 谢谢。我也试过这个解决方案,效果也很好。
    • 此解决方案的好处是您不必通过为按钮设置显式高度/宽度来限制自己,这在某些情况下可能很有用。
    【解决方案2】:

    一种可能的解决方案是,您可以将登录按钮放在一个 div 中,并给 div 一个特定的宽度。例如:150 像素

    import React from 'react';
    import logo from '../../images/logo.png';
    import './header.styles.scss';
    
    const Header = () => (
        <div className='header'>
            <div className='logo'>
                <img src={logo} alt='Instant Pickup' />
            </div>
            <div className='options'>
                <a className='option'>About Us</a>
                <a className='option'>Become a Driver</a>
                <a className='option'>Blog</a>
                <a className='option'>Support</a>
               <div className='signup-container'>
                  <a className='option' id='signIn'>
                      Sign In
                  </a>
               <div>
    
            </div>
        </div>
    );
    
    export default Header;
    

    注册容器可以是

    .signup-container {
    widht: 150px;
    background-color: transparent
    }
    

    【讨论】:

    • 这适用于其他元素。但是现在登录按钮向右移动了几个像素。
    • 150 px 只是一个假设,您可以根据需要为其设置适当的宽度,以使您可以添加的元素居中 {dispaly: flex;对齐项目:中心; .signup-container 中的 justify-content:center}
    • 是的,我使用开发工具找到了正确的宽度。我也会尝试其他的东西。感谢您的帮助。
    • 实际上,使按钮的大小与注册容器的大小相同。所以我将它们都设置为相同的宽度。
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2021-11-18
    • 2015-08-10
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    相关资源
    最近更新 更多