CSS vertical alignment of inline/inline-block elements

后端 未结 4 1144
南旧
南旧 2020-11-22 09:36

I\'m trying to get several inline and inline-block components aligned vertically in a div. How come the span in this exam

相关标签:
4条回答
  • 2020-11-22 09:47

    Simply floating both elements left achieves the same result.

    div {
    background:yellow;
    vertical-align:middle;
    margin:10px;
    }
    
    a {
    background-color:#FFF;
    width:20px;
    height:20px;
    display:inline-block;
    border:solid black 1px;
    float:left;
    }
    
    span {
    background:red;
    display:inline-block;
    float:left;
    }
    
    0 讨论(0)
  • 2020-11-22 09:51

    vertical-align applies to the elements being aligned, not their parent element. To vertically align the div's children, do this instead:

    div > * {
        vertical-align:middle;  // Align children to middle of line
    }
    

    See: http://jsfiddle.net/dfmx123/TFPx8/1186/

    NOTE: vertical-align is relative to the current text line, not the full height of the parent div. If you wanted the parent div to be taller and still have the elements vertically centered, set the div's line-height property instead of its height. Follow jsfiddle link above for an example.

    0 讨论(0)
  • 2020-11-22 09:56

    Give vertical-align:top; in a & span. Like this:

    a, span{
     vertical-align:top;
    }
    

    Check this http://jsfiddle.net/TFPx8/10/

    0 讨论(0)
  • 2020-11-22 09:57

    For fine tuning the position of an inline-block item, use top and left:

      position: relative;
      top: 5px;
      left: 5px;
    

    Thanks CSS-Tricks!

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