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

后端 未结 4 1946
时光取名叫无心
时光取名叫无心 2021-01-18 05:58

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

相关标签:
4条回答
  • 2021-01-18 06:42

    The H1 is going to inherit the width of its parent element since it's relative, so it will always end up being the same width you set #one to.

    What you can do is instead of #one having overflow: auto, wrap the table inside another DIV with overflow: auto. This way, #one stays a fixed width, but the wrapper around the table, allows the content to scroll horizontally.

    jsfiddle: http://jsfiddle.net/yetti/Ggua5/

    0 讨论(0)
  • 2021-01-18 06:51

    Try this:

    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;
        position:relative;
    }
    
    table td {
        border: solid 1px;
        padding: 20px;
    }
    
    h1:after {
         content:"";
         background: blue;
         position: absolute;
         top: 0;
         bottom: 0;
         width: 100%;
        left:100%
     }
    

    fiddle

    0 讨论(0)
  • 2021-01-18 06:51

    Change this CSS code like the following then check and let me know if you want this:

    #one {
    width: 100%;
    overflow: auto;
    border: solid 1px;
    }
    
    0 讨论(0)
  • 2021-01-18 06:56

    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;
    }
    
    0 讨论(0)
提交回复
热议问题