gpt4 book ai didi

html - 在容器内垂直居中 div

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

我正在尝试将我的 Logo 的 div 在我的导航标题中垂直居中。我已经研究过关于 stackoverflow 的其他主题,但没有一个能给我一个好的解决方案。你们谁能给我一个好的响应解决方案

调查:How to vertically center a div for all browsers?这并没有解决我的问题。

目标是让左侧带有 logo 的 div 类垂直居中,右侧带有 nav 的 div 类垂直居中。

片段:

html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
font-size: 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, Sans-Serif;
vertical-align: baseline;
outline: none;
}

#header {
background: #00968B;
height: 58px;
}

.logo {
height: 58px;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div id="header">
<div class="container">
<div class="logo">
logo
</div>
<div class="nav">
</div>
</div>
</div>
</body>
</html>

最佳答案

Flexbox 将允许您将元素垂直居中 align-items: center; 并在导航和 Logo 之间添加间距 justify-content: space-between;,添加 flex-flow: row wrap; 将允许元素在标题元素接触时换行。

#header {
background: #00968B;
height: 58px;
}
.logo {
height: 58px;
background: orange;
}
.nav {
width: 30%;
background: yellow;
height: 30px;
}

.container {
display: flex;
flex-flow: row wrap;
align-items: center;
justify-content: space-between;
}
<div id="header">
<div class="container">
<div class="logo">
logo
</div>
<div class="nav">
nav
</div>
</div>
</div>

关于html - 在容器内垂直居中 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38962908/

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