Flexbox Html CSS 设置位置问题

问题描述 投票:0回答:1

我是使用 html、css flexbox 创建网站的初学者,我已将网站标题的完整宽度设置为 1000px。标题徽标应从 50px 开始位置,导航栏应从 600px 开始位置。我尝试过的内容附在下面。它已显示,但我写得是否正确,请检查我

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Header Design</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        .header {
            display: flex;
            width: 1000px;
            background-color: red;
            align-items: center;
            padding: 0 50px; /* Adjust padding to align the logo */
            box-sizing: border-box; /* Ensure padding is included in width */
            height: 100px;
        }
        .logo {
            flex: 0 0 auto;
            font-size: 24px;
            color: black;
        }
        .spacer {
            flex: 0 0 500px; /* Fixed spacer to push the nav */
        }
        .nav {
            display: flex;
            gap: 20px;
        }
        .nav a {
            text-decoration: none;
            color: black;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">Logo</div>
        <div class="spacer"></div>
        <div class="nav">
            <a href="#">Home</a>
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Contact Us</a>
        </div>
    </div>
</body>
</html>
html css flexbox
1个回答
0
投票
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Header Design</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center; /* Center the header horizontally */
        }
        .header {
            display: flex;
            width: 1000px;
            background-color: red;
            align-items: center;
            padding: 0 50px; /* Adjust padding to align the logo */
            box-sizing: border-box; /* Ensure padding is included in width */
            height: 100px;
        }
        .logo {
            flex: 0 0 auto;
            font-size: 24px;
            color: black;
        }
        .nav {
            display: flex;
            gap: 20px;
            margin-left: auto; /* Push the nav to the right */
        }
        .nav a {
            text-decoration: none;
            color: black;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">Logo</div>
        <div class="nav">
            <a href="#">Home</a>
            <a href="#">About</a>
            <a href="#">Services</a>
            <a href="#">Contact Us</a>
        </div>
    </div>
</body>
</html>

.nav 容器使用 margin-left: auto 将其推到右侧,确保导航栏从相对于徽标的固定位置开始。

© www.soinside.com 2019 - 2024. All rights reserved.