我有这个代码,这些元素一起生活在这个页面的标题中。当H1#name的悬停效果生效时,home按钮会稍微向右移动。这是因为display flex吗?或者,有什么我可以做的,使这个主页按钮停留在原地,而悬停效果发生?到目前为止,我已经尝试了几种定位方法,但都没有奏效。
#info-header {
background-color: dimgrey;
height: 5rem;
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
font-size: 3rem;
}
#name {
color: mistyrose;
font-family: 'Monoton', cursive;
}
#name:hover {
font-family: 'Monoton', cursive;
text-transform: uppercase;
transform: scale(1.15);
transition: 1.75s;
}
a {
text-decoration: none;
color: mistyrose;
}
a:hover {
color: black;
opacity: 10%;
}
#home-button {
height: 2.25rem;
border-radius: 5%;
font-weight: 900;
cursor: pointer;
background-color: black;
}
#home-button:hover {
background-color: mistyrose;
transition: 1.25s;
}<header id="info-header">
<h1 id="name">Name</h1>
<button id="home-button"><a href="index.html">Home</a></button>
</header>
发布于 2020-08-22 15:36:35
将position: absolute;和right: 30%;添加到您的#home-button css。您可以根据需要调整right percentage值。
#home-button {
height: 2.25rem;
border-radius: 5%;
font-weight: 900;
cursor: pointer;
background-color: black;
position: absolute;
right: 30%;
}
#info-header {
background-color: dimgrey;
height: 5rem;
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
font-size: 3rem;
}
#name {
color: mistyrose;
font-family: 'Monoton', cursive;
}
#name:hover {
font-family: 'Monoton', cursive;
text-transform: uppercase;
transform: scale(1.15);
transition: 1.75s;
}
a {
text-decoration: none;
color: mistyrose;
}
a:hover {
color: black;
opacity: 10%;
}
#home-button {
height: 2.25rem;
border-radius: 5%;
font-weight: 900;
cursor: pointer;
background-color: black;
position: absolute;
right: 15%;
}
#home-button:hover {
background-color: mistyrose;
transition: 1.25s;
}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=">
<title></title>
</head>
<body>
<header id="info-header">
<h1 id="name">Name</h1>
<button id="home-button"><a href="index.html">Home</a></button>
</header>
</body>
</html>
发布于 2020-08-22 23:44:22
向h1#name添加固定宽度解决了这个问题。
https://stackoverflow.com/questions/63533613
复制相似问题