CSS vertical alignment text inside li

前端 未结 9 1501
甜味超标
甜味超标 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:29

      Define the parent with display: table and the element itself with vertical-align: middle and display: table-cell.

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

      Surprisingly (or not), the vertical-align tool actually works best for this job. Best of all, no Javascript is required.

      In the following example, I am positioning the outer class in the middle of the body, and the inner class in the middle of the outer class.

      Preview: http://jsfiddle.net/tLkSV/513/

      HTML:

      <div id="container">
          <span></span><div class="outer">
              <span></span><div class="inner">
      
              </div>
          </div>
      </div>
      

      CSS:

      html, body {
          height: 100%;
          margin: 0;
          padding: 0; }
      #container {
          text-align: center;
          height: 100%; }
      span { 
          height: 100%;
          vertical-align: middle;
          display: inline-block; }
      .outer {
          width: 100px;
          height: 200px;
          padding: 0;
          border: 1px solid #000;
          vertical-align: middle;
          display: inline-block; }
      .inner {
          background: red;
          width: 30px;
          height: 20px;    
          vertical-align: middle;
          display: inline-block; }
      

      Vertical align works by aligning the centers of elements that are next to each other. Applying vertical-align to a single element does absolutely nothing. If you add a second element that has no width but is the height of the container, your single element will move to vertically center with this no-width element, thus vertically centering it. The only requirements are that you set both elements to inline (or inline-block), and set their vertical-align attribute to vertical-align: middle.

      Note: You may notice in my code below that my <span> tag and <div> tag are touching. Because they are both inline elements, a space will actually add a space between the no-width element and your div, so be sure to leave it out.

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

      Give this solution a try

      Works best in most of the cases

      you may have to use div instead of li for that

      .DivParent {
          height: 100px;
          border: 1px solid lime;
          white-space: nowrap;
      }
      .verticallyAlignedDiv {
          display: inline-block;
          vertical-align: middle;
          white-space: normal;
      }
      .DivHelper {
          display: inline-block;
          vertical-align: middle;
          height:100%;
      }
      <div class="DivParent">
          <div class="verticallyAlignedDiv">
              <p>Isnt it good!</p>
           
          </div><div class="DivHelper"></div>
      </div>

      0 讨论(0)
    提交回复
    热议问题