html - 为什么我的 div 在高度设置为 50% 时不显示,但在设置为 500px 时显示?

标签 html css

为什么 .topcontainer 类的 div 在 height:50%(我认为应该占页面的一半)时不显示,但设置为 500px 时却显示?我正在尝试创建一个响应式页面,但我是 css/html 的新手。

下面是高度:50%

@import url('https://fonts.googleapis.com/css?family=Roboto');
@import url('https://fonts.googleapis.com/css?family=Slabo+27px');

body {
	font-family: "Roboto", sans-serif;
	margin: 0px;
	background-color: #AA3939;
}

div.container {
	width: 100%;
	height: 100%;
	position: relative;
}

div.topcontainer {
	background-image: url("https://scontent-lga3-1.cdninstagram.com/t51.2885-15/e35/20633567_1572345419476732_8981370747351990272_n.jpg");
	background-repeat: no-repeat;
	background-size: cover;
	position:relative; 
	width: 100%;
	height: 50%;
}
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="mobile.css">
		<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>-->
		<!--<script src="masonScript.js"></script>-->
		<title>majic.photography</title>
	</head>
	<body>
		<div class="container">
			<div class="topcontainer">
				
			</div>
		</div>
	</body>
</html>

这只显示空白背景。 下面是 height:500px。

@import url('https://fonts.googleapis.com/css?family=Roboto');
@import url('https://fonts.googleapis.com/css?family=Slabo+27px');

body {
	font-family: "Roboto", sans-serif;
	margin: 0px;
	background-color: #AA3939;
}

div.container {
	width: 100%;
	height: 100%;
	position: relative;
}

div.topcontainer {
	background-image: url("https://scontent-lga3-1.cdninstagram.com/t51.2885-15/e35/20633567_1572345419476732_8981370747351990272_n.jpg");
	background-repeat: no-repeat;
	background-size: cover;
	position:relative; 
	width: 100%;
	height: 500px;
}
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="mobile.css">
		<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>-->
		<!--<script src="masonScript.js"></script>-->
		<title>majic.photography</title>
	</head>
	<body>
		<div class="container">
			<div class="topcontainer">
				
			</div>
		</div>
	</body>
</html>

这显示了容器,但没有响应并且在移动显示器上效果不佳。

提前致谢!

最佳答案

试试这个! 这个想法是将您的 HTML 和正文放在 100% 高度,然后您可以使用高度百分比。

@import url('https://fonts.googleapis.com/css?family=Roboto');
@import url('https://fonts.googleapis.com/css?family=Slabo+27px');
html,body{width:100%; height:100%;}
body {
	font-family: "Roboto", sans-serif;
	margin: 0px;
	background-color: #AA3939;
}

div.container {
	width: 100%;
	height: 100%;
	position: relative;
}

div.topcontainer {
	background-image: url("https://scontent-lga3-1.cdninstagram.com/t51.2885-15/e35/20633567_1572345419476732_8981370747351990272_n.jpg");
	background-repeat: no-repeat;
	background-size: cover;
	position:relative; 
	width: 100%;
	height: 50%;
}
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="mobile.css">
		<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>-->
		<!--<script src="masonScript.js"></script>-->
		<title>majic.photography</title>
	</head>
	<body>
		<div class="container">
			<div class="topcontainer">
				
			</div>
		</div>
	</body>
</html>

干杯

关于html - 为什么我的 div 在高度设置为 50% 时不显示,但在设置为 500px 时显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666015/

相关文章:

javascript - 如何使用javascript将图像随机插入到表格中?

javascript - 渐进式 Web 应用软键盘和导航栏在全屏中重叠

html - 背景不在应有的位置 [CSS/HTML]

javascript - 折叠展开的 html 表格

javascript - 我绑定(bind)到 HTML 按钮的这个 Javascript 函数根本没有运行

Javascript 优雅地降级整个 ​​div

javascript - 更改 HTML 表格的字体样式?

当 url 包含尾部斜杠时 CSS 不加载

html - CSS 位置 :fixed without top gives unexpected layout?

html - 将标题和段落环绕图像