垂直 border/divider 居中于 header
Vertical border/divider centered in header
是否可以并且相当容易在我的 header 中创建一个居中的垂直边框?我想让它划分我的 logo/h1-text 和我的导航栏。
我知道css很乱,我上周刚学了html和css!我可能会删除一些 css.
header {
height: 60px;
width: 85%;
/background-color: white;
margin: 0 auto;
/box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
/border: 2px solid #333;
left: 0;
right: 0;
/border-sizing: border-box;
/-moz-box-sizing: border-box;
/-webkit-box-sizing: border-box;
z-index: 999999;
}
header #kage > *, header li {
display: inline-block;
}
header li {
padding: 0 8px 0 8px;
float:left;
}
#kage {
width: 99%;
margin: 0 auto;
height: 100%;
line-height: 59px;
text-align: center;
}
.button1:hover {
background-color: #f2f2f2;
}
.button2:hover {
background-color: #f2f2f2;
}
.button3:hover {
background-color: #f2f2f2;
}
.active {
text-decoration: underline;
}
header a {
text-decoration: none;
color: #333;
position: relative;
}
header h1 {
margin: 0;
float: left;
height: 100%;
text-shadow: 1px 2px lightgrey;
}
header h1:hover {
color: #f2f2f2;
}
#topnav {
height: 100%;
/*float: right;*/
font-weight: 700;
font-size: 1.3em;
width: 310.95px;
}
header ul {
list-style-type: none;
margin: 0;
}
<header>
<div id="kage">
<a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
<nav id="topnav">
<ul class="menu">
<a href="index.html"><li class="button1 active">Home</li></a>
<a href="profil.html"><li class="button2">About</li></a>
<a href="mdu.html"><li class="button3">MDU</li></a>
</ul>
</nav>
</div>
</header>
您似乎需要 运行 全屏显示该代码段,因此 header 不会分成两行。
有很多方法可以做到这一点。
我认为最简单的方法是在您的#topnav 上放置一个 border-left。
#topnav {
height: 100%;
/*float: right;*/
font-weight: 700;
font-size: 1.3em;
width: 310.95px;
border-left: 2px solid rgb(0,0,0);
margin: 0 0 0 40px;
}
我很容易地在你的topnav左侧添加了一个2像素的边框,宽度为黑色。
并用左侧的边距对其进行了一些调整。边距:0 0 0 40px; ~(边距:右上左下)
header {
height: 60px;
width: 85%;
/background-color: white;
margin: 0 auto;
/box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
/border: 2px solid #333;
left: 0;
right: 0;
/border-sizing: border-box;
/-moz-box-sizing: border-box;
/-webkit-box-sizing: border-box;
z-index: 999999;
}
header #kage > *, header li {
display: inline-block;
}
header li {
padding: 0 8px 0 8px;
float:left;
}
#kage {
width: 99%;
margin: 0 auto;
height: 100%;
line-height: 59px;
text-align: center;
}
.button1:hover {
background-color: #f2f2f2;
}
.button2:hover {
background-color: #f2f2f2;
}
.button3:hover {
background-color: #f2f2f2;
}
.active {
text-decoration: underline;
}
header a {
text-decoration: none;
color: #333;
position: relative;
}
header h1 {
margin: 0;
float: left;
height: 100%;
text-shadow: 1px 2px lightgrey;
}
header h1:hover {
color: #f2f2f2;
}
#topnav {
height: 100%;
/*float: right;*/
font-weight: 700;
font-size: 1.3em;
width: 310.95px;
border-left: 2px solid rgb(0,0,0);
margin: 0 0 0 40px;
}
header ul {
list-style-type: none;
margin: 0;
}
<header>
<div id="kage">
<a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
<nav id="topnav">
<ul class="menu">
<a href="index.html"><li class="button1 active">Home</li></a>
<a href="profil.html"><li class="button2">About</li></a>
<a href="mdu.html"><li class="button3">MDU</li></a>
</ul>
</nav>
</div>
</header>
您可以简化 HTML 和 CSS 并且仍然实现您正在寻找的布局。
使用 header
元素作为徽标和导航链接的父容器。
在 header
中,有两个子元素 h1
和 nav
,并让这两个元素成为行内块元素。
如果您希望子元素在 header
内居中,请使用 text-align: center
调整定位。
header
可能会在小屏幕尺寸上换行,您可以使用 white-space: nowrap
来防止这种情况发生(您的设计选择)。
然后您可以调整边距、填充和边框值来控制各个链接之间的间距。
请注意,对于 nav
,请应用 vertical-align: top
,否则您会在基线上方或下方得到一些额外的空格。
此外,如果您为 header
指定 height
,还指定 line-height
以便内联子元素垂直居中(如果这是您想要的)。
header {
height: 60px;
line-height: 60px;
width: 85%;
margin: 0 auto;
border: 1px dotted blue; /* for demo only */
text-align: center; /* optional? */
white-space: nowrap; /* to prevent line from wrapping, optional */
}
header h1 {
display: inline-block;
border-right: 1px solid black;
margin: 0;
padding-left: 10px; /* adjust to taste */
padding-right: 10px; /* add whitespace before vertical border */
height: 100%;
}
header h1 a {
text-shadow: 1px 2px lightgrey;
}
header h1:hover a {
color: #f2f2f2;
}
header a {
text-decoration: none;
color: #333;
}
.button:hover {
background-color: #f2f2f2;
}
.active {
text-decoration: underline;
}
nav {
display: inline-block;
vertical-align: top;
padding-left: 10px; /* whitespace after vertical border */
font-weight: bold;
font-size: 1.3em;
border: 1px dotted gray; /* for demo only */
}
nav a {
margin: 0 20px;
}
<header>
<h1><a href="index.html">H1 TEXT HEREEEEE</a></h1>
<nav>
<a href="index.html" class="button active">Home</a>
<a href="profil.html" class="button">About</a>
<a href="mdu.html" class="button">MDU</a>
</nav>
</header>
是否可以并且相当容易在我的 header 中创建一个居中的垂直边框?我想让它划分我的 logo/h1-text 和我的导航栏。
我知道css很乱,我上周刚学了html和css!我可能会删除一些 css.
header {
height: 60px;
width: 85%;
/background-color: white;
margin: 0 auto;
/box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
/border: 2px solid #333;
left: 0;
right: 0;
/border-sizing: border-box;
/-moz-box-sizing: border-box;
/-webkit-box-sizing: border-box;
z-index: 999999;
}
header #kage > *, header li {
display: inline-block;
}
header li {
padding: 0 8px 0 8px;
float:left;
}
#kage {
width: 99%;
margin: 0 auto;
height: 100%;
line-height: 59px;
text-align: center;
}
.button1:hover {
background-color: #f2f2f2;
}
.button2:hover {
background-color: #f2f2f2;
}
.button3:hover {
background-color: #f2f2f2;
}
.active {
text-decoration: underline;
}
header a {
text-decoration: none;
color: #333;
position: relative;
}
header h1 {
margin: 0;
float: left;
height: 100%;
text-shadow: 1px 2px lightgrey;
}
header h1:hover {
color: #f2f2f2;
}
#topnav {
height: 100%;
/*float: right;*/
font-weight: 700;
font-size: 1.3em;
width: 310.95px;
}
header ul {
list-style-type: none;
margin: 0;
}
<header>
<div id="kage">
<a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
<nav id="topnav">
<ul class="menu">
<a href="index.html"><li class="button1 active">Home</li></a>
<a href="profil.html"><li class="button2">About</li></a>
<a href="mdu.html"><li class="button3">MDU</li></a>
</ul>
</nav>
</div>
</header>
您似乎需要 运行 全屏显示该代码段,因此 header 不会分成两行。
有很多方法可以做到这一点。 我认为最简单的方法是在您的#topnav 上放置一个 border-left。
#topnav {
height: 100%;
/*float: right;*/
font-weight: 700;
font-size: 1.3em;
width: 310.95px;
border-left: 2px solid rgb(0,0,0);
margin: 0 0 0 40px;
}
我很容易地在你的topnav左侧添加了一个2像素的边框,宽度为黑色。 并用左侧的边距对其进行了一些调整。边距:0 0 0 40px; ~(边距:右上左下)
header {
height: 60px;
width: 85%;
/background-color: white;
margin: 0 auto;
/box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
/border: 2px solid #333;
left: 0;
right: 0;
/border-sizing: border-box;
/-moz-box-sizing: border-box;
/-webkit-box-sizing: border-box;
z-index: 999999;
}
header #kage > *, header li {
display: inline-block;
}
header li {
padding: 0 8px 0 8px;
float:left;
}
#kage {
width: 99%;
margin: 0 auto;
height: 100%;
line-height: 59px;
text-align: center;
}
.button1:hover {
background-color: #f2f2f2;
}
.button2:hover {
background-color: #f2f2f2;
}
.button3:hover {
background-color: #f2f2f2;
}
.active {
text-decoration: underline;
}
header a {
text-decoration: none;
color: #333;
position: relative;
}
header h1 {
margin: 0;
float: left;
height: 100%;
text-shadow: 1px 2px lightgrey;
}
header h1:hover {
color: #f2f2f2;
}
#topnav {
height: 100%;
/*float: right;*/
font-weight: 700;
font-size: 1.3em;
width: 310.95px;
border-left: 2px solid rgb(0,0,0);
margin: 0 0 0 40px;
}
header ul {
list-style-type: none;
margin: 0;
}
<header>
<div id="kage">
<a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
<nav id="topnav">
<ul class="menu">
<a href="index.html"><li class="button1 active">Home</li></a>
<a href="profil.html"><li class="button2">About</li></a>
<a href="mdu.html"><li class="button3">MDU</li></a>
</ul>
</nav>
</div>
</header>
您可以简化 HTML 和 CSS 并且仍然实现您正在寻找的布局。
使用 header
元素作为徽标和导航链接的父容器。
在 header
中,有两个子元素 h1
和 nav
,并让这两个元素成为行内块元素。
如果您希望子元素在 header
内居中,请使用 text-align: center
调整定位。
header
可能会在小屏幕尺寸上换行,您可以使用 white-space: nowrap
来防止这种情况发生(您的设计选择)。
然后您可以调整边距、填充和边框值来控制各个链接之间的间距。
请注意,对于 nav
,请应用 vertical-align: top
,否则您会在基线上方或下方得到一些额外的空格。
此外,如果您为 header
指定 height
,还指定 line-height
以便内联子元素垂直居中(如果这是您想要的)。
header {
height: 60px;
line-height: 60px;
width: 85%;
margin: 0 auto;
border: 1px dotted blue; /* for demo only */
text-align: center; /* optional? */
white-space: nowrap; /* to prevent line from wrapping, optional */
}
header h1 {
display: inline-block;
border-right: 1px solid black;
margin: 0;
padding-left: 10px; /* adjust to taste */
padding-right: 10px; /* add whitespace before vertical border */
height: 100%;
}
header h1 a {
text-shadow: 1px 2px lightgrey;
}
header h1:hover a {
color: #f2f2f2;
}
header a {
text-decoration: none;
color: #333;
}
.button:hover {
background-color: #f2f2f2;
}
.active {
text-decoration: underline;
}
nav {
display: inline-block;
vertical-align: top;
padding-left: 10px; /* whitespace after vertical border */
font-weight: bold;
font-size: 1.3em;
border: 1px dotted gray; /* for demo only */
}
nav a {
margin: 0 20px;
}
<header>
<h1><a href="index.html">H1 TEXT HEREEEEE</a></h1>
<nav>
<a href="index.html" class="button active">Home</a>
<a href="profil.html" class="button">About</a>
<a href="mdu.html" class="button">MDU</a>
</nav>
</header>