繁体   English   中英

在BODY中渲染DIV的背景图像

[英]Rendering a background-image for DIV within BODY

我的页面有以下样式:

    <style>
        html, body {
            margin: 0;
            padding: 0
        }
        body {
            background-color: rgb(5, 77, 179);
            text-align: center
        }   
        .contentarea {
            background-image: url('path-to-this-image.png')
        }
        .class {
            background-image: url('path-to-another-image.png')
        }

    </style>

体内有两个DIV标签:

<body>
    <div class="contentarea">
    </div>
    <div class="class">
    </div>
</body>

当我尝试加载页面时,仅显示蓝色背景。 为什么是这样? 我正在使用Chrome> v.25。

CSS:

    .contentarea {
        background-image: url('path-to-this-image.png');
        height: Height of image in px; // or 100%
    }
    .class {
        background-image: url('path-to-another-image.png');
        height: Height of image in px; // or100%
    }

如果div内没有文本,则必须指定高度。 另外,不要忘记添加分号; 在CSS属性的末尾

添加到上一个:使它与100%一起将元素属性设置为“ display:block;”。 它应该工作。 因此,您的CSS可能如下所示:

 body{
    background-color: rgb(5, 77, 179);
    text-align: center
  }
 .contentarea {
    background-image: url('path-to-this-image.png');
    width:100%; /*or whatever*/
    min-height:80%;   /*or whatever*/ 
    display:block;
  }
  .class {
      background-image: url('path-to-another-image.png');
      width:100%; /*or whatever*/
      min-height:80%;   /*or whatever*/ 
      display:block;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM