【问题标题】:Adding border on hover shifts surrounding elements在悬停上添加边框会移动周围的元素
【发布时间】:2019-08-02 02:33:13
【问题描述】:

只需悬停在下面 sn-p 中的“标题” 上,您就会看到元素是如何移动的。为什么?

没有边距 .. 只有当我为 inline-block 元素添加边框时,它们才会移动。尝试在section.twelve a 中添加更多边框宽度,例如:

section.twelve a {
        border-bottom: 10px solid #FFFAFF;
    }

但是,如果您删除边框一切都很好.. 为什么会出现这种行为?是否只用于边框?

我只想为元素添加任何样式而不影响其他元素。

section{
    position: relative;
    height: 300px;
    padding: 15px 80px;
    z-index: 1;
}

section h1{
    font-size:3em;
    font-weight: 100;
    line-height: 1.3;
 
}

section a {
    position: relative;
    display: inline-block;
    text-decoration: none;
    transition: all 0.3s ease-in-out;
    vertical-align: bottom;
}

section.twelve {
    background: #121A5A;
    color: #FFFAFF;
}
section.twelve a {
    color:#D8315B;
    font-weight: 700;
    overflow: hidden;
    padding: 0px 5px;
    transition all 0.2s ease;
    border-bottom: 5px solid #FFFAFF;
}

.twelve a:before{
    content: "";
    top:0; left: 0;
    position: absolute;
    width:100%; height: 100%;
    background: #FFFAFF;
    z-index: -1;
    transition: all 0.2s ease;
    transform: translateX(100%);
}
.twelve a:hover::before {
    transform: translateX(-95%);
    background: #D8315B;
}
.twelve a:hover{
    color: #FFFAFF;
    transform: translateX(5px);
    border-bottom: 1px solid #FFFAFF;
}
<section class="twelve">
        <h1>Write <a href="#">a headline</a> that makes people do kind of a double take whenthey read it.</h1>
    </section>

【问题讨论】:

    标签: html css border


    【解决方案1】:

    当您添加或更改边框的宽度时,会更改元素的大小。因此,通过在悬停时添加边框,框会增大以占用更多空间,这自然会改变周围文本/元素的位置。

    解决此问题的一种方法是始终存在边框,因此框的大小是固定的。当边框不可见时,它是透明的。

    这是一个例子:

    section {
      position: relative;
      height: 300px;
      padding: 15px 80px;
      z-index: 1;
    }
    section h1 {
      font-size: 3em;
      font-weight: 100;
      line-height: 1.3;
    }
    section a {
      position: relative;
      display: inline-block;
      text-decoration: none;
      transition: all 0.3s ease-in-out;
      vertical-align: bottom;
    }
    section.twelve {
      background: #121A5A;
      color: #FFFAFF;
    }
    section.twelve a {
      color: #D8315B;
      font-weight: 700;
      overflow: hidden;
      padding: 0px 5px;
      transition all 0.2s ease;
      border-bottom: 5px solid transparent;   /* ADJUSTMENT */
    }
    .twelve a:before {
      content: "";
      top: 0;
      left: 0;
      position: absolute;
      width: 100%;
      height: 100%;
      background: #FFFAFF;
      z-index: -1;
      transition: all 0.2s ease;
      transform: translateX(100%);
    }
    .twelve a:hover::before {
      transform: translateX(-95%);
      background: #D8315B;
    }
    .twelve a:hover {
      color: #FFFAFF;
      transform: translateX(5px);
      border-bottom: 5px solid white;       /* ADJUSED */
    }
    <section class="twelve">
      <h1>Write <a href="#">a headline</a> that makes people do kind of a double take whenthey read it.</h1>
    </section>

    【讨论】:

    • 这样一个简单而优雅的解决方案迈克尔。欣赏吧兄弟!
    【解决方案2】:

    是的,悬停时你正在改变元素的边框,所以元素的总高度也会改变

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 2023-03-31
      • 2014-09-12
      • 2014-10-23
      • 2011-11-29
      相关资源
      最近更新 更多