tags:

views:

58

answers:

5

Hi,

I have a simple div with a background color for it defined in CSS. There is some text that I wish to display over this div in a certain position, however when I pad the div it causes the background to shift as well:

#body {
   margin-left: 10px;
   background: red;
}

I just want the text to shift. How can I do this without using two div tags?

Thanks.

EDIT: Here is an example of what I'm working with: http://jsfiddle.net/qQxF2/

+1  A: 

If you don't have a border, use the padding instead of the margin.

#body {
    padding-left: 10px;
    background: red; /* this is not valid CSS! */
    background-color: red; /* this is valid */
}

The W3C has an extensive and (hopefully) accurate text on the box model and things like padding, margin, borders etc.

MvanGeest
Still, margin area (10px) is gonna be white (or whatever inherited color is)
Nikita Rybak
W3C text is accurate as long as you don't trigger quirks mode in MS Internet Explorer… ;-)
ghoppe
@Nikita Rybak - since my answer changed a lot, your comment doesn't really make sense. @Delup - I'm glad I could help you. It only cost us three of jsFiddle's unique IDs and 30 minutes. :) I wish you good luck with your site.
MvanGeest
A: 

I usually add outer div (<div class="outer"><div class="inner">text</div></div>) to solve this

.outer {
    background-color: red;
}
.inner {
    padding-left: 10px;
}
Nikita Rybak
In reaction to the comment on my answer: you're right, I misunderstood the question. But this shouldn't be happening...
MvanGeest
i was afraid i'd have to resort to using double divs. but your solution seems to be the most cleanest. will select this answer if no other viable solution comes up. thanks
Delup
@Delup - what's the difference between your page and the jsFiddle in my comment on your question? @Nikita Rybak - same question...
MvanGeest
@MvanGeest - for me the background shifts as well which is interesting because it does not show this effect in the jsFiddle.
Delup
@Delup - exactly... the big question is **why**? Is your `div` floated or positioned absolutely or relatively?
MvanGeest
@MvanGeest You're right, I probably confused margin with padding (I'm still new to that model).
Nikita Rybak
@MvanGeest - I see what you're getting at.. I'll post the code as soon as I can.
Delup
@MvanGeest - http://jsfiddle.net/qQxF2/
Delup
@Delup - but you used the `margin`, not the `padding`. Try http://jsfiddle.net/DFj6C/5/
MvanGeest
@MvanGeest - oh wow.. that's embarrassing. admittedly i've been using margin and padding interchangeably. thanks for pointing this out
Delup
@Delup - you could edit your question, I'll add an answer, and you can accept it :) Actually, the answer is already there, I just deleted it because I thought it was an answer to another question...
MvanGeest
@MvanGeest - sure i will do that!
Delup
@Delup Yep, we've made the same mistake :)
Nikita Rybak
A: 

Why not set the background color on the actual body tag itself?

body {
    background-color: red;
}

#body {
    padding-left: 10px;
}
pmalmsten
A: 

Additionally you could always just apply the padding to the text itself, depending on how much content you are actually going to have in the container div. You really don't want to name the div body though, since that's not very semantic. You might want to change it to #container or something similar so you don't develop bad habits. body should be the page body, not a div.

If your just going to have text, you can do something like this:

 #container { background-color: red; }
 #container p { margin-left: 10xp; }

Otherwise, if your going to have a mixture of tags and content in the parent div and you don't want to apply the margin them individually (although technically you should in order to avoid getting into the habit of just nesting extraneous div's), Nikita's answer should work good for you, assuming you just want the background on the container div and not the entire page itself.

Jervis
A: 

applying padding should not shift the background.

it should shift the background when you set the margin property.

try following...

<html>
<head>
<style type="text/css">

.padding
{
background-color:yellow;
padding-left:150px;
}

.margin
{
background-color:yellow;
margin-left:150px;
}
</style>
</head>

<body>

<div class="padding">This is a paragraph with padding.</div>
<br/>
<div class="margin">This is a paragraph with margin.</div>

</body>

</html>
N30