如何为背景图像添加边距?

lis*_*aro 14 css

我想为背景图像添加一个边距,以便我将它与屏幕中间距离,但是在该类中添加它会为整个身体添加边距.

body.poppage {
    background: url(/Imagenes/tip3.png) 50% 200px no-repeat #E2E4E9;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?谢谢

sha*_*han 9

你可以使用background-position属性

  • 使用此背景创建容器,并将其置于页面中心 (2认同)

The*_*emz 5

使用容器填充为背景留出某种余量。

<div class="thebox">
    <p>HEY!</p>
</div>

div.thebox
{
box-sizing: border-box;
width:400px;
height:400px;

border: 2px solid #000000;
padding: 10px;

background: url(http://studio-creator.com/blog/public/html5.jpg) center;
background-size: contain;
background-repeat: no-repeat;
background-origin: content-box;
}
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/gann1pwm/

  • 非常好的例子`background-origin` 用法!可能值得添加一个关于它为什么在那里的解释(使位置相对于内容,而不是默认的填充边缘) (2认同)
  • 这太棒了,我不知道“background-origin: content-box;”。谢谢你! (2认同)

小智 5

这和我一起工作我有 50px 导航栏

background-size: contain !important;
background-repeat: no-repeat !important;
background-position: 0 50px !important;
Run Code Online (Sandbox Code Playgroud)