0
<div id="wrapper">
  <div id="top-part">
    some para here
  </div>
</div>

main css:

#wrapper {
  position: relative;
  width: 1024px;
  margin: 0 auto;
  display: block;
}

This is full of window without scroll bar.
But when added another contents after above wrapper it appears scroll bar. Why??

<div id="wrapper">
  <div id="wrap">
    <p>
      <img src="images/some.gif" />
      some para here
    </p>
    <a href="#">Link here</a>
  </div>
</div>

I have inserted wrap contents more than 3 times... Css is here.....

#wrap {width: 322px; margin: 6px;}

I've tried display: inline-block, eliminating margin, etc. How to do? I've tried class="wrap" also.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

2 Answers2

0

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:

  1. Design your layout so that it collapses, showing the most important content first.
  2. Design your layout to stretch, either by resizing or using multi-column constructs.
  3. Avoid using fixed widths on wrappers, you can do so on sub-elements however.
  4. Learn to use max-width and min-width
  5. 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:

  1. http://www.getskeleton.com
  2. http://twitter.github.com/bootstrap
  3. 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.

Community
  • 1
  • 1
Pebbl
  • 34,937
  • 6
  • 62
  • 64
0

change your wrapper css to this:

#wrapper {
  position: relative;
  width: 100%;
  margin: 0 auto;
  display: block;
}

and also look out for the width of img tags. see if any of them is not stretching outside the bounds.

Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • yeah! i know we can specify full width by percentage method but is there not px method to show full width – Bhojendra Rauniyar Mar 06 '13 at 11:52
  • never give px to the large containers, as even if it doesn't come with a scrollbar on your machine, it will come on somebody's having lower resolution than yours – Manish Mishra Mar 06 '13 at 11:54
  • besides, what you are asking is to calculate the screen width, that you can do through jQuery very very easily. you can also adjust your div width upon window resize – Manish Mishra Mar 06 '13 at 11:54
  • there is no such width. that's why you use percentage. so that divs automatically scale according to the resolution. – Manish Mishra Mar 07 '13 at 07:02
  • @ManishMishra- Thank you for your comment. I would like to know how yahoo has managed the page width? In any resolution it looks same but their contents are not getting wider. How to use width 100%? min-width: 750px; max-width: 1000px;? Please could you show me a good example? – Bhojendra Rauniyar Mar 10 '13 at 04:28