为什么flex box在我的代码中不能按预期工作



我开始用HTML编写这段代码,但没有完成,但希望导航栏与它们水平对齐,但这不起作用。我是编码的初学者。

我本以为导航栏会出现在页面的顶部,但不知怎么的,它被向左对齐了。

@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;600&family=Poppins:wght@300;400&display=swap');
/* !BASE STYLES/RESET */
*,
*::before,
*::after{
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root{
--light-color: #f3f3fe;
--dark-color: #363636;
--border-top-color: rgba(0,0,0,.1);
--link-hover-color: rgba(255,255,255,.2);
--title-font-family: 'Playfair Display', 'serif';
--body-font-family: 'Poppins', 'sans-serif';
--container-padding: 1.5rem;
--section-vertical-spacing: 8rem;
}
html{
font-size: 62.5%;
}
body{
font-family: var(--body-font-family);
font-size: 1.6rem;
line-height: 1.5;
background-color: var(--light-color);
color: var(--dark-color);
overflow-x: hidden; 
}
body.dark-theme{
--light-color: #000;
--dark-color: #fff;
--border-top-color: rgba(255, 255, 255, .1);
}
a{
text-decoration: none;
color: inherit;
outline: none;
}
img{
max-width: 100%;
display: block;
}
ul{
list-style: none;
}
span{
display: inline-block;
}
i{
font-size: 2.4rem;
}
input,
button,
textarea{
font: inherit;
color: inherit;
background-color: transparent;
border: none;
outline: none;
}
/* !HEADER */
.header{
margin-bottom: 5rem;
}
/* !navbar */
.navbar{
display: flex;
align-items: center;
justify-content: space-between;
height: 8rem;
position: relative;
}
<!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">
<title>NGAYU'S WEBSITE</title>
<!-- !BOX ICONS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css">
<!-- !SCROLL REVEAL -->
<script src="https://unpkg.com/scrollreveal@4.0.0/dist/scrollreveal.min.js"></script>
<!-- !MY STYLESHEET -->
<link rel="stylesheet" href="./assets/css/styles.css">
</head>
<body>
<!-- !HEADER -->
<header class="header" id="home">
<nav class="navbar container">
<a href="./index.html" class="logo">Ngayu</a>
<div class="navbar-buttons">
<button type="button" class="button icon-button menu-toggle-button">
<i class="bx bx-menu open-icon"></i>
<i class="bx bx-x close-icon"></i>
</button>
<button type="button" class="button icon-button theme-toggle-button">
<i class="bx bx-toggle-left theme-off"></i>
<i class="bx bx-toggle-right theme-on"></i>
</button>
</div>
<div class="menu">
<ul class="list">
<li class="list-item">
<a href="#" class="list-link">
<span>01</span> About
</a>
</li>
<li class="list-item">
<a href="#" class="list-link">
<span>02</span> Portfolio
</a>
</li>
<li class="list-item">
<a href="#" class="list-link">
<span>03</span> Contact
</a>
</li>
</ul>
</div>
</nav>
</header>
</body>
</html>

您在.header中缺少结束大括号

如果你指的是右边的三个项目,那么添加这个CSS规则将它们放在一行中(弹性项目的默认设置是水平排列(:

.menu > .list {
display: flex;
}

如果你需要在它们之间创建一些距离,添加(例如(这个,这将为每个菜单项创建一个左边距:

.menu > .list > .list-item {
margin-left: 20px;
}

@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;600&family=Poppins:wght@300;400&display=swap');
/* !BASE STYLES/RESET */
*,
*::before,
*::after{
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root{
--light-color: #f3f3fe;
--dark-color: #363636;
--border-top-color: rgba(0,0,0,.1);
--link-hover-color: rgba(255,255,255,.2);
--title-font-family: 'Playfair Display', 'serif';
--body-font-family: 'Poppins', 'sans-serif';
--container-padding: 1.5rem;
--section-vertical-spacing: 8rem;
}
html{
font-size: 62.5%;
}
body{
font-family: var(--body-font-family);
font-size: 1.6rem;
line-height: 1.5;
background-color: var(--light-color);
color: var(--dark-color);
overflow-x: hidden; 
}
body.dark-theme{
--light-color: #000;
--dark-color: #fff;
--border-top-color: rgba(255, 255, 255, .1);
}
a{
text-decoration: none;
color: inherit;
outline: none;
}
img{
max-width: 100%;
display: block;
}
ul{
list-style: none;
}
span{
display: inline-block;
}
i{
font-size: 2.4rem;
}
input,
button,
textarea{
font: inherit;
color: inherit;
background-color: transparent;
border: none;
outline: none;
}
/* !HEADER */
.header{
margin-bottom: 5rem;
}
/* !navbar */
.navbar{
display: flex;
align-items: center;
justify-content: space-between;
height: 8rem;
position: relative;
}
.menu > .list {
display: flex;
}
.menu > .list > .list-item {
margin-left: 20px;
}
<!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">
<title>NGAYU'S WEBSITE</title>
<!-- !BOX ICONS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css">
<!-- !SCROLL REVEAL -->
<script src="https://unpkg.com/scrollreveal@4.0.0/dist/scrollreveal.min.js"></script>
<!-- !MY STYLESHEET -->
<link rel="stylesheet" href="./assets/css/styles.css">
</head>
<body>
<!-- !HEADER -->
<header class="header" id="home">
<nav class="navbar container">
<a href="./index.html" class="logo">Ngayu</a>
<div class="navbar-buttons">
<button type="button" class="button icon-button menu-toggle-button">
<i class="bx bx-menu open-icon"></i>
<i class="bx bx-x close-icon"></i>
</button>
<button type="button" class="button icon-button theme-toggle-button">
<i class="bx bx-toggle-left theme-off"></i>
<i class="bx bx-toggle-right theme-on"></i>
</button>
</div>
<div class="menu">
<ul class="list">
<li class="list-item">
<a href="#" class="list-link">
<span>01</span> About
</a>
</li>
<li class="list-item">
<a href="#" class="list-link">
<span>02</span> Portfolio
</a>
</li>
<li class="list-item">
<a href="#" class="list-link">
<span>03</span> Contact
</a>
</li>
</ul>
</div>
</nav>
</header>
</body>
</html>

最新更新