html - 为什么不显示除法的背景颜色?

标签 html css background-color

我有一个 <div>嵌套在另一个。家长<div>有背景图片。我想要嵌套的 <div>有背景色,但它一直显示其父级的背景图像。

body {
  background-color: #fca205;
}


#poem {
  width: 70%;
  background-color: black;
}


div {
  width: 30%;
  border: 3px gold solid;
  margin: 1em auto;
  background-image: url("images/TigerPattern.jpg");
}

blockquote {
  color: white;
  font-family: Georgia;
  font-size: 16px;	
}
<div>
  <div id="poem">
    <blockquote> 				
      Tiger, tiger, burning bright	 
      In the forests of the night,	 
      What immortal hand or eye	 
      Could frame thy fearful symmetry?	 
    </blockquote>
    <br>
  </div>
</div>

最佳答案

因为你正在使用这个:

 #poem {
    width: 70%;
    background-color: black;
 }


 div {
    width: 30%;
    border: 3px gold solid;
    margin: 1em auto;
    background-image: url("images/TigerPattern.jpg");
 }

因此,您正在为 #poem 定义背景颜色,但您随后覆盖了 div 元素的所有属性,因此您的背景属性如下所示:

#poem{background: url('http://assets.worldwildlife.org/photos/2090/images/hero_small/Sumatran-Tiger-Hero.jpg?1345559303') repeat scroll 0% 0% transparent;}

因此,你需要使用这个:

 body {
     background-color: #fca205;
 }
 #poem {
     width: 70%;
     background: black;
 }
 div {
     width: 30%;
     border: 3px gold solid;
     margin: 1em auto;
     background: url("http://assets.worldwildlife.org/photos/2090/images/hero_small/Sumatran-Tiger-Hero.jpg?1345559303");
 }
 blockquote {
     color: white;
     font-family: Georgia;
     font-size: 16px;
 }

或者更好的是,永远,永远,永远,将属性分配给 div 除非你真的需要它,最好使用 class

See fiddle here

关于html - 为什么不显示除法的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965462/

相关文章:

android - 如何在焦点和单击时更改 ListView 项目的颜色

css - 将一个 Div 内的三个 Div 标签居中

javascript - 为什么第一次加载失败,但后续加载却可以?

html - 在 IE 中点击导航链接向左移动

css - Semantic-UI 容器在某些断点处自动获取边距

css - 垂直居中响应图像

html - 如何制作 HTML/CSS/JS 变色背景(就像 Kahoot.it 有的那样)

jquery - 通过 jquery 更改 .css 背景颜色不起作用

html - 高度 100% 的 Fieldset child 太大了

javascript - 将值传递给弹出窗口