10

I have a div with several child divs which are floating left. I don't want them to break, so I set them to display:inline-block and white-space:nowrap. Unfortunately nothing happens at all. They just keep breaking.

At the end I want to scroll in x-direction, but when I add overflow-x:scroll; overflow-y:visible it scrolls in y-direction.

.a {
    width: 400px;
    height: 300px;
    white-space: nowrap;
    display: inline-block;
}
.b {
    float: left;
    width: 50px;
    height: 200px;
    display: inline-block;
}

<div class="a">
    <div class="b"></div>
    <div class="b"></div>
    <div class="b"></div>
    <div class="clearfix"></div>
</div>

You can see my complete implementation on JSFiddle

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107

3 Answers3

12

I may not fully understand your question but it seems like the divs/scroll behave if you remove: float: left; from .b and add: overflow:auto; to .a

Craighead
  • 519
  • 3
  • 8
  • 1
    Thanks! I changed `.b` to `display:inline-block` and didn't think of the fact, that inline blocks do not break... :) – Julian F. Weinert Jan 16 '14 at 15:07
  • 1
    In short, `white-space` and `float` are not good friends. Even with `nowrap`, elements if given `float` will ump to new line – Waleed93 Jun 03 '20 at 07:45
2

Not sure what you mean, but if you stop floading your b, and give your a overflow:auto it should work

see: /jsfiddle.net/88yjz/3/

  • Thanks, Craighead had the same fix. I didn't think of the fact, that inline-blocks do not wrap, when I changed `.b` from `display:block;` to `display:inline-block;` – Julian F. Weinert Jan 16 '14 at 15:11
-1

Does this give you what you want? Added overflow scroll.

* {
    margin: 0px;
    padding: 0px;
}
html, body {
    height: 100%;
    background-color: #EEEEEE;
}
.a {
    width: 400px;
    height: 300px;
    white-space: nowrap;
    overflow:scroll;          /* Added this line*/
    background-color: lightcoral;
    -webkit-box-sizing:border-box;
}
.b {
    width: 50px;
    height: 200px;
    margin-top: 50px;
    margin-left: 15px;
    display: inline-block;
    background-color: lightgreen;
    -webkit-box-sizing:border-box;
}
.clearfix {
    float: none;
    clear: both;
}
Ruskin
  • 5,721
  • 4
  • 45
  • 62