HTML代码:-
<!DOCTYPE html>
<html>
<head>
<title>The block-chain website</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
</div>
</body>
</html>
这是CSS代码:-
*{
margin: 0px;
padding: 0px;
font-family: sans-serif;
}
.container{
width: 100%;
height: 100%;
background-color: #42455a;
}
我页面中的问题是具有该颜色的 div 没有出现在屏幕上。
最佳答案
未设置html
和body
的width
和height
属性。
它们将由其中的内容自动设置。
由于您只有一个没有内容的 div,因此定义 width
和 height
属性不会使 body
和 html
扩展为您根据父维度定义这些百分比。
只需定义html
和body
的width
和height
:
*{
margin: 0px;
padding: 0px;
font-family: sans-serif;
}
.container{
width: 100%;
height: 100%;
background-color: #42455a;
}
html, body {
height: 100%;
width: 100%;
}
关于html - 为什么我添加了 css 后我的页面没有显示背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578846/