我能否将背景图片添加到一个 <div>
元素中呢?如果可以的话,应该如何操作?
<style type="text/css">
.bgimg {
background-image: url('../images/divbg.png');
}
</style>
...
<div class="bgimg">
div with background
</div>
你可以使用CSS的 background
属性来实现这个功能。有几种方法可以做到:
HTML:
<div id="div-with-bg"></div>
CSS:
#div-with-bg
{
background: color url('path') others;
}
HTML:
<div class="div-with-bg"></div>
CSS:
.div-with-bg
{
background: color url('path') others;
}
HTML:
<div style="background: color url('path')"></div>
其中:
background
CSS 属性是以下语法中所有背景属性的连接:
background: background-color background-image background-repeat background-attachment background-position;
来源:w3schools
是的:
<div style="background-image: url(../images/image.gif); height: 400px; width: 400px;">Text here</div>
使用此样式来获得无重复的居中背景图片。
.bgImgCenter{
background-image: url('imagePath');
background-repeat: no-repeat;
background-position: center;
position: relative;
}
在HTML中,为您的div设置这个样式:
<div class="bgImgCenter"></div>
使用方法如下..
<div style="background-image: url(../images/test-background.gif); height: 200px; width: 400px; border: 1px solid black;">Example of a DIV element with a background image:</div>
<div style="background-image: url(../images/test-background.gif); height: 200px; width: 400px; border: 1px solid black;"> </div>
<body>
<img id="backgroundimage" src="bgimage.jpg" border="0" alt="">
</body>
在你的CSS文件中(拉伸背景):
#backgroundimage
{
height: auto;
left: 0;
margin: 0;
min-height: 100%;
min-width: 674px;
padding: 0;
position: fixed;
top: 0;
width: 100%;
z-index: -1;
}
<div class="foo">Foo Bar</div>
在你的CSS文件中:
.foo {
background-image: url("images/foo.png");
}
<div id="image">Example to have Background Image</div>
.image {
background-image: url('C:\Users\ajai\Desktop\10.jpg');
}
.divi{
background-image: url('path');
}
</style>
<div class="divi"></div>