Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get span to take up the full height of the containing td

Tags:

html

css

I have a table, and in the left column I want to add an indicator for the row. I'm using a span to render the indicator, but I can't get the span to take up the full height:

<table>
    <tr>
        <td style="padding:0px;"><span style="height:100%; width:5px; background-color:pink;">&nbsp;</span></td>
        <td>Some content</td>
        <td>Some more content</td>
    </tr>
</table>

The table has a padding of 15px, so for the indicator col I remove the padding, and set the span to height:100%:

td {padding:15px;}
table {background-color: yellow;}

This fiddle shows it currently running - that pink bar needs to span the whole height of the containing td.

How do I do this? Note that I can't set the styling on the td instead of the span because that causes other issues with the rendering (it offsets the border-bottom of the th rows if I do that).

like image 424
Matt Roberts Avatar asked Jun 06 '13 11:06

Matt Roberts


People also ask

How do I make a div span the whole page height?

height:100vh When you set the height to 100vh, the box element will stretch its height to the full height of the viewport regardless of its parent height. I do not have to add one for horizontal as div is a block-level element that will take the full width horizontally by default.

Can we have span inside TD?

span is inline element by default so you have to change display property to inline-block for example. putting a width on the spans, it puts everyone in a separate line (see updated question).

Can we add height to span?

The <span> tag is a inline element, it fits into the flow of the content and can be distributed over multiple lines. We can not specify a height or width or surround it with a margin.


2 Answers

Should add overflow:auto to the span (and display:block of course)

<table>
   <tr>
      <td style="padding:0px;"><span style="height:100%; width:5px; background-color:pink;display:block;overflow:auto">&nbsp;</span></td>
      <td>Some content</td>
      <td>Some more content</td>
  </tr>
</table>
like image 60
Jaay Avatar answered Oct 19 '22 23:10

Jaay


You don't need the <span> there. All you need is to set the right and left padding to 0 so your colored row indicator is only 5 pixels wide as u wanted and leave the top and bottom paddings 15 as in the other cells so the background color covers the whole height of cell/row.

HTML

  <table>
    <tr>
        <td class="rowindicator">&nbsp;</td>
        <td>Some content</td>
        <td>Some more content</td>
    </tr>
</table>

CSS

    table{background: yellow;}
    td{padding:15px;}
    .rowindicator{
         width:5px;                       
         padding-right:0px;  
         padding-left:0px;                       
         background-color:pink;
    }

Demo: http://jsfiddle.net/mNjsb/34/

like image 41
Ms. Nobody Avatar answered Oct 20 '22 01:10

Ms. Nobody