tags:

views:

86

answers:

4

Hi,
We can set the padding and margin properties of an element E using either pixels or percentages.

A) I assume that when we use percentages, the percentage value refers to the width of the containing block? Thus, if E is declared directly inside <body> element, then the containing block is <body> and percentage value refers to width of <body> element.

B) What is the default width of a <body> element?

Thanks.

EDIT:

If I set E’s padding to 50%:

   #E
   {
      padding: 50%;
   }

then the width of E's "padding-left + padding-right" will equal the width of a viewport (when viewport is expanded over the entire screen).

A) Thus, I assume the width (specified in pixels) of <body> depends on a horizontal resolution of a monitor and the width of a viewport? My screen resolution is set to 1024*768, and thus in my case the width of a <body> is 1024px (assuming viewport is expanded over the entire screen) ? But if I was to change the resolution to 800*600, then the width of a <body> would be 800px?

+2  A: 
  1. Yes, the CSS will be applied to the element containing it.

  2. The default width in percentages for the <body> tag is 100%.

In general, you should be using a CSS file linked to your HTML, and use CSS Selectors to chose what elements to style. It is bad practice to embed CSS into your HTML, as this is not very maintainable (i.e. if you need to change the style across a website).

Edit:

#E { padding: 50%; }

is a shortcut to:

#E { padding-top: 50%; padding-right: 50%; padding-left: 50%; padding-bottom: 50%;}

In regards to what the % stand for, it's viewport width, i.e. the number of pixels in the inner browser window, showing the HTML, nothing to do with your screen resolution.

If you were to change your window resolution to 800px*600px your viewport width would be somewhat less than 800px, as the scrollbar takes up some of that.

Oded
hi, in case you find the time ... I've edited my initial post in response to your replies. BTW - I do use external CSS file
carewithl
"In regards to what the % stand for, it's viewport width, i.e. the number of pixels in the inner browser window, showing the HTML, nothing to do with your screen resolution" But greater the screen resolution, the more pixels will viewport contain?!
carewithl
And if you keep your window on a 600px by 400px size? Not everyone has their browser window maximized, you know...
Oded
thank you all for your help
carewithl
+1  A: 

The default width of body is 100%, but html is the parent container of body, so it will affect body in some circumstances if you're not aware of it.

Tor Valamo
hi, in case you find the time ... I've edited my initial post in response to your replies
carewithl
+1  A: 

“My screen resolution is set to 1024*768, and thus in my case the width of a <body> is 1024px?”

As Tor mentioned, the <html> element can have margin, borders and padding of its own.

In addition, the browser window’s scrollbars can take up some space too.

Paul D. Waite
Under what conditions would <html> element have its own margin, borders and padding?
carewithl
I may be remembering this wrong, but I think some browsers put a little border on the HTML element. I always tend to set margin, border and padding on <html> and <body> to zero.
Paul D. Waite
+1  A: 

A) I assume that when we use percentages, the percentage value refers to the width of the containing block?

It varies depending on the property the percentage is assigned to. For font-size, for example, it is the computed font-size of the parent element.

Padding is calculated based on the computed width of the parent block — even padding-top uses the width. (Remember, the width is the content width and excludes padding, borders and margins).

B) What is the default width of a element?

The default width of the body element is auto which is "Whatever space is left once margin, padding and borders have been taken into account". Since browsers have non-zero padding and/or margin there by default (which of the two depends on the browser), this is not 100%.

David Dorward
I assume by “browsers having non-zero padding…” you mean the body element has non zero padding and margin? Anyways, thanx for helping me
carewithl