当鼠标光标悬停在按钮上时,文本向下移动,我只需要将文本5px
向左移动,当悬停在按钮上时,如何实现呢?
div {
text-align: center;
}
span {
margin-right: 10px;
}
button {
background: green;
border: none;
padding: 9px;
color: #FFF;
border-radius: 50%;
cursor: pointer;
transition: all 500ms ease;
}
button:hover {
padding: 13px;
transition: all 500ms ease;
}
<div>
<span>test</span><button>⟶</button>
</div>
答案 0 :(得分:1)
您面临的“问题”是span元素的显示类型为内联,因此它将跟随其同级元素。
我想有多种解决方法。
您可以将vertical-align: top;
添加到跨度。 (超级简单的CSS修复程序可保留HTML不变。缺点:将文本修复到元素顶部)
div {
text-align: center;
}
span {
vertical-align: top;
margin-right: 10px;
}
button {
background: green;
border: none;
padding: 9px;
color: #FFF;
border-radius: 50%;
cursor: pointer;
transition: all 500ms ease;
}
button:hover {
padding: 13px;
transition: all 500ms ease;
}
<div>
<span>test</span><button>⟶</button>
</div>
要真正解决此问题(不使文本流到元素顶部),您需要在跨度之外添加包装元素:
.center {
text-align: center;
}
span {
margin-right: 10px;
}
.wrapper {
display:inline-block;
}
button {
background: green;
border: none;
padding: 9px;
color: #FFF;
border-radius: 50%;
cursor: pointer;
transition: all 500ms ease;
}
button:hover {
padding: 13px;
transition: all 500ms ease;
}
<div class="center">
<p class="wrapper"><span>test</span></p><button>⟶</button>
</div>
您还可以将父级显示为flex并相应地证明子级:
.flex {
display: flex;
justify-content: center;
align-items: center;
}
span {
margin-right: 10px;
}
button {
background: green;
border: none;
padding: 9px;
color: #FFF;
border-radius: 50%;
cursor: pointer;
transition: all 500ms ease;
}
button:hover {
padding: 13px;
transition: all 500ms ease;
}
<div class="flex">
<div><span>test</span></div><button>⟶</button>
</div>