gpt4 book ai didi

html - Middle Div 不会位于其他 div 的中心

转载 作者:搜寻专家 更新时间:2023-10-31 23:22:49 25 4
gpt4 key购买 nike

我正在尝试创建我的 header。其中包括 Logo (左)、横幅(中)和导航栏(右)。我可以让它们彼此齐平,但无法让横幅完全位于页面中央。

横幅被推向右侧,占据屏幕大约 70% 的位置。

关于为什么会发生这种情况有什么想法吗?

#header1 {
position: absolute;
width: 100%;
height: 100px;
text-align: center;
margin: 0px;
padding-top: 2px;
padding-right: 2px;
padding-left: 2px;
padding-bottom: 2px;
color: white;
font-size: 18px;
background-image: url(content/structure/fmabannerbehind.jpg);
background-position: center;
font-family: Constantia, "Lucida Bright", "DejaVu Serif", Georgia, serif;
font-style: italic;
}
#logo {
margin-left: 0px;
height: 100px;
width: 200px;
float: left;
border-color: red;
border-width: 3px;
border-style: solid;
}
#logo img {
height: 100px;
width: auto;
}
#banner {
position: absolute;
height: 100px;
width: auto;
display: inline-block;
margin: 0 auto;
border-color: red;
border-width: 3px;
border-style: solid;
}
#banner img {
height: 100px;
width: auto;
}
#menu1 {
margin-right: 0px;
height: 100px;
width: auto;
float: right;
border-color: red;
border-width: 3px;
border-style: solid;
}
<div id="header1">
<div id="logo">
<a href="http://www.fmarchived.com">
<img src="/content/structure/logo3.jpg" alt="logo">
</a>
</div>
<div id="banner">
<a href="/">
<img src="/content/structure/fmabanneronly.jpg" alt="banner">
</a>
</div>
<div id="menu1">
<div class="child2menu">
<ul>
<li><a href="/aboutus/">About Us</a>
</li>
<li><a href="https://www.facebook.com/fmarchived/">Facebook</a>
</li>
<li><a href="https://twitter.com/fmarchived">Twitter</a>
</li>
<li><a href="https://www.youtube.com/channel/UCOA0NFV-DuoqCRFT3YzBzRg">Youtube</a>
</li>
</ul>
</div>
</div>
</div>

最佳答案

更新

Perhaps I should have explained better, I want the banner to be center of the entire page, regardless of what the other divs are doing

然后将position:relative添加到#header1position:absolute + display: inline-table; position:absolute; left:0; right:0; margin:auto;#banner两个片段

选项#1 - 使用 CSS flexbox

使用 flexbox 中的 justify-content: space-between 你可以做到这一点,并大大简化你的代码


* {
box-sizing: border-box;
margin: 0
}
#header1 {
display: flex;
justify-content: space-between;
position: relative
}
#header1 > div {
border: solid red
}
img {
vertical-align: bottom
}
#banner {
display: inline-table;
position: absolute;
left: 0;
right: 0;
margin: auto;
}
<div id="header1">
<div id="logo">
<a href="http://www.fmarchived.com">
<img src="//lorempixel.com/200/100" alt="logo">
</a>
</div>
<div id="banner">
<a href="/">
<img src="//lorempixel.com/100/100" alt="banner">
</a>
</div>
<div id="menu1">
<div class="child2menu">
<ul>
<li><a href="/aboutus/">About Us</a>
</li>
<li><a href="https://www.facebook.com/fmarchived/">Facebook</a>
</li>
<li><a href="https://twitter.com/fmarchived">Twitter</a>
</li>
<li><a href="https://www.youtube.com/channel/UCOA0NFV-DuoqCRFT3YzBzRg">Youtube</a>
</li>
</ul>
</div>
</div>
</div>

选项#2 - 使用您当前的代码

position:absolute#header1 更改为 relative 以删除滚动条


* {
box-sizing: border-box;
margin: 0
}
#header1 {
position: relative;
width: 100%;
height: 100px;
text-align: center;
margin: 0;
padding: 2px;
color: white;
font-size: 18px;
background-image: url(content/structure/fmabannerbehind.jpg);
background-position: center;
font-family: Constantia, "Lucida Bright", "DejaVu Serif", Georgia, serif;
font-style: italic;
}
#header1 > div {
border: 3px solid red
}
img {
height: 100px;
width: auto;
vertical-align:bottom
}
#logo {
margin-left: 0;
width: 200px;
float: left;
}
#banner {
width: auto;
display: inline-table;
position:absolute;
left:0;
right:0;
margin:auto;


}
#menu1 {
margin-right: 0px;
float: right;
}
<div id="header1">
<div id="logo">
<a href="http://www.fmarchived.com">
<img src="//lorempixel.com/100/100" alt="logo">
</a>
</div>
<div id="banner">
<a href="/">
<img src="//lorempixel.com/100/100" alt="banner">
</a>
</div>
<div id="menu1">
<div class="child2menu">
<ul>
<li><a href="/aboutus/">About Us</a>
</li>
<li><a href="https://www.facebook.com/fmarchived/">Facebook</a>
</li>
<li><a href="https://twitter.com/fmarchived">Twitter</a>
</li>
<li><a href="https://www.youtube.com/channel/UCOA0NFV-DuoqCRFT3YzBzRg">Youtube</a>
</li>
</ul>
</div>
</div>
</div>

关于html - Middle Div 不会位于其他 div 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37395613/

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