Vertical align all(!) elements in TD?

前端 未结 11 1055
鱼传尺愫
鱼传尺愫 2021-01-31 14:28

I have a simple table with 1 TD with vertical-align:middle;. This TD contains an Image :



        
相关标签:
11条回答
  • 2021-01-31 14:35

    Just move your vertical-align: middle from the span to the image.

    The image will align itself on the text; works better than the opposite ;)

    img
    {
      height:43px;width:43px;
      vertical-align:middle;
    }
    

    http://jsfiddle.net/ZnpNF/1/

    0 讨论(0)
  • 2021-01-31 14:37

    You can just use vertical-align: middle; to your span

    img
    {
      height:43px;width:43px;
      display: inline;
      vertical-align: middle;
    }
    
    span
    {
      vertical-align:middle;
      display: inline;
      
    }
    

    your jsbin


    As per comment


    You may think as if td is given vertical-align: middle; then it should align all the contents inside this but having an image and a span in which browser is understanding the image is what? : is this inline or inline-block, so you need to set display: inline or inline-block; Then you may see its working only applying display property for image. demo

    Edit

    img tag : source: display inline vs inline-block

    They are "block" elements in that they have a width and a height.

    It's true, they are both - or more precisely, they are "inline block" elements. This means that they flow inline like text, but also have a width and height like block elements.

    Also check this:

    Replaced Elements

    A replaced element is any element whose appearance and dimensions are defined by an external resource. Examples include images ( tags), plugins ( tags), and form elements (, , , and tags). All other elements types can be referred to as non-replaced elements.

    Replaced elements can have intrinsic dimensions—width and height values that are defined by the element itself, rather than by its surroundings in the document. For example, if an image element has a width set to auto, the width of the linked image file will be used. Intrinsic dimensions also define an intrinsic ratio that’s used to determine the computed dimensions of the element should only one dimension be specified. For example, if only the width is specified for an image element—at, say, 100px—and the actual image is 200 pixels wide and 100 pixels high, the height of the element will be scaled by the same amount, to 50px.

    Replaced elements can also have visual formatting requirements imposed by the element, outside of the control of CSS; for example, the user interface controls rendered for form elements.

    In an inline formatting context, you can also think of a replaced element as being one that acts as a single, big character for the purposes of wrapping and layout. A width and height can be specified for replaced inline elements, in which case the height of the line box in which the element is positioned is made tall enough to accommodate the replaced element, including any specified box properties.

    0 讨论(0)
  • 2021-01-31 14:38

    Is this what you mean? http://jsfiddle.net/JFVNq/

    The reason is spans are treated as inline so you need to make them block.

    CSS for the span:

    td.vert span
    {
        vertical-align: middle;
        display: block;
    }
    
    0 讨论(0)
  • 2021-01-31 14:38

    I think we are all nearly there but

    td img,
    td span {
    display:inline-block;
    vertical-align:middle;
    }
    

    seems to work.

    Codepen Example

    Or am I missing something?

    0 讨论(0)
  • 2021-01-31 14:42

    Here's a JS fiddle solution

    fiddle

    Here's the css

      table
    {
      border:solid 1px red;
      width:300px;
    }
    
    td
    {
      height:100px;
      vertical-align:middle;
      width:100%;
      border:solid 1px green;
    }
    img
    {
      display: inline;
    height: 43px;
    width: 43px;
      position: relative !important;
    float: left;
    }
    
    span
    {
      height: auto !important;
    width: 80%;
    float: right;
    position: relative;
    vertical-align: middle !important;
    text-align: left;
    }
    
    0 讨论(0)
  • 2021-01-31 14:44

    Just add vertical-align:middle; to your img style?

    Demo

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