gpt4 book ai didi

html - 内联 h3 将 ul 向右移动

转载 作者:行者123 更新时间:2023-11-28 08:05:17 25 4
gpt4 key购买 nike

我希望导航栏居中,文本按原样放置在左侧,但是当我添加 h3 标签时,导航栏向右移动。我该怎么做?

html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#toptext {
margin: 5px 0px 0px 10px;
padding: 0;
display: inline-block;
float: left;
font-style: bold;
font-size: 2em;
color: white;
}
.nav ul {
list-style: none;
background-color: #444;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
font-family: 'Oswald', sans-serif;
font-size: 1.2em;
line-height: 40px;
text-align: left;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
padding-left: 15px;
border-bottom: 1px solid #888;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #aaa;
color: #444;
cursor: default;
}
/* Sub Menus */

.nav li li {
font-size: .8em;
}
/*******************************************
Style menu for larger screens

Using 650px (130px each * 5 items), but ems
or other values could be used depending on other factors
********************************************/

@media screen and (min-width: 650px) {
.nav li {
width: 130px;
border-bottom: none;
height: 50px;
line-height: 50px;
font-size: 1em;
display: inline-block;
margin-right: -4px;
}
.nav a {
border-bottom: none;
}
.nav > ul > li {
text-align: center;
}
.nav > ul > li > a {
padding-left: 0;
}
/* Sub Menus */
.nav li ul {
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul {
display: block;
}
.nav li ul li {
display: block;
}
}
<div class="nav">
<h3 id="toptext">Text text text text</h3>
<ul>
<li class="home"><a class="active" href="#">Home</a>
</li>
<li class="about"><a href="About">About</a>
</li>
<li><a href="#">Other</a>
<ul>
<li><a href="site1.html">Site1</a>
</li>
<li><a href="site2.html">Site2</a>
</li>
</ul>
</li>
<li class="contact"><a href="contact.html">Contact</a>
</li>
</ul>
</div>

我使用的导航栏来自:http://css-snippets.com/drop-down-navigation/

最佳答案

文档流中的任何内容都会影响菜单的位置。因此,您必须通过绝对定位将 h3 移出流程。

#toptext {
margin: 5px 0px 0px 10px;
padding: 0;
font-style: bold;
font-size: 2em;
color: white;
position: absolute;
}

这还有其他问题,但可以解决您最初的问题。

html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
#toptext {
margin: 5px 0px 0px 10px;
padding: 0;
/*
display: inline-block;
float: left;
*/
font-style: bold;
font-size: 2em;
color: white;
position: absolute;
}
.nav ul {
list-style: none;
background-color: #444;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
font-family: 'Oswald', sans-serif;
font-size: 1.2em;
line-height: 40px;
text-align: left;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
padding-left: 15px;
border-bottom: 1px solid #888;
transition: .3s background-color;
}
.nav a:hover {
background-color: #005f5f;
}
.nav a.active {
background-color: #aaa;
color: #444;
cursor: default;
}
/* Sub Menus */

.nav li li {
font-size: .8em;
}
/*******************************************
Style menu for larger screens

Using 650px (130px each * 5 items), but ems
or other values could be used depending on other factors
********************************************/

@media screen and (min-width: 650px) {
.nav li {
width: 130px;
border-bottom: none;
height: 50px;
line-height: 50px;
font-size: 1em;
display: inline-block;
margin-right: -4px;
}
.nav a {
border-bottom: none;
}
.nav > ul > li {
text-align: center;
}
.nav > ul > li > a {
padding-left: 0;
}
/* Sub Menus */
.nav li ul {
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul {
display: block;
}
.nav li ul li {
display: block;
}
}
<div class="nav">
<h3 id="toptext">Text text text text</h3>

<ul>
<li class="home"><a class="active" href="#">Home</a>

</li>
<li class="about"><a href="About">About</a>

</li>
<li><a href="#">Other</a>

<ul>
<li><a href="site1.html">Site1</a>

</li>
<li><a href="site2.html">Site2</a>

</li>
</ul>
</li>
<li class="contact"><a href="contact.html">Contact</a>

</li>
</ul>
</div>

关于html - 内联 h3 将 ul 向右移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29517631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com