Reputation: 1521
This is my code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style type="text/css">
.bigbox {
width:1024px;
height:600px;
background-color:#000000;
overflow-x:auto;
overflow-y:hidden;
}
.box{
width:500px;
height:500px;
float:left;
background-color:#AAAAAA;
margin:5px;
}
</style>
</head>
<body>
<div class="bigbox">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
</html>
How can I make all box display horizontally?
And the bigbox has a scroll bar
with the width
fixed.
I have tried to change the width to 5000px, and it work, but the width is over the boxes
Upvotes: 1
Views: 5363
Reputation: 6996
Make your .box div
display:inline-block;
.bigbox {
width:400px;
height:600px;
background-color:#000000;
overflow-x:auto;
overflow-y:hidden;
white-space:nowrap;
}
.box{
width:100px;
height:100px;
float:left;
background-color:#AAAAAA;
margin:5px;
display:inline-block;
}
Upvotes: 1
Reputation: 32182
HI now add some properties
as like this
.bigbox {
white-space:nowrap;
}
.box{
float:left; // remove this line
display:inline-block;
vertical-align:top;
}
Upvotes: 6
Reputation: 4348
Best and easiest way (as far as I know) is to wrap all .box
elements and define their parent width as the sum of all his children's width.
In your code example it would be:
CSS:
.boxview {
width: 3060px;
}
HTML:
<div class="bigbox">
<div class="boxview">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
If the number of boxes is not prefixed then I suggest you use JavaScript to set .boxview
width dynamically.
Also notice overflow-x
will not work on older browsers (IE9+ only).
You might wanna add overflow: auto
before defining overflow-x
and overflow-y
as a fallback.
Code example: http://jsfiddle.net/zvs4H/1/
Upvotes: -1