Html 空div是隐藏的,没有任何内容,如何显示它,但保持它为空?

Html 空div是隐藏的,没有任何内容,如何显示它,但保持它为空?,html,css,css-position,center,Html,Css,Css Position,Center,所以我知道当你设置一个像素的宽度和高度时,你可以解决一个空div被隐藏的问题,但是当使用宽度和高度作为百分比时呢 这是我的代码,看看你是否能想出如何使这个div显示背景色,但保持它为空。。。谢谢 HTML: 在div中提及class属性以引用容器样式类 <div id="container"></div> <style> <!-- #container{ position: relative; width

所以我知道当你设置一个像素的宽度和高度时,你可以解决一个空div被隐藏的问题,但是当使用宽度和高度作为百分比时呢

这是我的代码,看看你是否能想出如何使这个div显示背景色,但保持它为空。。。谢谢

HTML:


在div中提及class属性以引用容器样式类

<div id="container"></div>

<style>
    <!--
    #container{
        position: relative;
        width: 80px;
        height: 80px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 10%;
        margin-bottom: 10%;
        background-color: #E9E9E9;
        opacity:0.6; filter:alpha(opacity=60);
    }
    -->
    </style>

检查此处的演示文件:

我看到两种选择:

  • 在div内插入一个
  • 插入
    最小宽度:1px
如果未指定高度编号(如果DIV id为空),则无法使高度变大。如果父级定义了高度,则只能使用高度百分比(
%

#container{
    position: relative;
    width: 80%;
    height: 80%;
    margin-left: auto;
    margin-right: auto;
    margin-top: 10%;
    margin-bottom: 10%;
    background-color: #E9E9E9;
    opacity:0.6; filter:alpha(opacity=60);
}
<div id="container"></div>

<style>
    <!--
    #container{
        position: relative;
        width: 80px;
        height: 80px;
        margin-left: auto;
        margin-right: auto;
        margin-top: 10%;
        margin-bottom: 10%;
        background-color: #E9E9E9;
        opacity:0.6; filter:alpha(opacity=60);
    }
    -->
    </style>
#container:before {
    content:"\00a0\00a0"; 
}