gpt4 book ai didi

html - 为什么此 Logo 未在其父容器内垂直居中?

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:21 24 4
gpt4 key购买 nike

此页面中的 Logo 元素未在 <header> 中垂直居中容器。该问题在移动设备上比在桌面设备上更为明显。第二个元素 ( #forum-link ) 正确对齐。

flex 盒 align-items:center规则似乎适用于一个子 div 但不适用于另一个。为什么会这样?如何解决?

  html {
max-width: 780px;
margin: 0 auto;
}

body {
background-image: url('https://img.freepik.com/free-vector/retro-styled-pattern-background_1048-6593.jpg');
background-size: 116px;
background-repeat: repeat-x;
}

header {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
height:116px;
}

#logo {
margin-left: 15px;
}

#forum-link {
max-width: 110px;
margin-right: 35px;
}

#forum-link a {
color: white;
text-decoration: none;
font-weight: bold;
font-size: x-large;
}

@media only screen and (orientation: portrait) {
html {
margin: 0;
height: 100%;
}
body {
margin: 0;
height: 100%;
display: flex;
flex-flow: column;
}
header {
display: block;
width: 100%;
position: relative;
height: auto;
margin-bottom: 50px;
}
#logo {
margin: initial;
display: flex;
align-items: center;
justify-content: center;
}
#forum-link {
margin: initial;
max-width: initial;
background: #323232;
height: 27px;
position: absolute;
bottom: -50px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#forum-link a {
font-weight: bold;
font-size: .9em;
}
#forum-link a:hover {
text-decoration: underline;
}
<body>
<header>
<div id="logo"><img src="http://placekitten.com/g/354/85" srcset="http://placekitten.com/g/354/85, http://placekitten.com/g/354/85 2x" width="354" height="85"></div>
<div id="forum-link"><a href="/forum">Join our Forums!</a></div>
</header>
</body>

编辑:澄清了问题

最佳答案

嗯,我想这就是您想要的: Logo 和链接在背景上垂直居中?仅针对非移动解决方案更新。

此外,正如我在评论中所说,为全面起见,在此重复:您的图像不是垂直居中的,因为它是其父级的高度:#logoheader.

链接的高度小于 header,因此它是垂直居中的。

如果您指的是 5px 左右的空间,只需在 #logo 的图像上添加一个 display: block 即可删除该间距。不过,它仍将是其 parent 的高度。

我的解决方案基本上是给你的 body 一个高度, flex 它,然后你的 header 垂直居中对齐。

 html {
max-width: 780px;
margin: 0 auto;
}

body {
background-image: url('https://img.freepik.com/free-vector/retro-styled-pattern-background_1048-6593.jpg');
background-size: 116px;
background-repeat: repeat-x;
height: 116px;
margin: 0;
display: flex;
}

header {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
margin-bottom: 0;
width: 100%;
}

#logo {
margin-left: 15px;
}

#logo img {
display: block;
}

#forum-link {
max-width: 110px;
margin-right: 35px;
}

#forum-link a {
color: white;
text-decoration: none;
font-weight: bold;
font-size: x-large;
}

@media only screen and (orientation: portrait) {
html {
margin: 0;
height: 100%;
}
body {
margin: 0;
height: 100%;
display: flex;
flex-flow: column;
}
header {
display: block;
width: 100%;
position: relative;
height: auto;
margin-bottom: 50px;
}
#logo {
margin: initial;
display: flex;
align-items: center;
justify-content: center;
}
#forum-link {
margin: initial;
max-width: initial;
background: #323232;
height: 27px;
position: absolute;
bottom: -50px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#forum-link a {
font-weight: bold;
font-size: .9em;
}
#forum-link a:hover {
text-decoration: underline;
}
<body>
<header>
<div id="logo"><img src="http://placekitten.com/g/354/85" srcset="http://placekitten.com/g/354/85, http://placekitten.com/g/354/85 2x" width="354" height="85"></div>
<div id="forum-link"><a href="/forum">Join our Forums!</a></div>
</header>
</body>

关于html - 为什么此 Logo 未在其父容器内垂直居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763749/

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