aaronstacy
aaronstacy

Reputation: 6428

How do I get rid of this horizontal scrollbar in Chrome and Safari?

How do i get rid of the horizontal scrollbar on this code: codepen? I'm seeing it in Safari and Chrome, but not Firefox.

I'm using bootstrap, and I've got roughly the following markup:

<div class="container">
  <div class="row">
    <div class="messages span6 offset1">
      <table class="table">
        <tbody>
          <tr>
            <td class=timestamp>[2:22 PM]</td>
            <td>echo|</td>
            <td>zot: Got a paste or gist of the code?</td>
          </tr>
          <!-- many more rows… -->
        </tbody>
      </table>
    </div>
  </div>
</div>

And styling:

.messages {
  height: 200px;
  overflow: auto;
}
.messages .timestamp {
  min-width: 75px;
}

The problem seems to be the min-width constraint, but I need that to keep the first column from wrapping. I also need to limit the height of messages to 200 pixels. I can't set overflow-x: hidden on .messages because it'll cut off content.

Upvotes: 0

Views: 3844

Answers (5)

Chris Krycho
Chris Krycho

Reputation: 3155

A completely alternate thought: why are you using tables to do this? You're not laying out tabular data; you have some semantically related pieces, but they're not tabular in their relationship. Given that, you're breaking one of the cardinal rules: don't use tables for layout! It looks to me like you could probably make this work much more sensibly using div elements, using either float or inline-block with specified widths on them. In that case, your markup would look something like this:

<div class="container">
  <div class="row">
    <div class="messages span6 offset1">
      <div class="message">
        <span class="timestamp">[2:22 PM]</div>
        <span class="author">echo|</div>
        <span class="messageContent">zot: Got a paste or gist of the code?</div>
      </div>
      <!-- many more rows… -->
    </div>
  </div>
</div>

Then, your CSS would be fairly straightforward, since you've defined the width value for the span6 (I looked at the actual CSS on the CodePen):

.message {
    display: block;
    clear: both;
}

.timestamp, .author, .messageContent {
    display: inline-block;
    vertical-align: top;
}

.timestamp, .author {
    width: 75px;
}

.messageContent {
    400px; /* You'd obviously need to tweak this down to account for any padding */
}

You shouldn't have the nasty overflow problems, and the divs should fill up their heights in perfectly normal ways. You can also bound them. And there's no overflow issue anymore.

(Perhaps you're where you are because it's something that bootstrap defaults to, in which case: UGH. Break it, or do whatever is necessary to get away from using tables for layout. It will always, always be more pain than it's worth, and it's unsemantic to boot.)

Upvotes: 0

Jimmy Breck-McKye
Jimmy Breck-McKye

Reputation: 3064

You could increase the width of the table by changing its span6 to a span7, or use a span class to force a width on the message tds that is consistent with the Twitter bootstrap grid structure context.

I couldn't tell you exactly why this is necessary; I actually don't know much about how tables get laid out. But this seems like a solution you could deploy.

Upvotes: 0

PSL
PSL

Reputation: 123749

How about this:-

Use word-break on the last column to avoid it cut off.

word-break

Demo

.messages {
    height: 200px;
    overflow-y: auto;
    
}
.messages .timestamp {
    min-width: 75px;
}
.messages td:nth-child(3) {
    word-break:break-all; /* or use word-break:normal; if you don't want to get the word cut in between*/
   
}

This will adjust the word-break based on the width available, without hiding the contents.

Upvotes: 3

Srikanth AD
Srikanth AD

Reputation: 1854

You could change the height property for .messages to "auto" instead of 200px.

Upvotes: 0

jdepypere
jdepypere

Reputation: 3553

Use the following css:

.messages {
  height: 200px;
  overflow-y: scroll;
  overflow-x: hidden;
}
.messages .timestamp {
  min-width: 75px;
}

Upvotes: 2

Related Questions