CSS: background-color only inside the margin
Asked Answered
W

5

68

I have searched for an answer but couldn't find it anywhere. My question is reasonably simple: I have a background color of my body, then a large margin, and now I want a different background color inside the margin.

How do I do that with CSS?

Wetmore answered 6/4, 2012 at 20:19 Comment(2)
you only have background inside the margin. Box model: w3.org/TR/CSS2/box.htmlTriptych
if you use a padding instead of a margin, it will then have the background-color you chose for the elementGreenbelt
L
63

If your margin is set on the body, then setting the background color of the html tag should color the margin area

html { background-color: black; }
body { margin:50px; background-color: white; }

http://jsfiddle.net/m3zzb/

Or as dmackerman suggestions, set a margin of 0, but a border of the size you want the margin to be and set the border-color

Letti answered 6/4, 2012 at 20:24 Comment(3)
or put a div #main_container inside the body to do the same thingBehaviorism
If your background color on the html tag is any color than white, when the page first loads, the color takes up the whole page for a second, before the color of the body kicks in, so it looks kind of weird.Sherer
@AlexF - outlook emails are not standard html, you are extremely limited in what you can do.Letti
E
21

Instead of using a margin, could you use a border? You should do this with <div>, anyway.

Something like this?enter image description here

http://jsfiddle.net/GBTHv/

Euphemize answered 6/4, 2012 at 20:23 Comment(4)
Hi, I've done something similar, but the background color inside the border only extends to the bottom of the text inside the margin. I need it to be inside the whole margin area.. if that makes sense.Wetmore
@Wetmore - I think you're confused by what margin means. Margin is the area around the element, so a border like above is exactly what margin is.Letti
Yeah, but with margins you can set it by percentage - borders you can't. So you if want a margin of 1% on the left and right, you won't be able to do that with borders.Sherer
if the color is transparent, then the border color will blend with the background color.Raimund
P
8

I needed something similar, and came up with using the :before (or :after) pseudoclasses:

#mydiv {
   background-color: #fbb;
   margin-top: 100px;
   position: relative;
}
#mydiv:before {
   content: "";
   background-color: #bfb;
   top: -100px;
   height: 100px;
   width: 100%;
   position: absolute;
}

JSFiddle

Paper answered 4/10, 2014 at 18:8 Comment(0)
B
2

Are you possibly looking to change the margin color outside the border? Maybe outline outline will help? Particularly

outline-color: green;
Beers answered 13/10, 2021 at 17:33 Comment(1)
Or box-shadow https://mcmap.net/q/110933/-outline-on-only-one-borderRaimund
T
1

That is not possible du to the Box Model. However you could use a workaround with css3's border-image, or border-color in general css.

However im unsure whether you may have a problem with resetting. Some browsers do set a margin to html as well. See Eric Meyers Reset CSS for more!

html{margin:0;padding:0;}
Therewithal answered 6/4, 2012 at 20:21 Comment(1)
if the color is transparent, then the border color will blend with the background color.Raimund

© 2022 - 2024 — McMap. All rights reserved.