Extend a background into the margin

2019-07-29 18:36发布

I have a <div> element that has a 10 pixel margin, as shown in the code below. My question is rather simple: how do I extend the background image into the margin? I tried using the CSS3 background-clip and background-origin sub-atributes, but they have no option for extending a background into the margin.

CSS:

#pagesidebar
{
    background-image:url('/Users/bagavatu/images/background.png');
    margin:10px;
}

HTML:

<div id="pagesidebar">Hello</div>

I know I could append the pagesidebar div into a parent div, and simpyl apply the background-image there, but I would prefer a solution not requiring a new div.

3条回答
Animai°情兽
2楼-- · 2019-07-29 18:54

You should be able to do it with a combination of background-size and background-position

查看更多
Melony?
3楼-- · 2019-07-29 18:55

The background is relative to your div, not to margins, use paddings instead.

查看更多
甜甜的少女心
4楼-- · 2019-07-29 18:58

Switch margin: 10px to padding: 10px and it should do the trick.

查看更多
登录 后发表回答