<ul>元素未与DIV

时间:2018-03-23 19:33:25

标签: html css

我正在尝试为网页制作标题,我希望网页的标题(屏幕的最左侧)与屏幕最右侧的元素垂直居中。我认为将float: left;应用于“title”div会解决此问题,但事实并非如此。

我已经包含了代码&amp;片段向您展示我的意思。我还为每个元素添加了一个box-shadow,以便您可以更好地将它们可视化。

* {
  box-shadow: inset 0px 0px 1px rgba(255, 255, 255, 0.3)
}

* {
  margin: 0;
  padding: 0;
}

.nav>ul>li {
  display: inline-block;
  padding: 0 25px 0 25px;
  color: #ffffff;
  font-family: Roboto Slab;
  font-size: 14px;
  font-weight: bold;
}

.nav>ul>li:hover {
  color: #cccccc;
}

.nav>ul {
  text-align: right;
  list-style: none;
  padding: 20px 0px 20px 0px;
}

.nav {
  background-color: #111111;
}

.nav>.title>p {
  color: #ffffff;
  font-size: 22px;
  font-family: Roboto Slab;
  font-size: 22px;
  font-weight: bold;
  float: left;
  padding-left: 25px;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab|Roboto+Slab:bold,italic,underline">

<div class="nav">
  <div class="title">
    <p>Title</p>
  </div>
  <ul>
    <li>Home</li>
    <li>About</li>
    <li>Forums</li>
    <li>Contact</li>
  </ul>
</div>

4 个答案:

答案 0 :(得分:0)

使用line-height css属性设置行使用的空间量。

.nav > .title > p {
    color: #ffffff;
    font-size: 22px;
    font-family: Roboto Slab;
    font-size: 22px;
    font-weight: bold;
    float: left;
    padding-left: 25px;
    text-align: center;
    line-height:2.5;      // add this line 
}

我认为这会对你有帮助。

答案 1 :(得分:0)

&#13;
&#13;
* {box-shadow: inset 0px 0px 1px rgba(255,255,255,0.3)}
* {
	margin: 0;
    padding: 0;
}
.nav > ul > li {
	display: inline-block;
	padding: 0 25px 0 25px;
	color: #ffffff;
	font-family: Roboto Slab;
	font-size: 14px;
	font-weight: bold;
}
.nav > ul > li:hover {
	color: #cccccc;
}
.nav > ul {
	text-align: right;
	list-style: none;
	padding: 20px 0px 20px 0px;
}
.nav {
	background-color: #111111;
}
.nav > .title > p {
	color: #ffffff;
	font-size: 22px;
	font-family: Roboto Slab;
	font-size: 22px;
	font-weight: bold;
	float: left;
    padding: 15px;
	text-align: center;
}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
<title>Website</title>
<script rel="javascript" src="Website.js"></script>
<link type="text/css" rel="stylesheet" href="Website.css"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab|Roboto+Slab:bold,italic,underline">
</head>
<body>
<div class="nav">
<div class="title">
<p>Title</p>
</div>
	<ul>
		<li>Home</li>
		<li>About</li>
		<li>Forums</li>
		<li>Contact</li>
	</ul>
</div>

</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

提供p element of title class a margin of 15px on top

希望这个更新的代码适合您。

* {box-shadow: inset 0px 0px 1px rgba(255,255,255,0.3)}
* {
	margin: 0;
    padding: 0;
}
.nav > ul > li {
	display: inline-block;
	padding: 0 25px 0 25px;
	color: #ffffff;
	font-family: Roboto Slab;
	font-size: 14px;
	font-weight: bold;
}
.nav > ul > li:hover {
	color: #cccccc;
}
.nav > ul {
	text-align: right;
	list-style: none;
	padding: 20px 0px 20px 0px;
}
.nav {
	background-color: #111111;
}
.nav > .title > p {
	color: #ffffff;
	font-size: 22px;
	font-family: Roboto Slab;
	font-weight: bold;
	float: left;
	padding-left: 25px;
	margin-top: 15px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Website</title>
<script rel="javascript" src="Website.js"></script>
<link type="text/css" rel="stylesheet" href="Website.css"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab|Roboto+Slab:bold,italic,underline">
</head>
<body>
<div class="nav">
<div class="title">
<p>Title</p>
</div>
	<ul>
		<li>Home</li>
		<li>About</li>
		<li>Forums</li>
		<li>Contact</li>
	</ul>
</div>

</body>
</html>

答案 3 :(得分:0)

你必须只给出.nav以下的风格。请确保您还使用flex的用户前缀; 您还可以通过.titleul display:inline-block以及vertical-align:middle

来制作垂直对齐中心
.nav {
  background-color: #111111;
  display:flex;
  justify-content: space-between;
  align-items: center;
}

&#13;
&#13;
* {
  box-shadow: inset 0px 0px 1px rgba(255, 255, 255, 0.3)
}

* {
  margin: 0;
  padding: 0;
}

.nav>ul>li {
  display: inline-block;
  padding: 0 25px 0 25px;
  color: #ffffff;
  font-family: Roboto Slab;
  font-size: 14px;
  font-weight: bold;
}

.nav>ul>li:hover {
  color: #cccccc;
}

.nav>ul {
  text-align: right;
  list-style: none;
  padding: 20px 0px 20px 0px;
}

.nav {
  background-color: #111111;
  display:flex;
  justify-content: space-between;
  align-items: center;
}

.nav>.title>p {
  color: #ffffff;
  font-size: 22px;
  font-family: Roboto Slab;
  font-size: 22px;
  font-weight: bold;
  float: left;
  padding-left: 25px;
  text-align: center;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab|Roboto+Slab:bold,italic,underline">

<div class="nav">
  <div class="title">
    <p>Title</p>
  </div>
  <ul>
    <li>Home</li>
    <li>About</li>
    <li>Forums</li>
    <li>Contact</li>
  </ul>
</div>
&#13;
&#13;
&#13;