首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >悬停时调整CSS文本大小会导致同级元素移动

悬停时调整CSS文本大小会导致同级元素移动
EN

Stack Overflow用户
提问于 2020-08-22 15:22:57
回答 2查看 47关注 0票数 0

我有这个代码,这些元素一起生活在这个页面的标题中。当H1#name的悬停效果生效时,home按钮会稍微向右移动。这是因为display flex吗?或者,有什么我可以做的,使这个主页按钮停留在原地,而悬停效果发生?到目前为止,我已经尝试了几种定位方法,但都没有奏效。

代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
<header id="info-header">
            <h1 id="name">Name</h1>
            <button id="home-button"><a href="index.html">Home</a></button>
        </header>

EN

回答 2

Stack Overflow用户

发布于 2020-08-22 15:36:35

position: absolute;right: 30%;添加到您的#home-button css。您可以根据需要调整right percentage值。

代码语言:javascript
复制
#home-button {
    height: 2.25rem;
    border-radius: 5%;
    font-weight: 900;
    cursor: pointer;
    background-color: black;
    position: absolute;
    right: 30%;
}

代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
<!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>

票数 0
EN

Stack Overflow用户

发布于 2020-08-22 23:44:22

向h1#name添加固定宽度解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63533613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档