css - 为什么以百分比表示的元素高度不起作用?
问题描述
我试图创建一个响应式正方形并添加了以下内容:
.square {
width: 50%;
height: 50%;
}
但是,它水平扩展但垂直根本不扩展......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
body{
background: #cccede;
}
.square {
width: 50%;
height: 50%;
border: 2px solid black
}
</style>
</head>
<body>
<div class="square"></div>
</body>
</html>
在这个片段中,为什么盒子不使用窗户的高度来计算它的高度?
解决方案
height: 50%
means that it will be half as tall as its parent container. Your square is that small because it's inside the body
tag which is small. You can make the body
as tall as the screen by doing this:
body {
height: 100%;
}
推荐阅读
- python - 矢量场平均代码不平滑在一起?
- html - 除非我单击 Web 图标,否则按钮不会打开链接
- java - 如何在Java中查找文本文件中的重复项
- python - 有没有办法知道前一个种子来计算下一个种子?
- python - 通过 TCP 隧道的套接字连接(Python 的 socket.socket)的主机和端口是什么?
- php - 一个信号通过 api 发送通知
- php - 是否有重定向规范的功能。用户登录后到dashboard.php 页面?
- button - JFXDialog 在 KeyCode.Enter、MouseEvent.Click 上关闭
- c# - 如何从 XAML local:imageresource 实现自定义 BindableProperty
- typescript - 如何使用扩展运算符调用重载函数