There are two possibilities that you are having problems with.
1. Vertical scroll bar appearing causing horizontal scroll bar to appear
If you position an element to be the exact width of the screen, whenever a vertical scroll bar appears this will reduce the horizontal width available, and mean that the horizontal scroll bar will appear also. You shouldn't need to specify a width to strech a display: block
element to the entire width of a page (except when using floats or position absolute). Instead just remove width: 1024px;
and the element should automatically strech to fill the available space.
2. Overflowing children on the horizontal
A child within a wrapping parent will always extend outside of it's parent if forced to do so by specific CSS or styling. i.e. if you have floated children -- For example:
+--------------------+
| +---+ +------+ +------+
| | | | | | |
| +---+ +------+ +------+
+--------------------+
If the parent is dimensioned to be the full width of the page, then horizontal scroll bars will appear because the child is pushing outside the scrollable area. To prevent this you can use overflow: hidden
on the wrapping element:
+--------------------+
| +---+ +------+ +---|
| | | | | | |
| +---+ +------+ +---|
+--------------------+
UPDATE :- With regard to your comments
It seems that you are trying to work out the best way to handle displaying content when the screen/viewport is small. The best practice for this is not to disable scrollbars or use overflow:hidden
— but instead to create what is called a 'responsive layout'. There are a number of ways to achieve such a layout and they all really depend on your target audience and target devices. Which ever route you choose however, they all follow the same basic principals:
- Design your layout so that it collapses, showing the most important content first.
- Design your layout to stretch, either by resizing or using multi-column constructs.
- Avoid using fixed widths on wrappers, you can do so on sub-elements however.
- Learn to use
max-width
and min-width
- Learn to use 'progressive enhancement', meaning that you should have a base layout that works well for most — but even better for those that can support better features.
The most simple way to create a responsive layout is to start with an off-the-shelf base template. Here are a few that can get you started:
- http://www.getskeleton.com
- http://twitter.github.com/bootstrap
- http://www.responsivegridsystem.com
You can find quite a lot more just searching around with the keywords responsive html template
or responsive html boilerplate
.
Put simply
As a basic rule, it is best to try and break your design down into columns and float these using float: left
or other positioning abilities like display: inline-block
. Once you get into floating however you need to know how to clear floats either using some kind of clear fix or other css tricks that have the same affect like overflow: hidden
. A simple responsive layout would be as follows:
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
</div>
Using the following css:
.row {
overflow: hidden;
background: #aaa;
padding: 10px;
}
.row .col {
float: left;
width: 200px;
background: #777;
margin: 2px;
}
On a wide screen this layout would display as follows:
+--------------------+
| +---+ +---+ +---+ |
| | | | | | | |
| +---+ +---+ +---+ |
+--------------------+
But on a smaller screen this would show:
+---------+
| +---+ |
| | | |
| +---+ |
| +---+ |
| | | |
| +---+ |
| +---+ |
| | | |
| +---+ |
+---------+
The trick is to order your elements so that you get your most important content on top. The least important content can either fall to the bottom of the page (or depending on what CSS/JS tricks you deploy) you could hide that content or convert it to appear in a totally different place. It can get rather complicated, and is a constantly evolving field, which is why it is a good idea to start with a base template which can do the hard work for you.