我正在尝试创建我的header
。其中包括徽标(左),横幅(中)和导航栏(右)。我可以让他们彼此保持水平,但不能让横幅完全坐在页面的中央。
横幅向右推,横过屏幕大约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>
&#13;
答案 0 :(得分:2)
<强>更新强>
也许我应该更好地解释,我希望横幅是中心 整个页面,无论其他div正在做什么
然后将position:relative
添加到#header1
和position:absolute
+ display: inline-table;
position:absolute;
left:0;
right:0;
两个片段中的margin:auto;
到#banner
使用justify-content: space-between
中的flexbox
,您可以执行此操作,并简化您的代码
* {
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;
}
&#13;
<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>
&#13;
将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;
}
&#13;
<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>
&#13;
答案 1 :(得分:1)
从#banner div中删除position: absolute
属性。如果#banner div由于某种原因需要绝对定位,您可以通过给它一个宽度和相应的left
属性来使其居中。
答案 2 :(得分:1)
只需移除绝对位置,它就会与中心对齐。
#banner {
/*position:absolute;*/
height: 100px;
width: auto;
display: inline-block;
margin:0 auto;
border-color: red;
border-width: 3px;
border-style: solid;
}
答案 3 :(得分:0)
你可以使用coordonates和margin:auto;如果使用绝对的话,使你的盒子居中。但你需要重置显示。
#banner {
position: absolute;
height: 100px;
width: auto;
display: table;/* shrinks on content */
left:0;
right:0;
margin: 0 auto;/* will center in between coordonates if boxe smaller */
border-color: red;
border-width: 3px;
border-style: solid;
}
运行代码片段:
#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;
background:linear-gradient(to right,gray 50%, lightgray 50%);
}
#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: table;
margin: 0 auto;
left:0;
right:0;
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>