【发布时间】:2020-05-28 09:05:30
【问题描述】:
是否可以让<div> 元素包含背景图片,如果可以,我将如何做?
【问题讨论】:
标签: html
是否可以让<div> 元素包含背景图片,如果可以,我将如何做?
【问题讨论】:
标签: html
<div id="image">Example to have Background Image</div>
我们需要在Style标签中添加如下内容:
.image {
background-image: url('C:\Users\ajai\Desktop\10.jpg');
}
【讨论】:
大部分方法和设置全身是一样的
.divi{
background-image: url('path');
}
</style>
<div class="divi"></div>
【讨论】:
使用此样式获得居中的背景图像,不重复。
.bgImgCenter{
background-image: url('imagePath');
background-repeat: no-repeat;
background-position: center;
position: relative;
}
在 HTML 中,为您的 div 设置此样式:
<div class="bgImgCenter"></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-xxx 属性的连接:
背景: 背景色 背景图片 背景重复 背景附件 背景位置;
来源:w3schools
【讨论】:
您可以简单地添加一个带有 id 的 img src 属性:
<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;
}
【讨论】:
你是说这个吗?
<style type="text/css">
.bgimg {
background-image: url('../images/divbg.png');
}
</style>
...
<div class="bgimg">
div with background
</div>
【讨论】:
是:
<div style="background-image: url(../images/image.gif); height: 400px; width: 400px;">Text here</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>
【讨论】:
<div class="foo">Foo Bar</div>
在您的 CSS 文件中:
.foo {
background-image: url("images/foo.png");
}
【讨论】: