我正在创建一个主页,为此我在视口顶部放置了一个导航栏。导航栏包含按钮,用户应该将注意力从一个部分导航到另一个部分。 但实际上它们位于左侧。如果有2,3,4或5+按钮,我希望它们完全是中心。整个"按钮线"应该完全居中。
或者谈谈照片:
实现这一目标的常见/可能模式是什么?
我想到的唯一模式是为按钮home
和ubuntu
添加宽度,最后添加margin 0 auto
。
我的navigationbar.css(紫色条):
#navigationBar {
background-color:#660099;
}
.navigationButton {
background-color:transparent;
border-bottom:2px solid transparent;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:arial;
font-size:17px;
padding:16px 31px;
text-decoration:none;
text-shadow:0px 1px 0px #2f6627;
}
.navigationButton:hover {
background-color:transparent;
border-bottom:2px solid #18ab29;
}
.navigationButton:active {
position:relative;
top:1px;
}
my home.html:
<!DOCTYPE html>
<html>
<head>
<title>Ubuntu</title>
<link rel="stylesheet" type="text/css" href="style/general.css" />
<link rel="stylesheet" type="text/css" href="style/navigationBar.css" />
</head>
<body>
<div id="navigationBar">
<a href="home.html" class="navigationButton">Home</a>
<a href="ubuntuOverview.html" class="navigationButton">Ubuntu</a>
</div>
</body>
</html>
希望有人可以帮忙!
答案 0 :(得分:5)
添加: -
#navigationBar {
background-color:#660099;
text-align:center;
}
答案 1 :(得分:0)
您可以使用专为此目的而设计的CSS3 flexbox规范。基本上,您只需要将父容器设置为display: flex
,并集中证明其内容,即justify-content: center
。
#navigationBar {
background-color: #660099;
display: flex;
justify-content: center;
}
.navigationButton {
background-color: transparent;
border-bottom: 2px solid transparent;
cursor: pointer;
color: #ffffff;
font-family: arial;
font-size: 17px;
padding: 16px 31px;
position: relative;
top: 0;
text-decoration: none;
text-shadow: 0px 1px 0px #2f6627;
}
.navigationButton:hover {
background-color: transparent;
border-bottom: 2px solid #18ab29;
}
.navigationButton:active {
top: 1px;
}
&#13;
<div id="navigationBar">
<a href="home.html" class="navigationButton">Home</a>
<a href="ubuntuOverview.html" class="navigationButton">Ubuntu</a>
</div>
&#13;
答案 2 :(得分:0)
.navigationParent{
text-align:center;
}
.navigationBar{
display:inline-block;
}
如果条形显示设置为内联块,则文本对齐:中心将适用于它:
<div class="navigationParent">
<div class="navigationBar">
<a href="home.html" class="navigationButton">Home</a>
<a href="ubuntuOverview.html" class="navigationButton">Ubuntu</a>
</div>
</div>