gpt4 book ai didi

html - 如何垂直对齐 img 旁边顶部导航栏中的 'email promo' 文本以及其他文本链接?

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

我一直在尝试学习 CSS 和 HTML,但在一些事情上遇到了困难。我似乎可以理解如何使用 CSS 在页面上定位元素?我需要垂直对齐顶部导航栏内的元素,但似乎终究无法弄清楚。我不只是在寻找一个简单的答案,我还想解释一下我做错了什么,这使我无法将元素移动到我需要的地方:)任何帮助将不胜感激。这是代码和 js fiddle 链接 https://jsfiddle.net/9trux138/2/

body {
margin: 0;
}

.topbar {
background-color: #17D8C7;
height: 35px;
overflow: hidden;
align-items: center;
padding: 0 2% 0 2%;
}

.topbar {
display: flex;
justify-content: space-between;
}

.topbar ul {
margin: 0;
padding-left: 0;
list-style: none;
position: relative;
display: inline-block;
}

.topbar ul li {
display: inline-block;
vertical-align: middle;
}

.topbar ul li a {
display: inline-block;
text-align: center;
vertical-align: middle;
text-decoration: none;
}

.topbar ul li img {
height: 24px;
padding: 5px 5px 0px 0px;
}

.topright li {
vertical-align: middle;
}
<!DOCTYPE html>
<html lang="en-gb">

<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Complete Suites</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body class="body">
<header class="topbar">
<ul class="topleft">
<li>
<a href="#"><img src="https://s8.postimg.cc/6t7njymhx/location-pin.png
" alt="Location Pin"></a>
</li>
<li>
<a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Contact Email"></a>
</li>
<li>
<a href="#"><img src="https://s8.postimg.cc/fbh3oalat/open-time.png
" alt="Opening Times"></a>
</li>
</ul>
<ul class="topmiddle">
<li>
<a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Email Promotions Sign Up"><span>Email Promotions</span></a>
</li>
</ul>
<ul class="topright">
<li><a href="#">Blog</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>


</body>

</html>

詹姆斯

最佳答案

从 img 标签中移除 padding-top 它会在我们使用 vertical-align: middle; 时阻止默认居中。

您还需要在 anchor 内的 span 和 img 标签上添加 vertical-align: middle,而不是 anchor 。

.topbar ul li a span {
vertical-align: middle;
}
.topbar ul li img {
height: 24px;
padding: 0 5px 0px 0px;
vertical-align: middle;
}

body {
margin: 0;
}

.topbar {
background-color: #17D8C7;
height: 35px;
overflow: hidden;
align-items: center;
padding: 0 2% 0 2%;
}

.topbar {
display: flex;
justify-content: space-between;
}

.topbar ul {
margin: 0;
padding-left: 0;
list-style: none;
position: relative;
display: inline-block;
}

.topbar ul li {
display: inline-block;
vertical-align: middle;
}

.topbar ul li a {
display: inline-block;
text-align: center;
text-decoration: none;
}
.topbar ul li a span {
vertical-align: middle;
}
.topbar ul li img {
height: 24px;
padding: 0 5px 0px 0px;
vertical-align: middle;
}
<!DOCTYPE html>
<html lang="en-gb">

<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Complete Suites</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body class="body">
<header class="topbar">
<ul class="topleft">
<li><a href="#"><img src="https://s8.postimg.cc/6t7njymhx/location-pin.png
" alt="Location Pin"></a></li>
<li><a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Contact Email"></a></li>
<li><a href="#"><img src="https://s8.postimg.cc/fbh3oalat/open-time.png
" alt="Opening Times"></a></li>
</ul>
<ul class="topmiddle">
<li><a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Email Promotions Sign Up"><span>Email Promotions</span></a></li>
</ul>
<ul class="topright">
<li><a href="#">Blog</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>


</body>

</html>

关于html - 如何垂直对齐 img 旁边顶部导航栏中的 'email promo' 文本以及其他文本链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225607/

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