CSS width of a tag

后端 未结 7 627
清歌不尽
清歌不尽 2020-12-07 16:36

I use tags in my module titles, e.g.

Categories.

I specify the span\'s background color/imag

相关标签:
7条回答
  • 2020-12-07 16:43

    spans default to inline style, which you can't specify the width of.

    display: inline-block;
    

    would be a good way, except IE doesn't support it

    you can, however, hack a multiple browser solution

    0 讨论(0)
  • 2020-12-07 16:48

    You can't specify the width of an element with display inline. You could put something in it like a non-breaking space ( ) and then set the padding to give it some more width but you can't control it directly.

    You could use display inline-block but that isn't widely supported.

    A real hack would be to put an image inside and then set the width of that. Something like a transparent 1 pixel GIF. Not the recommended approach however.

    0 讨论(0)
  • 2020-12-07 16:48

    Like in other answers, start your span attributes with this:

    display:inline-block;  
    

    Now you can use padding more than width:

    padding-left:6%;
    padding-right:6%;
    

    When you use padding, your color expands to both side (right and left), not just right (like in widht).

    0 讨论(0)
  • 2020-12-07 16:50

    Having fixed the height and width you sholud tell the how to bahave if the text inside it overflows its area. So add in the css

    overflow: auto;

    0 讨论(0)
  • 2020-12-07 16:56

    You could explicitly set the display property to "block" so it behaves like a block level element, but in that case you should probably just use a div instead.

    <span style="display:block; background-color:red; width:100px;"></span>
    
    0 讨论(0)
  • 2020-12-07 17:03

    Use the attribute 'display' as in the example:

    <span style="background: gray; width: 100px; display:block;">hello</span>
    <span style="background: gray; width: 200px; display:block;">world</span>
    
    0 讨论(0)
提交回复
热议问题