简体   繁体   English

如何在我的情况下将垂直对齐设置为中间?

[英]How to set vertical align to middle in my case?

I am trying to modify htmls element to achieve two things: 我正在尝试修改htmls元素以实现两件事:

  1. Vertical align two glyphicons and texts to middle. 将两个字形图标和文本垂直对齐到中间。 (vertical align middle doesn't work) (垂直对齐中间不起作用)

  2. Modify css to have a small gap between text "thanks" and glyphicons to become something like <icon><thanks><icon> instead of <icon> <thanks> <icon> 修改css,使其在文本“感谢”和字形<icon><thanks><icon>之间留出很小的间隙,以使其类似于<icon><thanks><icon>而不是<icon> <thanks> <icon>

I have created a jsfiddle: https://jsfiddle.net/vwdhd1wy/ 我创建了一个jsfiddle: https ://jsfiddle.net/vwdhd1wy/

It seems odd that it created two gap without any additional css. 它在没有任何额外的CSS的情况下创建了两个间隙,这似乎很奇怪。 Can someone help me about it? 有人可以帮我吗?

Working Code: https://jsfiddle.net/vwdhd1wy/4/ 工作代码: https//jsfiddle.net/vwdhd1wy/4/

For 1. Vertical align two glyphicons and texts to middle. 对于1.将两个字形图标和文本垂直对齐到中间。 (vertical align middle doesn't work). (垂直对齐中间不起作用)。

You can really tidy up the CSS as you don't want repeated code. 您确实可以整理CSS,因为您不需要重复的代码。 Add vertical-align: top; 添加vertical-align: top; to all span elements to ensure they are all starting at the same position as inline-block elements next to each other can get messy depending on their contents. 到所有span元素,以确保它们都从相同的位置开始,因为相邻的inline-block元素可能会因内容而混乱。

There is some inherited code for .glyphicon which is setting line-height: 1 and top: 1px; .glyphicon有一些继承的代码,它设置line-height: 1top: 1px; so you need to override this with div > span.glyphicon { line-height: 30px; top: 0px; } 因此您需要使用div > span.glyphicon { line-height: 30px; top: 0px; } div > span.glyphicon { line-height: 30px; top: 0px; } div > span.glyphicon { line-height: 30px; top: 0px; } . div > span.glyphicon { line-height: 30px; top: 0px; } The rest is just setting height: 30px; 其余的只是设置height: 30px; and the line-height: 30px; line-height: 30px; (line-height for placing it in the middle). (将其放置在中间的行高)。

div > span {  
  display: inline-block;
  vertical-align: top;  
}

div > span, div > span.glyphicon { 
  height: 30px;
  line-height: 30px; 
  top: 0px;
}

.left, .right {
  background-color: red;  
}

.middle {
  background-color: grey; 
}

For 2. Modify css to have a small gap between text "thanks" and glyphicons to become something like instead of 对于2。修改css,使其在文本“ thanks”和glyphicons之间留出很小的间隙,从而变为

As @ryantdecker said, remove the whitespace after all the span elements to remove the extra space that's showing. 正如@ryantdecker所说,在所有span元素之后删除空格,以删除显示的多余空间。 You get this whitespace due to it being treated as an "inline" (text) element when set to inline-block so whenever there is a space between inline elements, it also includes that when rendering the HTML. 之所以得到此空格,是因为将其设置为inline-block时将其视为“ inline”(文本)元素,因此,只要inline元素之间存在空格,则在呈现HTML时也会包含该空格。

Solution: 解:

<span class="left glyphicon glyphicon-forward"></span><span class="middle">Thansk</span><span class="right glyphicon glyphicon-backward"></span>

This work for you 为你工作

   <div>
       <span class="left glyphicon glyphicon-forward"></span>
       <span class="middle">Thansk</span>
       <span class="right glyphicon glyphicon-backward"></span>
    </div>

And style like this 像这样的风格

  .left {
      background-color: red;
      height: 30px;
    }

    .middle {
      height: 30px;
      display:inline-block;
      background-color: grey;
      vertical-align:middle;
    }

    .right {
      background-color: red;
        height: 30px;
    }
    span {
       align-items: center;
       display: inline-flex !important;
       flex-wrap: wrap;
    } 

Please use this HTML/CSS 请使用此HTML / CSS

<div class="mid">
   <span class="left glyphicon glyphicon-forward"></span>
   <span class="middle">Thansk</span>
   <span class="right glyphicon glyphicon-backward"></span>
</div>


    .mid{display:table;}
    .left {
      background-color: red;
      height: 30px;
      vertical-align:middle;
       display:table-cell;
    }

    .middle {
      height: 30px;
      display:table-cell;
      background-color: grey;
      vertical-align:middle;
    }

    .right {
      background-color: red;
        height: 30px;
        vertical-align:middle;
         display:table-cell;
    }

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

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