0

Given a <table> with one or many <td>'s with text that is wider than the parent <div>, is there a way to make the table scroll without making the parent <div> use overflow:auto, and still have the table retain 100% width?

I'm hoping for a CSS solution I can apply to ONLY the <table> element (or its children).

Example: See JSFiddle Demo.

CSS:

<style>
#wrapper {
    width: 250px;
    /* looking for solution that doesn't use overflow auto here */
}
table {
    border-collapse: collapse;
}
td {
    border:1px solid #ccc;
    padding: 3px;
}
</style>

HTML:

<div id="wrapper">
    <p>Table should scroll, but not this text.</p>
    <table>
        <thead>
            <tr>
                <td>A</td>
                <td>B</td>
                <td>C</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>..</td>
                <td>..</td>
                <td>....................................................................................</td>
            </tr>
            <tr>
                <td>..</td>
                <td>..</td>
                <td>..</td>
            </tr>
        </tbody>
    </table>    
</div>

Not modifying the parent div is important in my project because <table>'s are in a <div> with a bunch of other content that I do not want to scroll with it. While I could add a wrapper <div> to all tables in my project, I would also have to rewrite a JavaScript plugin (has to do with paging), which I am trying to avoid.

3 Answers 3

1

You can use overflow: scroll on the table itself if you make it display as block:

table {
    display: block;
    overflow: scroll;
}

Edit:
As the comments below suggest, use td { width: 1%; } as a somewhat-messy way to get the table to still be 100% width if the content is narrower than the wrapper.

Fiddle: http://jsfiddle.net/94g53edb/12/

Sign up to request clarification or add additional context in comments.

8 Comments

This makes the table lose its 100% width if the content is shorter. I am applying the style to a variety of tables so some will have long content and others short. See jsfiddle.net/94g53edb/7
@Justin you could add td { width: 100%; } but that will expand just the first td.
@Justin td { width: 1%; } seems to make the trick. At least in Firefox.
It works for me on Chrome but I'm squemish to use a hack like that without knowing why it works. Do you know what's happening to make it stretch with a 1% width?
@Justin I used it like a hack several times, here's an explanation: stackoverflow.com/questions/21130221/…
|
0

I am just a newbie in css and html, but if I can give my opinion, so there will be two ways in achieving that:

  1. You can set the <p> to the fixed position,

or

  1. You can create another wrapper for the table.

:)

1 Comment

Since the content is variable inside the wrapper, setting it to fixed position would not be a reliable solution. Your second answer is what I said I'm trying to avoid.
0

[I'm adding a second answer because the comments on my first answer are going in a different direction than my new answer, and I don't want to derail that train]

Set the table to display: block and overflow: scroll, and give each of the cells a min-width (in pixels) to make up 100% of the container's width.

Here's what it looks like with table content less than the container width: http://jsfiddle.net/94g53edb/8/

Because the cells have only a min-width and not a fixed width, they can expand as needed, pushing the table to greater than the width of the container, and the table will scroll: http://jsfiddle.net/94g53edb/9/

1 Comment

I like the concept but the <td> widths are all variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.