css - 背景图像未显示在标题中

标签 css background-image

我想将背景图像添加到 <header>标记,但图像不显示。

header {
    background-image: url("/img/header-bg.jpg");
    background-repeat: repeat-x;
    width: 100%;
    height: 105px;
    background: #fff;
    position: relative;
    display:block
}

当我转到 Chrome 中的开发人员工具时,我可以看到图像的 URL 很好(我可以看到图像),但让我感到困惑的是,当我添加

background-image: url("/img/header-bg.jpg");
background-repeat: repeat-x;

作为 header 的新样式规则在 Chrome 中,显示背景图片。

最佳答案

background 属性更改为 background-color 或在不需要时将其完全删除。

background is a short hand css property, It will override all other previous styles.

您的代码应该是:

header {
  background-image: url("/img/header-bg.jpg");
  background-repeat: repeat-x;
  background-color: #fff;
  width: 100%;
  height: 105px;
  position: relative;
  display:block

或简写形式如下:

header {
  background: #fff url("/img/header-bg.jpg") repeat-x;
  width: 100%;
  height: 105px;
  position: relative;
  display:block

关于css - 背景图像未显示在标题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38180418/

相关文章:

html - 像表格一样对齐但使用 CSS?

html - doctype对css的影响

css - 使输入字段透明

Java:在 JFrame 上添加背景图像

html - Div 不会到达网站底部

CSS/SCSS rails 转换 div,但不是嵌套图像 (rails)

javascript - 只检测鼠标滚动一次

css - 如何将 CSS 背景图像保持在所有 div(面包屑箭头)之上?

css - h2 中的背景图像未显示

javascript - 如何在 ASP.NET 中配置面板背景图像