gpt4 book ai didi

html - 将导航栏中的 IMG 元素居中

转载 作者:行者123 更新时间:2023-11-28 02:27:11 26 4
gpt4 key购买 nike

大家好 我想问一下如何将导航栏中的图像元素居中,当我尝试使用 absolute 时,我无法使用 ul 元素工作以及..我想让它发生的就像这里附上的图片一样。谢谢

logo

这是代码,我在这里使用了网格系统

body,
html {
padding: 0;
margin: 0;
height: 100%;
}


/* ########## Custome Design ######### */

.mainbox {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(12, 1fr);
/* margin: 10px 0; */
height: 100vh;

}

.box{
background-image: linear-gradient(to bottom,
rgba(93, 173, 226, 0.800),
rgba(93, 173, 226, 0.932) ),
url(/img/bg-picture.jpg);
background-size: cover;
background-position: left;
height: 100vh;
}

header {
grid-row: 1 / 2;
grid-column: 1 / -1;
/* background-color: #fff; */
}

.logo {
height: 65px;
width: 65px;
position: absolute;
margin: auto;
}

.firstNav {
text-align: center;
}

.firstNav > a {
font-family: 'Montserrat', sans-serif;
font-weight: 600;
font-size: 14px;
text-transform: uppercase;
text-decoration: none;
padding: 0 12px;
color: #fff;
}

.firstNav > a:hover {
background: #000;
}
<body>
<div class="mainbox box">
<header>
<nav>
<div class="firstNav">
<a href="#">Home</a>
<a href="#">Blog</a>
<a href="#">Portfolio</a>
<img src="./img/logo.png" alt="logo" class="logo">
<a href="#">Progress</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
</header>
</div>

</body>

当我尝试使用上面的代码时,结果就像我附上的下图一样

enter image description here

最佳答案

我使用 flexbox 解决了这个问题,替换了 .firstNav 中的 text-align:center; 并添加了一个 display: flex;

.firstNav {
display: flex;
justify-content: center;
align-items: center;
}

Here如果你想检查它,你有一个代码笔!让我知道是否有帮助!

关于html - 将导航栏中的 IMG 元素居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282989/

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