我创建了一个导航栏,但我不知道如何将其置于网页的中心。请告知,我已将链接附加到底部的小提琴。不知道还要写什么,因为堆栈溢出希望我在发布此内容之前提供更多详细信息。希望你能尽快回答我的问题!
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<navbar>
<div class="topnav">
<a class="active" href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
</navbar>
</header>
<div class="row">
<p>Test text</p>
</div>
</body>
</html>
CSS
.row {
max-width: 1140px;
background-color: green;
margin: auto;
width: 80%;
}
body {
margin: 0;
}
/* Add a black background color to the top navigation */
.topnav {
background-color: #333;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
给你,
https://jsfiddle.net/90m2m0uh/
.topnav a {
/* dont use this */
/* float:left */
}
请注意“浮动”CSS
浮动 CSS 属性指定元素应放置在其容器的左侧或右侧,允许文本和内联元素环绕它。该元素已从网页的正常流程中删除,但仍保留为流程的一部分(与绝对定位相反)。
不太确定你在问什么,但我认为你只是想要这个。将 css 中的 .topnav 替换为此
.topnav {
background-color: #333;
overflow: hidden;
display: flex;
justify-content: center;
}
你需要做两件事 1. 将文本对齐方式更改为
<navbar>
navbar{
text-align: center;
}
这样菜单就位于导航栏的中心
2.去掉浮动并添加显示
.topnav a {
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
display: inline-block;
text-decoration: none;
font-size: 17px;
}
我们需要移除浮动,以便它们可以对齐,因为您迫使它们向左。 通过添加
display: inline-block
,<a>
标签将尊重您指定的填充。
记住
<a>
标签默认是内联元素,你不能指定顶部和底部值。
希望这是您正在寻找的。如果需要,很乐意解释或帮助提供更好的解决方案。
.row {
max-width: 1140px;
background-color: green;
margin: auto;
width: 80%;
}
body {
margin: 0;
}
/* Add a black background color to the top navigation */
navbar{
text-align: center;
}
.topnav {
background-color: #333;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
display: inline-block;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<navbar>
<div class="topnav">
<a class="active" href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
</navbar>
</header>
<div class="row">
<p>Test text</p>
</div>
</body>
</html>
您将
display
元素强制向左移动。不要使用它。
请使用
display: inline-block
。
其次使用
text-align: center;
到您的导航栏。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<nav>
<div class="topnav">
<a class="active" href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
</nav>
</header>
<div class="row">
<p>Test text</p>
</div>
</body>
</html>
body {
margin: 0;
padding: 0;
}
nav {
text-align: center; /* For aligning the text contents to center */
}
.topnav {
background-color: #333;
overflow: hidden;
}
.topnav a {
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
display: inline-block; /* You were forcing this to left DON'T DO IT! */
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.row {
max-width: 1140px;
background-color: green;
margin: auto;
width: 80%;
}
希望对你有帮助😊