gpt4 book ai didi

HTML/CSS : Keep my logo at equal distance from the top and bottom of my navbar

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

我的问题

我试图将我的 Logo 保持在导航栏的左侧,并使其与导航栏的顶部和底部保持相等的距离。 (在提供的图片中解释)

HTML:

<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<div class="navbar">
<div id="logo-nav" class="col-4">
<a href="/" title="Home">
<img class="logo-nav" src="images/logo-2.png" alt="My Logo">
</a>
</div>
</div>
</div>
</body>

CSS:

.container {
max-width :100%;
margin: auto;
}

.navbar{
width: 100%;
height: 70px;
background-color: #181827;
}

.logo-nav{
position: absolute;
margin:auto ;
}

.row {
width:100%;
height: 160px;
}

.col-12 {
width:100%;
}

.col-8 {
width:66.6%;
}

.col-6 {
width:50%
}

.col-4 {
width:33.3%;
}

说明

红色框是我的 Logo 当前所在的位置,它似乎卡在导航栏的顶部。

绿色框是我想要的位置 - 在导航栏的中间:与底部和顶部的距离相等(不是从左到右,我希望它像现在一样粘在左边)

所以我尝试在线研究它,这让我尝试了

.logo-nav{
position: absolute;
margin:auto ;

但不幸的是它并没有给我想要的结果。这就是我来这里的原因

我认为我理解错了 positionmargin 是如何工作的。

所以我的问题是:我如何从红框转到绿框? (我的代码出了什么问题?)

最重要的是

感谢您的输入:)

最佳答案

试试这个变体:

.navbar{
width: 100%;
height: 70px;
background-color: #181827;
position: relative;
}

.logo-nav{
position: absolute;
top: 50%;
transform: translateY(-50%);
}

关于HTML/CSS : Keep my logo at equal distance from the top and bottom of my navbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958410/

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