首页 > 解决方案 > 固定位置导致我的标题被推到右边

问题描述

当我没有固定它时,我的标题会占据其容器的整个宽度。

html {
    font-size: 32px;
}

body {
    text-align: center;
}

header {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.25rem;
    padding: 0.625rem;
    border-bottom: 3px solid black;
    font-family: 'Cinzel Decorative', cursive;
}

nav {
    font-family: 'IM Fell French Canon SC', serif;
    text-align: right;
    flex-grow: 1;
    font-size: 1rem;
}

当我修复我的标题时,它的右侧会被推出页面:

header {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.25rem;
    padding: 0.625rem;
    position:fixed;
    width: 100%;
    z-index: 10;
    border-bottom: 3px solid black;
    font-family: 'Cinzel Decorative', cursive;
}

有没有其他适当的方法可以做到这一点,而无需在右侧添加填充以抵消更改?

HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Cinzel+Decorative:wght@700&family=IM+Fell+French+Canon+SC&display=swap" rel="stylesheet"> 
    <link href="normalize.css" rel="stylesheet" />
    <link href="style.css" rel="stylesheet" />
    <title>Watch iT</title>
</head>
<body>

    <!-- Header Section -->
    <header>
        <span id="logo">W</span>
        <nav>
            <span><a href="#">About</a></span>
            <span><a href="#">Products</a></span>
            <span><a href="#">Contact</a></span>
        </nav>
    </header>


</body>
</html>

标签: htmlcssflexbox

解决方案


使用 时position: fixed,该元素会从正常的文档流中移除,并且不会在页面布局中为该元素创建空间。它相对于视口建立的初始包含块定位。

如果您指定width:100%,则元素的总宽度将是其包含块的 100% 加上任何水平边距、填充和边框(除非您使用了 box-sizing:border-box,在这种情况下,只有边距被添加到 100% 到更改其总宽度的计算方式)。

width: 100%position: fixedon 的组合header是导航菜单项溢出 flex 容器中页面右侧的原因。这是因为width: 100%允许内容大于其包含块和溢出。如果您添加box-sizing: border-box;到页面,则溢出问题得到解决。此外,您可以为固定位置容器设置 atopleftposition 以指定距元素包含块边缘的偏移量。

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
    font-size: 32px;
}

body {
    text-align: center;
}

header {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.25rem;
    padding: 0.625rem;
    position:fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 10;
    border-bottom: 3px solid black;
    font-family: 'Cinzel Decorative', cursive;
}

nav {
    font-family: 'IM Fell French Canon SC', serif;
    text-align: right;
    flex-grow: 1;
    font-size: 1rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Cinzel+Decorative:wght@700&family=IM+Fell+French+Canon+SC&display=swap" rel="stylesheet"> 
    <link href="normalize.css" rel="stylesheet" />
    <link href="style.css" rel="stylesheet" />
    <title>Watch iT</title>
</head>
<body>

    <!-- Header Section -->
    <header>
        <span id="logo">W</span>
        <nav>
            <span><a href="#">About</a></span>
            <span><a href="#">Products</a></span>
            <span><a href="#">Contact</a></span>
        </nav>
    </header>


</body>
</html>


推荐阅读