简体   繁体   English

如何在范围内垂直对齐这些元素?

[英]How to vertically align these elements inside a span?

Here is the prototype I am trying to implement. 这是我尝试实现的原型。 在此处输入图片说明

Here is what I have right now 这是我现在所拥有的

在此处输入图片说明

What I am trying to do right now is vertically align all the elements( span s with image and text) - My Account, Cards, Travel, etc.. 我现在想做的是垂直对齐所有元素( span与图像和文本)-我的帐户,卡,旅行等。

Here is my HTML and CSS for this section 这是本节的HTML和CSS

HTML: HTML:

   <div id="header">
      <img class ="header_component" src="Logo.PNG" />
      <span class="header_component">
        <img src="MyAccount.PNG"/><BR/>
        My Account
      </span>
      <span class="header_component">
        <img src="Cards.PNG"/> <BR />
        Cards
      </span>
      <span class="header_component">
        <img src="Cards.PNG"/> <BR />
        Travel
      </span>
      <span class="header_component">
        <img src="Rewards.PNG"/> <BR />
        Rewards
      </span>
      <span class="header_component">
        <img src="Business.PNG"/> <BR />
        Business
      </span>
    </div>

CSS: CSS:

.header_component {
    width: 12%;
    float:left;
    text-align: center;
    vertical-align: middle; 
}

I tried applying the vertical-align: middle attribute I learned from Vertical Alignment but that didn't do the job(elements not vertically aligned in span ). 我尝试应用了vertical-align: middle属性,该属性是我从Vertical Alignment中学到的,但没有完成这项工作(元素未在span垂直对齐)。

Does anyone know of any alternative styles that could work? 有谁知道其他可行的样式吗?

I've restructured slightly to add a wrapper around the contents of each item (for centering) and change float to inline-block to the header item. 我已经进行了稍微的重组,为每个项目的内容添加了一个包装器(用于居中),并将float更改为inline-block到标题项目。 white-space: nowrap on the parent fixes line breaking issues. white-space: nowrap父级上的white-space: nowrap修复了换行问题。

 #header { white-space: nowrap; } .header_component { text-align: center; white-space: nowrap; display: inline-block; vertical-align: middle; width: 100px; height: 100px; } .header_component div { max-width: 100%; max-height: 100%; margin-top: 50%; transform: translateY(-50%); } 
 <div id="header"> <img class="header_component" src="Logo.PNG" /> <span class="header_component"> <div class="inner"> <img src="MyAccount.PNG"/><br /> My Account </div> </span> <span class="header_component"> <div class="inner"> <img src="Cards.PNG"/><br /> Cards </div> </span> <span class="header_component"> <div class="inner"> <img src="Cards.PNG"/><br /> Travel </div> </span> <span class="header_component"> <div class="inner"> <img src="Rewards.PNG"/><br /> Rewards </div> </span> <span class="header_component"> <div class="inner"> <img src="Business.PNG"/><br /> Business </div> </span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM