Home  >  Q&A  >  body text

css - 浮动对span高度的影响

给span加上浮动(float:left),再给他加上行高(line-height:30px;),那么此时span的高度为30px。那么去除其浮动,它的高度就不为30px。为什么呢?暂时没想明白。

span{float:left; line-height:30px;}
伊谢尔伦伊谢尔伦2713 days ago974

reply all(7)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:06:11

    First of all, span is an inline element, and secondly, span is a non-replacement element, so the width and height cannot be set. After you float, make span a block-level element, so you can set the width and height. Although img, input, textarea, and select are inline, they are replacement elements. If the width and height are not set, then the default width and height are set, and the width and height can be set.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:06:11

    float will become an element block, that is to say, the display of a floating element is block

    reply
    0
  • 阿神

    阿神2017-04-17 13:06:11

    Span is an inline element by default

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:06:11

    I feel like your description is unclear.
    With the line-height:30px; attribute, the line height is always 30px regardless of whether it is floated or not.
    If it is height:30px; the height attribute does not work without the float attribute, and the row height cannot be set for inline elements. With the addition of float, the element will become a block-level element, and the height will be effective.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:06:11

    The inline element has no border. For example, two spans will be mixed into one line. Use the height from outside to inside to determine the height of the content. It can be understood that the inline element has no border and shrinks to the content boundary. If float is added, the rules are not explicitly specified. It will inherit inline-block by default; due to the block method of flat, it is used to determine the boundary from the inside out and then take the height;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:06:11

    It’s very simple, span is an inline element, and the width and height cannot be set for this element. But there are two ways to indirectly change its display: float and position.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:06:11

    I can’t tell what the problem is just by looking at your CSS.
    You write and add line-height: 30px !important; try

    reply
    0
  • Cancelreply