似乎无法将导航栏中的文本居中 我将如何使用来解决这种情况?
Cant seem to center the text in the nav bar how would i use fix this situation?
我试图将导航栏中的文本居中,但出于某种原因,代码似乎无法正常工作并停留在左侧。我该如何解决这种情况 我尝试向其中添加不同类型的代码以查看情况是否会得到解决,但仍然无法将导航栏上的文本居中
CSS:
.topnav {
background-color: #333;
overflow: hidden;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<title>My Wesbite </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<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>
<div>
<h1> this is header 1</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</body>
</html>
这是使用 flexbox
更动态地执行此操作的方法:
如果您添加新选项卡,它会自动更新。您无需编辑 CSS 宽度。这就是我如此喜欢 flexbox 的原因。它会自动处理所有事情。
CSS:
.topnav {
background-color: #333;
display: flex;
}
.topnav a {
text-align: center;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
flex: 1;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
我试图将导航栏中的文本居中,但出于某种原因,代码似乎无法正常工作并停留在左侧。我该如何解决这种情况 我尝试向其中添加不同类型的代码以查看情况是否会得到解决,但仍然无法将导航栏上的文本居中
CSS:
.topnav {
background-color: #333;
overflow: hidden;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<title>My Wesbite </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<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>
<div>
<h1> this is header 1</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</body>
</html>
这是使用 flexbox
更动态地执行此操作的方法:
如果您添加新选项卡,它会自动更新。您无需编辑 CSS 宽度。这就是我如此喜欢 flexbox 的原因。它会自动处理所有事情。
CSS:
.topnav {
background-color: #333;
display: flex;
}
.topnav a {
text-align: center;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
flex: 1;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}