求助:实现Header右侧图片水平排列且文字居中于图片下方
问题解决:Header右侧图标水平排列+文字居中显示
你的代码核心问题出在全局样式影响和布局细节上,直接调整以下几处就能实现目标效果:
修改后的HTML(仅给logo添加专属类)
<div class="container"> <div class="header"> <img class="header-logo" src="siteImages/siteLogo.png"> <div class="header-center"> <div class="headerLinks"> <a class="active" href="#home">HOME</a> <a href="#contact">GAS & CENTRAL HEATING</a> <a href="#about">SWIMMING POOLS</a> <a href="#about">AIR CONDITIONING</a> <a href="#about">SWIMMING POOLS</a> </div> <div class="socials"> <a href="https://www.facebook.com/#LINK"><img src="siteImages/faceBook.png"></a> <a href="https://www.instagram.com/#LINK/"><img src="siteImages/instagram.jpg"></a> <a href="https://www.tiktok.com/@#LINK"><img src="siteImages/tiktok.png"></a> </div> </div> <div class="header-right"> <div class="contactIcons"> <img src="siteImages/whatsApp.jpg"> <span class="contactText">WhatsApp - Changed</span> </div> <div class="contactIcons"> <img src="siteImages/telePhone.jpg"> <span class="contactText">#TELE - Changed</span> </div> </div> </div> </div>
修改后的CSS(关键改动已标注)
body{ background: white; font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, "sans-serif"; margin: 0px; width: 100%; height: 100%; } .container .headerMain{ } .headerMain .HeaderLogo{ } .headerMain .HeaderLinks{ } .headerMain .HeaderContact{ } /*HEADER SECTION */ .header { overflow: hidden; background-color: black; padding: 10px 10px; } /* Style the header links */ .headerLinks a { float: left; color: white; text-align: center; padding: 15px; text-decoration: none; font-size: 22px; line-height: 25px; border-radius: 4px; margin-top: 35px; } .header a.logo { font-size: 25px; font-weight: bold; max-width:150px; max-height:150px; } /* 只给logo设置浮动,避免影响右侧联系图标 */ .header-logo { float: left; max-width:150px; max-height:150px; } .header-center { float: left; margin:0px; padding:0px; } .headerLinks{ margin-left:20px; margin-bottom:0px; padding: 0px; } .headerLinks a:hover { background-color: #13628d; color: white; } .headerLinks a.active { background-color: #13628d; color: white; } .socials{ float: right; margin:0px; padding: 0px 0px 0px 15px; } .socials img{ float:right; max-width:40px; max-height:40px; padding: 0px 0px 0px 15px; } .socials a:hover{ padding: 0px; } .header-right { float: right; display: flex; /* 让两个图标区块均匀分布 */ justify-content: space-around; width: 400px; align-items: center; } .contactHeader{ font-size: 22px; color: #13628d; width: 300px; } .contactIcons { /* 用flex垂直排列图标和文字,自动居中 */ display: flex; flex-direction: column; align-items: center; text-align: center; width: 45%; } .contactIcons img { max-width: 75px; max-height: 75px; } .contactIcons .contactText { font-size: 20px; color: #13628d; /* 确保文字换行也保持居中 */ width: 100%; }
改动说明:
- 给logo添加专属类,取消全局
.header img的浮动设置,避免干扰右侧联系图标布局 - 将
.contactIcons改为flex垂直布局,实现图标在上、文字在下的居中排列 - 调整
.header-right的justify-content属性,让两个联系区块均匀分布在右侧区域 - 给
.contactText设置100%宽度,确保文字过长换行时依然保持居中
内容的提问来源于stack exchange,提问作者Earunder
相关产品推荐
相关产品推荐

