中心导航栏链接

时间:2019-02-21 01:50:36

标签: html css web

我似乎无法将导航栏按钮居中。有没有办法在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;
}

6 个答案:

答案 0 :(得分:0)

/* Links inside the navbar */
.navbar a {
 display:inline-block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

我修改了您的“ .navbar a”样式。希望它对您有用。

答案 1 :(得分:0)

您会 flexbox-超级简单,而且非常有用。

Flexbox需要 父级

您在父级上打开flexbox ,然后在父级上设置各种开关(例如{ {1}}或项目上。

Here is a great cheatsheet for Flexbox。

Here is a fantastic YouTube tutorial

演示:

justify-content
.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;
}

答案 2 :(得分:0)

您可以使用

=unique([range.sourceItems])

答案 3 :(得分:0)

如果我对您的理解正确,则需要在导航栏的中心对齐链接,为此,您需要执行以下操作:

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/

答案 4 :(得分:0)

您的元素样式有几个错误。尝试对齐浮动元素,将显示块分配给线性链接并在尝试全长时定义经验长度就是其中的一部分。

尝试以下方法:

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>

答案 5 :(得分:0)

.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;