CSS vertical alignment text inside li

前端 未结 9 1500
甜味超标
甜味超标 2020-12-07 19:59

I am displaying number of boxes in a row with fix height and width, generated from

  • tags. now I need to align the text in the vertical center. The CSS vertical-a
  • 相关标签:
    9条回答
    • 2020-12-07 20:06

      line-height is how you vertically align text. It is pretty standard and I don't consider it a "hack". Just add line-height: 100px to your ul.catBlock li and it will be fine.

      In this case you may have to add it to ul.catBlock li a instead since all of the text inside the li is also inside of an a. I have seen some weird things happen when you do this, so try both and see which one works.

      0 讨论(0)
    • 2020-12-07 20:07

      In the future, this problem will be solved by flexbox. Right now the browser support is dismal, but it is supported in one form or another in all current browsers.

      Browser support: http://caniuse.com/flexbox

      .vertically_aligned {
      
          /* older webkit */
          display: -webkit-box;
          -webkit-box-align: center;
          -webkit-justify-content: center;
      
          /* older firefox */
          display: -moz-box;
          -moz-box-align: center;
          -moz-box-pack: center;
      
          /* IE10*/
          display: -ms-flexbox;
          -ms-flex-align: center;
          -ms-flex-pack: center;
      
          /* newer webkit */
          display: -webkit-flex;
          -webkit-align-items: center;
          -webkit-box-pack: center;
      
          /* Standard Form - IE 11+, FF 22+, Chrome 29+, Opera 17+ */
          display: flex;
          align-items: center;
          justify-content: center;
      }
      

      Background on Flexbox: http://css-tricks.com/snippets/css/a-guide-to-flexbox/

      0 讨论(0)
    • 2020-12-07 20:09

      However many years late this response may be, anyone coming across this might just want to try

      li {
          display: flex;
          flex-direction: row;
          align-items: center;
      }
      

      Browser support for flexbox is far better than it was when @scottjoudry posted his response above, but you may still want to consider prefixing or other options if you're trying to support much older browsers. caniuse: flex

      0 讨论(0)
    • There are no perfect answers provided here except Asaf's answer which doesn't provide any code nor any example, so I would like to contribute mine...

      Inorder to make vertical-align: middle; work, you need to use display: table; for your ul element and display: table-cell; for li elements and than you can use vertical-align: middle; for li elements.

      You don't need to provide any explicit margins, paddings to make your text vertically middle.

      Demo

      ul.catBlock{
          display: table;
          width:960px; 
          height: 270px; 
          border:1px solid #ccc; 
      }
      
      ul.catBlock li {
          list-style: none;
          display: table-cell; 
          text-align: center; 
          width:160px; 
          vertical-align: middle;
      }
      
      ul.catBlock li a { 
          display: block;
      }
      
      0 讨论(0)
    • 2020-12-07 20:18

      As explained in here: https://css-tricks.com/centering-in-the-unknown/.

      As tested in the real practice, the most reliable yet elegant solution is to insert an assistent inline element into the <li /> element as the 1st child, which height should be set to 100% (of its parent’s height, the <li />), and its vertical-align set to middle. To achieve this, you can put a <span />, but the most convenient way is to use li:after pseudo class.

      Screenshot: enter image description here

      ul.menu-horizontal {
          list-style-type: none;
          margin: 0;
          padding: 0;
          display: inline-block;
          vertical-align: middle;
      }
      
      ul.menu-horizontal:after {
          content: '';
          clear: both;
          float: none;
          display: block;
      }
      
      ul.menu-horizontal li {
          padding: 5px 10px;
          box-sizing: border-box;
          height: 100%;
          cursor: pointer;
          display: inline-block;
          vertical-align: middle;
          float: left;
      }
      
      /* The magic happens here! */
      ul.menu-horizontal li:before {
          content: '';
          display: inline;
          height: 100%;
          vertical-align: middle;
      }
      
      0 讨论(0)
    • 2020-12-07 20:23

      Simple solution for vertical align middle... for me it works like a charm

      ul{display:table; text-align:center; margin:0 auto;}
      li{display:inline-block; text-align:center;}
      li.items_inside_li{display:inline-block; vertical-align:middle;}
      
      0 讨论(0)
    提交回复
    热议问题