html - 为什么我的 <span> 不能垂直居中?

标签 html css

我已经尝试了所有方法,但似乎这个跨度不会执行我告诉它的任何事情。没有应用的属性改变它的垂直位置,但水平属性会。

我已遵循本指南中的所有建议:https://css-tricks.com/centering-css-complete-guide/

代码:

#header{
    height: 50px;
    width: 100% - 50px;
    padding: 0px 25px 0px 25px;
    background-color: #00A680;
}

#header-right-icon-div > img{
    height: 20px;
    display: inline;
    padding: 15px 10px 15px 10px;
}

#menu-join-button{
    
}
<div id="header">
    <div id="header-right" style="float: right;">
           <div id="header-right-icon-div">
               
               <img src="briefcase_icon.svg" alt="">
               
               <img src="notification_icon.svg" alt="">
               
               <img src="profile_icon.svg" alt="">
               
               <img src="search_icon.svg" alt="">
               
               <span id="menu-join-button">hello</span>
               
           </div>
       </div>
</div>

即使是顶部或底部边距和填充也不会影响它...但是侧边距会...

这里有什么问题,为什么这个 menu-join-button 有自己的想法?如何将跨度置于该绿色标题中?

最佳答案

垂直对齐中间方式:

* {
box-sizing: border-box;
}

#header{

    width: 100%;
    padding: 0px 25px 0px 25px;
    background-color: #00A680;
}
#header:after {
  content: '';
  display: table;
  clear: both;
}
#header-right{
     float:right;
    padding: 10px 25px 10px 25px;
    background-color: #00A680;
}

#header-right-icon-div > img{
   display:inline-block;
   vertical-align: middle;
   padding: 0px 10px 0px 10px;
}

#menu-join-button{

}
<div id="header">
<div id="header-right">
           <div id="header-right-icon-div">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <span id="menu-join-button">hello</span>
               
           </div>
  </div>
  </div>

flex 方式:

* {
box-sizing: border-box;
}

#header{
    width: 100%;
    padding: 0px 25px 0px 25px;
    background-color: #00A680;
    display: flex;
    justify-content: flex-end;
    align-items:center;
}

#header-right{
    padding: 10px 25px 10px 25px;
    background-color: #00A680;
 
}
#header-right-icon-div {
   display: flex;
    align-items:center;
}
#header-right-icon-div > img{
   padding: 0px 10px 0px 10px;
}
<div id="header">
<div id="header-right">
           <div id="header-right-icon-div">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <img src="http://via.placeholder.com/30x30" alt="">
               
               <span id="menu-join-button">hello</span>
               
           </div>
  </div>
  </div>

关于html - 为什么我的 <span> 不能垂直居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50416486/

相关文章:

c# - 在 WPF 中放入像 Html Float Resource 这样的图片

html - 背景大小 : cover doesn't cover the whole screen

java - 使用图像 JavaFX 创建图像边框

javascript - 将滚动条隐藏在固定定位的 div 后面

javascript - 当有大量数据需要提交时,form.submit()需要更多时间

javascript - Konva - 拖动时有条件地改变矩形颜色

html - 根据 div 对齐我的内容

Css - 扩展速记属性 - 什么是 border-left-color-ltr-source :physical

html - 行高在浏览器之间是可变的吗

html - 如何在 Wordpress 中添加指向硬编码图像的链接?