【问题标题】:Empty div is hidden without any content, how to display it but keep it empty?空 div 被隐藏,没有任何内容,如何显示但保持为空?
【发布时间】:2019-09-26 20:36:23
【问题描述】:

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

这是我的代码,看看你能否弄清楚如何让这个 div 显示背景颜色,但保持空白...谢谢!

HTML:

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

CSS:

#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);
}

【问题讨论】:

    标签: html css css-position center


    【解决方案1】:
    #container:before {
        content:"\00a0\00a0"; 
    }
    

    Demo

    【讨论】:

      【解决方案2】:

      我看到两个选项:

      • 在 div 中插入 &amp;nbsp;&lt;div id="container"&gt;&amp;nbsp;&lt;/div&gt;
      • 在 CSS 中插入 min-width:1px;

      如果不指定高度数(如果 DIV id 为空),则无法增大高度。如果父级定义了高度,则只能使用高度百分比 (%)。

      【讨论】:

        【解决方案3】:

        在 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>
        

        在此处查看演示文件:jsfiddle

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-08
          • 2016-03-30
          • 2013-04-25
          • 1970-01-01
          • 1970-01-01
          • 2013-04-27
          • 2014-02-27
          • 2011-01-15
          相关资源
          最近更新 更多