为什么删除100%的宽度后我的背景图像消失了?

问题描述

我正在创建标题,但是如果我关闭“宽度:100%”,则背景图像将不再显示。这有什么意义?

.img_header{
  position: absolute;
  background-image: url(space.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  width: 100%;   /*----> example if I dismiss this property the image disappear */
  z-index: -1;
  height: 400px;
}

解决方法

唯一提供宽度的是CSS。

背景图像本身不会占用任何空间。因此,如果删除宽度,则宽度将消失,因为没有其他内容可以指定元素的大小。