I am trying to put a link in a <th>
element and have the <a>
element fill the entire space of the <th>
. While I can get it to fille horizontally with width:100%, getting it to fill vertically is proving to be troublesome. height:100% seems to have no effect.
I want to do this so that the user can click anywhere in the cell to activate the link. I know I could put an onClick property on the <th>
itself and handle it via javascript, but I would like to do it the "proper" css way.
Clarification: Each row of the table can have a different height because the content is dynamic, so solutions that use a fixed height for the <a>
or <th>
elements will not work.
Here is some sample code:
<style type="text/css"> th { font-size: 50%; width: 20em; line-height: 100%; } th a { display:block; width:100%; height:100%; background-color: #aaa; } th a:hover { background-color: #333 } </style> <table border=1> <tr> <th><a href="foo">Link 1</a></th> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor.</td> </tr> <tr> <th><a href="foo">Link 2</a></th> <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor. </td> </tr> </table>
And here is a page with that exact same code
As you can see if you mouseover the link, the <a>
element is not filling vertically which produces an awkward look. I could fix the hover background by putting it in a "th:hover a" style, but the link would still only actually function if you clicked where the actual a
tag is.
Since the child div is a child of the parent div , then you can set its width to 100%, which will set it to 100% of the parent width. If you know the width of the parent, then you can just set its width to the same value.
The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.
What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.
By default a block level element (eg div ) will fill it's parent... you can specify width:100%; too if you want, although that's the default, and will cause issues if you have padding/margin/borders in some browsers ;) Does this answer your question?
just change th style to this
th { font-size: 50%; width: 20em; height: 100%; line-height: 100%; }
cheers
As @Hober says, you need to give the <th>
a height. You could also use height: 100%
there.
If you know the dimensions of your <th>
, which it looks like you probably won't in this case (since they're on the side), I've had luck adding a padding to push the <a>
out to the edges (past any padding from the <th>
itself), and then bringing it back in with a corresponding negative margin. Like this:
th a{ padding: 10px 0; margin: -10px 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With