473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DIV height behaviour

Hi,
I am a bit confused about the height behaviour of DIV elements. In particular, if I give a div a border it will include the padding of anything inside it when deciding the height. If I use no border the padding is not included in the height. This is very obvious if you color the background of the div.

Is this the correct behaviour? Can someone point me to where it is described on w3? Also any recommendations for books that really cover css well would be appreciated.

Many thanks,
Alex.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <style>
  5. .div1
  6. {
  7.   background: blue;
  8.   width: 200px;
  9. }
  10.  
  11. .div2
  12. {
  13.   background: blue;
  14.   width: 200px;
  15.   border: 1px solid black;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="div1">
  21. <p>Hello</p>
  22. </div>
  23. <div class="div2">
  24. <p>World</p>
  25. </div>
  26. </body>
  27. </html>
  28.  
Jun 14 '10 #1
3 1453
drhowarddrfine
7,435 Expert 4TB
This may be what you need as well as this. In the first link, pay attention to collapsing margins. height and width have nothing to do with margin/padding and is the height/width of the content only.

The background style of the content, padding, and border areas of a box is specified by the 'background' property of the generating element. Margin backgrounds are always transparent.
"Transcending CSS" is highly recommended.
Jun 14 '10 #2
Thanks for the reply. At the moment I still find that css doesn't behave how I expect it to very often. Unfortunately there is no substitute for a good knowledge!
Jun 15 '10 #3
drhowarddrfine
7,435 Expert 4TB
Make sure you are using a modern browser. Do not trust any version of IE to do anything right.
Jun 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Not4u | last post by:
Hello, I have a problem with CSS code. I want to have one menu column with a fixed width and a 100% height and the rest of the page for content. ---------------- |.|.|<--100%-->|...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
2
by: Clemens Quoss | last post by:
Hello, I just encountered a strange thing when switching the doctype on and off in the following html testpage (with IE 6.0): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
5
by: S | last post by:
Whew! Thanks for your help on that last post. I actually understand what you wrote. I'm trying not to use these groups as a way to get my code written for me, I really want to understand what...
1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
6
by: . | last post by:
I have an absolutely-positioned div with a y-repeating background inside a container div of variable height (but taller than the browser screen). I need the child div to expand to fill the entire...
1
by: astrochelonian | last post by:
Hello, all, First time I post, so I beg for everyone's patience. I am writing because I have the following doubt. in the page http://atuin.no-ip.org, you can see a blue-grey bar stretching...
0
by: amrhi | last post by:
Hy Guys , Can anybody help me ? I try to make small web database in my unit. Some of fields have on change behaviour to get other data that automatically filled other text field. But when i try to...
1
by: =?ISO-8859-1?Q?Andr=E9_Wagner?= | last post by:
Hello! (please read this message using fixed-font) I'm writting a website with boxes of content. The number of cells in each row might change depending on the browser window size, and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.