简体   繁体   English

CSS 背景图像未显示

[英]CSS background images not showing up

Why background image not showing up on page?为什么背景图片不显示在页面上? I have unordered list which contained two background images list items.我有一个无序列表,其中包含两个背景图像列表项。

 .grid { height: 350px; padding: 20px; background-clip: content-box; background-size: cover; background-position: center; }.small { flex-basis: 30%; }.large { flex-basis: 70%; }
 <ul class="grid"> <li class="small" style="background-image: url(https://thumbs.dreamstime.com/b/spring-flowers-blue-crocuses-drops-water-backgro-background-tracks-rain-113784722.jpg);"></li> <li class="large" style="https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500);"></li> </ul>

You need to add background-image: url to your second li for the code to register the URL.您需要将background-image: url添加到您的第二个li以获取注册 URL 的代码。

<li class="large" style="background-image: URL(https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500);"></li>

Alternatively, I would recommend that you shift your url to your CSS file to make your HTML code neater and more readable.或者,我建议您将url为 CSS 文件,以使您的 HTML 代码更整洁、更易读。

.small {
  [other css]
  background-image: url("https://thumbs.dreamstime.com/b/spring-flowers-blue-crocuses-drops-water-backgro-background-tracks-rain-113784722.jpg");
}

.large {
  [other css]
  background-image: url("https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");
}

You're missing background-image:url() for the second list item.您缺少第二个列表项的background-image:url()

 .grid { height: 350px; padding: 20px; background-clip: content-box; background-size: cover; background-position: center; }.small { flex-basis: 30%; }.large { flex-basis: 70%; }
 <ul class="grid"> <li class="small" style='background-image: url("https://thumbs.dreamstime.com/b/spring-flowers-blue-crocuses-drops-water-backgro-background-tracks-rain-113784722.jpg");'> </li> <li class="large" style='background-image:url("http://placekitten.com/301/301")';></li> </ul>

With code moved to css file:将代码移至 css 文件:

 .grid { height: 350px; padding: 20px; background-clip: content-box; background-size: cover; background-position: center; }.small { flex-basis: 30%; background-image: url("https://thumbs.dreamstime.com/b/spring-flowers-blue-crocuses-drops-water-backgro-background-tracks-rain-113784722.jpg"); }.large { flex-basis: 70%; background-image: url("http://placekitten.com/301/301"); }
 <ul class="grid"> <li class="small"> </li> <li class="large"></li> </ul>

the URL of the image has to go under " " (quotations).图像的 URL 必须在“”(引用)下的 go 下。 Also, the 2nd item has some syntax errors.此外,第二项有一些语法错误。

 <li class="small" style="background-image: url("https://thumbs.dreamstime.com/b/spring-flowers-blue-crocuses-drops-water-backgro-background-tracks-rain-113784722.jpg");"></li>

<li class="large" style="background-image: url("https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");"></li>

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

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