问题描述
将鼠标悬停在上,您就会看到元素的移动方式.为什么?
Just hover on 'a headline' in the snippet below and you will see how elements are moving. Why?
没有边距..并且它们仅在我向inline-block
元素添加边框时才移动.尝试在section.twelve a
中添加更多边框宽度,例如:
There's no margin .. And they're only moving when I add border to the inline-block
element. Try to add more border width in section.twelve a
like:
section.twelve a {
border-bottom: 10px solid #FFFAFF;
}
但是,如果您删除边框,一切都很好.为什么会出现这种情况?只是边界吗?
But if you remove the border everything's fine.. Why is this behavior ? and is it only for border?
我只想在元素中添加任何样式,而不会影响其他样式.
I just want to add any styles to the element without effecting the others.
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>
推荐答案
添加或更改边框的宽度时,将更改元素的大小.因此,通过在悬停时添加边框,该框会扩大以占据更多空间,这自然会改变周围文本/元素的位置.
When you add, or change the width, of a border, that changes the size of the element. Hence, by adding the border on hover, the box grows to occupy more space, which naturally shifts the position of surrounding text / elements.
解决此问题的一种方法是始终显示边框,因此框的大小是固定的.当边框不可见时,它是透明的.
One method to resolve this issue is to always have the border present, so the size of the box is fixed. When the border shouldn't be visible, it's transparent.
这是一个例子:
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>
这篇关于在悬停上添加边框会移动周围的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!