【问题标题】:How to avoid the box movement on hover? [duplicate]如何避免框在悬停时移动? [复制]
【发布时间】:2018-10-20 12:40:05
【问题描述】:

我构建了一个基于li 标签的导航块,看起来如下screencasts。如您所见,整个盒子在移动,问题如何避免盒子移动?

html 是 ReactJs 的一个组件:

<li className="nav-item">
  <a className="nav-link" href={href}>
    {children}
    <span className="pl-3 nav-link-text font-weight-bold">{ReasonReact.string(text)}</span>
  </a>
</li>  

以及基于scss的样式:

  >.nav-item {
    >.nav-link {
      color: #515253;
      &:hover {
        color: #85899B;
        border-left: 5px solid #37BDE7;
        background-color: #f2f2f2;
      }
    }
  }

【问题讨论】:

  • 添加box-sizing:border-box 或用透明色初始定义边框

标签: html css sass jsx


【解决方案1】:

为非悬停状态添加一个透明的border-left:

 >.nav-item {
    >.nav-link {
      color: #515253;
      border-left: 5px solid transparent;
      &:hover {
        color: #85899B;
        border-left-color: #37BDE7;
        background-color: #f2f2f2;
      }
    }
  }

【讨论】:

    【解决方案2】:

    你可以使用插入box-shadow来代替边框

    a:hover {
       box-shadow: inset 5px 0 0 #37BDE7;
    }
    

    Codepen demo

    【讨论】:

      【解决方案3】:
      You can also add left border on hover before like,
      
      >.nav-item {
          >.nav-link {
            color: #515253;
            position:relative;
            &:hover {
              color: #85899B;
              background-color: #f2f2f2;
            }
            &:hover:before{
             content: '';
             height: 100%;
             width: 5px;
             background: #37BDE7;
             position: absolute;
             left: 0;
             top: 0;
            }
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 2018-07-01
        相关资源
        最近更新 更多