Я не могу центрировать кнопки панели навигации. Есть ли способ сделать это в файле css? Я пробовал центрировать, но это не сработало.
HTML
<div class = "navbar">
<a href = "#home">Home</a>
<a href = "#news">News</a>
<a href = "#contact">Contact</a>
</div>
CSS
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 1300px; /* Full width */
z-index: 99999;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}






/* Links inside the navbar */
.navbar a {
display:inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
Я изменил ваш стиль для ".navbar a". Надеюсь, это сработает для вас.
Вы будете любовь flexbox — очень простым и очень полезным.
Flexbox требует родитель и Предметы.
Вы включаете флексбокс justify-content), либо на элементы.
Вот отличная шпаргалка для Flexbox.
Вот фантастический учебник YouTube.
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
z-index: 99999;
text-align: center;
width: 100vw; /* Full width */
display:flex;
justify-content:center;
border:5px solid yellow;
}
/* Links inside the navbar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
border:1px solid pink;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}<div class = "navbar">
<a href = "#home">Home</a>
<a href = "#news">News</a>
<a href = "#contact">Contact</a>
</div>Ты можешь использовать
<div class = "navbar">
<div style = "display: inline-block;">
<a href = "#home">Home</a>
<a href = "#news">News</a>
<a href = "#contact">Contact</a>
</div>
</div>
Если я вас правильно понял, вам нужно выровнять ссылки по центру навбара, для этого нужно сделать:
CSS:
/* Links inside the navbar */
.navbar a {
/* float: left; remove this property */
display: inline-block; /* change display: block to inline-block */
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
Вы можете увидеть пример на: https://jsfiddle.net/4gy2japx/
В стилизации ваших элементов есть несколько ошибок. Попытка выровнять плавающие элементы, назначение блока отображения линейным ссылкам и определение эмпирической длины, когда вы стремитесь к полной длине, — вот некоторые из них.
Попробуйте это вместо этого:
html,body {
margin: 0; /* overwrite browser defaults */
}
.navbar{
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%; /* Full width */
z-index: 99999;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}<div class = "navbar">
<a href = "#home">Home</a>
<a href = "#news">News</a>
<a href = "#contact">Contact</a>
</div>.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%; /* Full width */
z-index: 99999;
text-align: center;
margin: 0 auto;
}
.navbar ul {
display:inline-block;
list-style-type: none;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
</head>
<body>
<div class = "navbar">
<a href = "#home">Home</a>
<a href = "#news">News</a>
<a href = "#contact">Contact</a>
</div>
</body>
</html>You have to remove float left and add display: inline-block;
.navbar a {
float: left;
display: block;
Привет, Махар, снова продолжаю. Можем ли мы побеспокоить вас, чтобы выбрать лучший ответ — или предоставить свой собственный и выбрать его — чтобы закрыть вопрос? Это помогло бы нам. Огромное спасибо