这是我第一次使用 stackoverflow,也是第一次尝试开发网页请不要私刑我。我是一名网络工程师,从事一些编码工作。
我正在尝试构建一个基本的投资组合网页。页眉、导航、图像、内容和页脚。没什么特别的。我在让标题与导航对齐时遇到一些问题。在我将标题文本“Terry”更改为 oleo 字体之前一切正常。如果我将其保留为默认值,它会与导航文本对齐。
我该如何解决这个问题,以便我的 NAv 显示在我的标题中(它看起来像是被推下了)? 将两个 div 并排放置会更好吗(就像我在正下方对左列和右列所做的那样? 与使用花车有关吗?
任何建议都会很棒!
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
height: inherit;
}
#container {
width: 100%;
height: inherit;
background-color: green;
}
#nav li {
float: right;
font-family: Courier;
padding: 15px;
}
.navli {
display: inline;
list-style-type: none;
font-family: "Brush Script MT", cursive;
font-size: 24px;
font-style: normal;
font-variant: normal;
font-weight: bold;
line-height: 26.4px;
text-decoration: none;
}
a {
text-decoration: none;
}
#banner {}
#leftcolumn {
width: 40%;
background-color: yellow;
height: 75%;
float: left;
}
#rightcolumn {
width: 60%;
background-color: grey;
height: 75%;
float: right;
}
#header {
background-color: #6495ed;
width: 100%;
height: 20%;
float: left;
font: 200 100px/1.3 'Oleo Script', Helvetica, sans-serif;
color: white;
text-shadow: 4px 4px 0px rgba(0, 0, 0, 0.1);
}
#footer {
background-color: #6495ed;
height: 15%;
width: 100%;
}
<link href='http://fonts.googleapis.com/css?family=Oleo+Script' rel='stylesheet' type='text/css'>
<script src="C:\Users\nick.szilagyi\Desktop\PROJECT\JS\jquery-3.2.1.min.js"></script>
<script src="C:\Users\nick.szilagyi\Desktop\PROJECT\JS\jquery-ui-1.12.1.custom\jquery-ui.js"></script>
<div id="container">
<div id="header">
Terry
<div id="nav">
<ul class="navli">
<li><a href="#">Contact</a></li>
<li><a href="#">Teaching</a></li>
<li><a href="#">My research</a></li>
<li><a href="#">Home</a></li>
</ul>
</div>
</div>
<div id="leftcolumn">left</div>
<div id="rightcolumn">right</div>
<div id="footer">Footer</div>
</div>
最佳答案
你可以使用display: flex;证明内容:空格之间; align-items: center;
在标题上将名称和导航推到两侧并将它们垂直居中,同时保持它们在同一行上。然后不需要 float 你的导航或导航 li,所以我在导航 li 上使用 display: inline-block
并重新排序它们,这样它们在代码中的顺序与它们显示的顺序相同在页面上。
您可能还应该删除页眉上的 height: 20%
并让它成为自然高度,但您没有问过这个,所以我没有更改它。
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
height: inherit;
}
#container {
width: 100%;
height: inherit;
background-color: green;
}
#nav li {
font-family: Courier;
padding: 15px;
display: inline-block;
}
.navli {
list-style-type: none;
font-family: "Brush Script MT", cursive;
font-size: 24px;
font-style: normal;
font-variant: normal;
font-weight: bold;
line-height: 26.4px;
text-decoration: none;
}
a {
text-decoration: none;
}
#banner {}
#leftcolumn {
width: 40%;
background-color: yellow;
height: 75%;
float: left;
}
#rightcolumn {
width: 60%;
background-color: grey;
height: 75%;
float: right;
}
#header {
background-color: #6495ed;
width: 100%;
height: 20%;
font: 200 100px/1.3 'Oleo Script', Helvetica, sans-serif;
color: white;
text-shadow: 4px 4px 0px rgba(0, 0, 0, 0.1);
display: flex;
justify-content: space-between;
align-items: center;
}
#footer {
background-color: #6495ed;
height: 15%;
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='stylesheetterry.css' />
<link href='http://fonts.googleapis.com/css?family=Oleo+Script' rel='stylesheet' type='text/css'>
<script src="C:\Users\nick.szilagyi\Desktop\PROJECT\JS\jquery-3.2.1.min.js"></script>
</head>
<body>
<div id="container">
<div id="header">
<div>Terry</div>
<div id="nav">
<ul class="navli">
<li><a href="#">Home</a></li>
<li><a href="#">My research</a></li>
<li><a href="#">Teaching</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<div id="leftcolumn">left</div>
<div id="rightcolumn">right</div>
<div id="footer">Footer</div>
</div>
</body>
关于html - 标题和导航列表的 CSS 问题 - 不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43809872/