如何根据背景图像大小自动调整 div 的大小?

How to automatically resize a div based on background image size?

我需要帮助调整 div。

我需要的很简单:我有一张 1920x1080px 的图片,我把这张图片作为背景图片放在我的 div 中。

我的代码:

HTML:

    <!DOCTYPE html>
<html>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="_css/Estilo.css"/>
<head>
    <title>ooOOOoos</title>
</head>
<body>
    <div id=imgHeader>
    <p>Hey!!</p>
</div>
</body>
</html>

CSS:

@charset "UTF-8";

*{
    margin: 0;
    padding: 0;
}

body{
    background-color: #ffffff;
}

#imgHeader{
    background: url(../_img/img1.jpg) no-repeat center center fixed;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -ms-background-size: cover;
    -o-background-size: cover;
}

我需要这样的东西: 但是..我得到了这个: 关于如何帮助我的任何想法?

最好的办法是绝对将 div 与背景图片放在一起。然后你可以设置它来拉伸整个宽度和高度。

#imgHeader{
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;

    background: url(../_img/img1.jpg) no-repeat center center fixed;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -ms-background-size: cover;
    -o-background-size: cover;
}

Demo

根据 OP

的评论进行编辑

如果固定高度 header 是您想要达到的目标,那么您可以在其上设置 min-height

#imgHeader{
        min-height: 80rem;

        background: url(../_img/img1.jpg) no-repeat center center fixed;
        background-size: cover;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -ms-background-size: cover;
        -o-background-size: cover;
    }

Demo

您只需为 div 定义一个高度。我建议像这样使用 100%:

* {
  margin: 0;
  padding: 0;
}
html, body {
  background-color: #ffffff;
  height: 100%; /*see change here*/
}
#cabecalho {
  height: 100%; /*see change here*/
  background: transparent url("http://i.stack.imgur.com/vNQ2g.png") no-repeat fixed center center / contain; /*contain will ensure that the entire image is in view*/
}
<div id="cabecalho">
  <p>Hey!! Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit​​, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris. Hi mindless mortuis soulless creaturas, imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead.

Cum horribilem walking dead resurgere de crazed sepulcris creaturis, zombie sicut de grave feeding iride et serpens. Pestilentia, shaun ofthe dead scythe animated corpses ipsa screams. Pestilentia est plague haec decaying ambulabat mortuos. Sicut zeder apathetic malus voodoo. Aenean a dolor plan et terror soulless vulnerum contagium accedunt, mortui iam vivam unlife. Qui tardius moveri, brid eof reanimator sed in magna copia sint terribiles undeath legionis. Alii missing oculis aliorum sicut serpere crabs nostram. Putridi braindead odores kill and infect, aere implent left four dead.

Lucio fulci tremor est dark vivos magna. Expansis creepy arm yof darkness ulnis witchcraft missing carnem armis Kirkman Moore and Adlard caeruleum in locis. Romero morbo Congress amarus in auras. Nihil horum sagittis tincidunt, zombie slack-jawed gelida survival portenta. The unleashed virus est, et iam zombie mortui ambulabunt super terram. Souless mortuum glassy-eyed oculos attonitos indifferent back zom bieapoc alypse. An hoc dead snow braaaiiiins sociopathic incipere Clairvius Narcisse, an ante? Is bello mundi z?</p>
</div>