简体   繁体   English

如何使用嵌入式CSS将图像应用于背景?

[英]How do I apply an image to background using inline CSS?

I've tried this: 我已经试过了:

<div id="test" style="background:url(*Various image links*)>
</div>

But nothing shows. 但是什么也没显示。

How do I do it? 我该怎么做?

also display:block or inline-block, with a size 同时显示:块或行内块,大小

display:inline-block;
width:100px;
height:100px;
background-image:.....;

Provide width and height of div: 提供div的宽度和高度:

<div id="test" style="background:url(*Various image links*); width:60px; height:60px;">
</div>

remove the apostrophes in the url() and set size to the div 删除url()中的撇号并将大小设置为div

suppose 假设

style="background:url('images/a.jpg');"

should be 应该

style="background:url(images/a.jpg);"

Two things. 两件事情。 Firstly, you need a height & width, unless you have content within the div, else it'll be sized at 0px by 0px. 首先,您需要一个高度和宽度,除非您在div中有内容,否则它的大小将为0px x 0px。 Secondly, you need to watch out for single vs double quotes. 其次,您需要注意单引号和双引号。 Here is working example: 这是工作示例:

<div id="test" style="background:url('http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png'); height:100px; width:100px;">

</div>

here it is: 这里是:

<body>

<div style="background-image:url('../../Images/flower.jpg');width:300px;height:234px">
The flowers are very beautiful.
</div>

and it's recommended to use background-color so in case the picture doesnt load there's a colorful page not blank. 并且建议使用background-color ,以防万一图片无法加载,请选择彩色页面而不是空白页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM