i want box 1 and box 2 horizontally next to each other. I want the parent div if the content overflows to scroll and box1 and box2 horizontally next to each other.

i tried below but box2 keeps going underneath box1. the scroll is necessary.

<div id='parent'>
<div id='box1'></div>
<div id='box2'></div>
</div>

#parent {
overflow:scroll;
width:100%;
}

#box1 {
float:left;
width:100px;
height:100px;
}

#box2 {
float:left;
}

Dani AI

Generated

The issue you are hitting comes from float layout and shrink-to-fit behavior: a parent with width:auto will not expand to accommodate floated children, so the second box wraps beneath. For a horizontal scroller where the parent auto-sizes to its content, avoid floats and use a two-layer approach: an outer scrolling container of fixed or responsive width, and an inner row that grows to the combined width of the boxes. Using inline-blocks with nowrap achieves this without hard-coding widths. Set horizontal scrolling explicitly with overflow-x:auto and suppress vertical scrolling with overflow-y:hidden. See MDN: overflow and MDN: white-space.

<style>
.scroller {
  width: 100%;            /* or any fixed/responsive width */
  overflow-x: auto;
  overflow-y: hidden;
}
.row {
  white-space: nowrap;    /* keep boxes on one line; row grows to fit */
}
.box {
  display: inline-block;  /* no floats */
  width: 101px;
  height: 101px;
  border: 1px solid #000;
  vertical-align: top;    /* align tops if heights differ */
}
</style>

<div class="scroller">
  <div class="row">
    <div class="box"></div>
    <div class="box"></div>
    <!-- add more .box as needed -->
  </div>
</div>

Modern alternative: use a flex container with display:flex; flex-wrap:nowrap; overflow-x:auto; and children flex:0 0 auto; to prevent shrinking. Reference: MDN: Flexbox.

Recommended Answers

All 7 Replies

You could change your css to something like the below:

#parent {
overflow-y: scroll;
width: 100%;
}

#box1 {
width: 15%;
float: left;
}

#box2 {
width: 80%;
float: right;
}

I think this achieves what you're asking.

element.style {
float: left;
height: 405px;
overflow-x: hidden;
overflow-y: scroll;
padding-right: 15px;
width: 470px;
}

fix the height of the box then it wil work

i don't understand .. in my code do i apply this to the parent div?

i tried this but it didn't work. the box go beneath one another.

<style type="text/css">
  #parent {
overflow:scroll;
width:100px;
}


.box1 {

border:1px solid black;
float: left;
height: 101px;
overflow-x: hidden;
overflow-y: scroll;
padding-right: 15px;
width: 101px;
}
</style>


<div id='parent'>
<div class='box1'></div>
<div class='box1'></div>
</div>

what i'm trying to accomplish is two boxes next to each other inside a div, which i can scroll horizontally to see the two boxes. if you copy the code below it works correctly, only because the width of the parent div is set.

<style type="text/css">


#container {
overflow:scroll;
width:100px;
}

  #parent {
overflow:hidden;
width:400px;
}

.box1 {

border:1px solid black;
float: left;
height: 101px;
width: 101px;
}

</style>

<div id='container'>
<div id='parent'>
<div class='box1'></div>
<div class='box1'></div>
</div>
</div>

if i change width:900px in the parent div to width:auto then the boxes go underneath one another. That's the problem, the parent div should auto adjust to the width of both boxes.

i'd really appreciate someone solving this. i can't have a fixed width on the parent div it should auto adjust and both boxes should be next to each other.

Try the below in it's entirety; I'm reasonably sure this is what you're asking, though I'm not clear if you want to make it fluid (therefore, the width: auto on #parent) or not. With fixed widths, the below should work fine.

<style type="text/css">
#container {
	overflow-x: scroll;
	width:100px;
}

#parent {
	overflow:hidden;
	width:200px;
}

#parent div {
	float: left;
	border:1px solid black;
	height: 98px;
	width: 98px;
}

.box1 {
	background: yellow;
}

.box2 {
	background: lightgreen;
}

</style>
<div id='container'>
    <div id='parent'>
        <div class='box1'></div>
        <div class='box2'></div>
    </div>
</div>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.