Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have block level element fill 100% width of overflow-x: scroll container

Tags:

html

css

I've got a container element that's a certain width, with overflow-x: auto. In it I have a block level header element (h1) that's supposed to, being a block element, fill the container horizontally. And it does so, as long as there are no other elements in the container that overflow, creating a horizontal scrollbar. If there are overflowing elements, then the header element fills only the non-overflowing horizontal space of the container, but doesn't appear in the overflowing space.

Fiddle demonstrating the problem: http://jsfiddle.net/rand0mbits/qUh3s/

HTML:

<div id="one">
    <h1>header</h1>
    <table><tr><td>text</td><td>text</td><td>text</td><td>text</td><td>text</td>
    <td>text</td></tr></table>
</div>

CSS:

#one {
    width: 200px;
    overflow: auto;
    border: solid 1px;
}

#one h1 {
    font-size 1.1em;
    background-color: blue;
    display: inline-block;
    width: 100%;
    margin-top: 0;
}

table td {
    border: solid 1px;
    padding: 20px;
}

How do i make the <h1> fill the whole width of the container?

like image 220
Val Schuman Avatar asked Jun 03 '14 15:06

Val Schuman


People also ask

What is overflow-x scroll?

The overflow-x CSS property sets what shows when content overflows a block-level element's left and right edges. This may be nothing, a scroll bar, or the overflow content.

What is overflow-y and overflow-X?

overflow-x specifies what to do with the left/right edges of the content. overflow-y specifies what to do with the top/bottom edges of the content. You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.

What is overflow-X property?

The overflow-x property specifies whether to clip the content, add a scroll bar, or display overflow content of a block-level element, when it overflows at the left and right edges. Tip: Use the overflow-y property to determine clipping at the top and bottom edges.

Does overflow hidden prevent scrolling?

To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element. This hides all content beyond the element's border.


1 Answers

See the fiddle.

Use the HTML caption element:

<div id="one">    

    <table>
        <caption>
            <h1>header</h1>
        </caption>
        <tr>
            <td>text</td>
            <td>text</td>
            <td>text</td>
            <td>text</td>
            <td>text</td>
            <td>text</td>
        </tr>
    </table>

</div>

CSS:

#one {
    width: 200px;
    overflow: auto;
    border: solid 1px;    
}

#one h1 {
    font-size 1.1em;
    background-color: blue;        
    margin-top: 0;
    text-align: left;
}

table td {
    border: solid 1px;
    padding: 20px;
}
like image 144
Fabian Mebus Avatar answered Oct 23 '22 18:10

Fabian Mebus