【问题标题】:Why does my background image disappear when the width is 100% deleted?100%删除宽度后,为什么我的背景图片消失了?
【发布时间】:2020-12-18 13:05:22
【问题描述】:

我正在创建一个标题,但如果我关闭“宽度: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;
}

【问题讨论】:

  • @MisterJojo width 的默认值不是 0,而是 auto 和 auto 的 position:absolute 是收缩以适应,如果没有内容,计算的值将是 0
  • 如果你删除width: 100%;,“默认值”将是喜欢 width: 0;

标签: html css image background width


【解决方案1】:

唯一赋予它宽度的是你的 CSS。

背景图片本身不占用任何空间。所以如果你去掉宽度,那么它就会消失,因为没有其他内容可以给元素大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2012-08-18
    • 2011-11-23
    • 2019-06-02
    相关资源
    最近更新 更多