创建一个高度可以根据背景图像高度调整的部分



我有3个部分,它们的父母只是主体,在这种情况下,第2部分(菜单(背景大于100vh。给出高度:auto;不起作用,我想根据背景长度拉伸部分的高度(auto(,我不想使用(px,vh,cm,…等(给它一个特定的值。我很确定这是一个简单的答案,但我自己却搞不明白。谢谢

html,body {
	position: relative;
	background: white;
	margin: 0;
	padding: 0;
}
#Home {
	position: relative;
	height: 100vh;
	width: 100vw;
	background-image: url(/images/Header.jpg);
	background-size: cover;
	background-repeat: no-repeat;
}
#Menu {
	display: block;
	position: relative;
	height: auto;
	width: 100vw;
	background-image: url(/images/Menu.jpg);
	background-size: cover;
	background-repeat: no-repeat;
	
}
<html>
	<body>
		<section id="Home"></section>
		
		<section id="Menu"></section>
		
		<section id="Map"></section>
	</body>
</html>

实现这一点的唯一方法是不使用实际的后台属性。

正如@Lister先生评论的那样:

元素无法知道背景的高度形象

但是,您可以在#菜单部分中使用带有所需图像作为背景的img标记。然后,创建一个具有绝对定位的容器div,它将包含实际部分的内容。

html,
body {
position: relative;
background: white;
margin: 0;
padding: 0;
}
#Home {
position: relative;
height: 100vh;
width: 100vw;
background-image: url(http://i.imgur.com/8tcxHWh.jpg);
background-size: cover;
background-repeat: no-repeat;
}
#Menu {
display: block;
position: relative;
width: 100vw;
}
.content {
position: absolute;
left: 0;
top: 0;
}
h2 {
color: white;
font-size: 4em;
}
<section id="Home"></section>
<section id="Menu">
<img src="http://i.imgur.com/BF3ty6o.jpg">
<div class="content">
<h2>My content</h2>
</div>
</section>
<section id="Map"></section>

最新更新