首页 > 解决方案 > 背景 div 不显示

问题描述

<!doctype html>
<html>
<style type="text/css">
body {
background-color: #444669;
}
#1 {
background-color: #DD2124;
height: 337px;
width: 388px;
}
</style>

<body>
<div id="1"></div>
</body>
</html>

我正在尝试制作具有不同背景颜色的 div 部分,但它甚至根本不显示。

标签: htmlbackground

解决方案


CSS 中的 ID 不能以数字开头。#1例如,只需将 更改为 ,就可以了#first

预习:

https://jsfiddle.net/uLwn05q2/

<!doctype html>
<html>
<style type="text/css">
body {
    background-color: #444669;
}

#first {
    background-color: #DD2124;
    height: 337px;
    width: 388px;
}
</style>

<body>
<div id="first"></div>
</body>
</html>

更新:

您可以使用一个技巧将 id 实际保留为数字:

<style type="text/css">
body {
    background-color: #444669;
}

[id='1']  {
    background-color: #DD2124;
    height: 337px;
    width: 388px;
}
</style>

推荐阅读