简体   繁体   English

HTML容器未正确设置子div的样式

[英]HTML container not styling child div's properly

I am trying to set the background color of my container div and all child div's within it but I can't get it to work for some reason, and I am unsure as to where I am going wrong. 我正在尝试设置容器div以及其中所有子div's背景颜色,但是由于某种原因我无法使其正常工作,而且我不确定我要去哪里。

When I set the background-color and a border on the container you can see that it is not actually "containing" any child elements. 当我在容器上设置background-color和边框时,可以看到它实际上没有“包含”任何子元素。

#facility_container{
    text-align: center;
    padding: 2px;
    width: 100%;
    background-color: #ffffff;
}

Here is a JSFiddle demonstrating where I am at so far. 这是一个JSFiddle,展示了我到目前为止所处的位置。

Add overflow: hidden to #facility_container , #facility_general_info , #facility_section_info 添加overflow: hidden#facility_container#facility_general_info#facility_section_info

Float makes the inner divs not expand the outer ones. Float使内部div不扩展外部div。 Using table settings to style your page is a big no-no in HTML5. 在HTML5中,使用表格设置来设置页面样式是一件大事。

working jsfiddle: https://jsfiddle.net/nayish/docg128w/8/ 工作的jsfiddle: https ://jsfiddle.net/nayish/docg128w/8/

The issue here is that, since your #facility_info divs are floated, they are taken out of the normal element flow, and therefore do not affect the width or height of the #facility_container div. 这里的问题是,由于#facility_info div是浮动的,因此它们不包含在常规元素流中,因此不会影响#facility_container div的宽度或高度。

As suggested by Jon Ducket in his book HTML & CSS : 正如Jon Ducket在他的书HTML&CSS中所建议的那样
Set the container div's overflow property to auto, and its width property to 100%. 将容器div的溢出属性设置为auto,将其width属性设置为100%。

#facility_container {
    text-align: center;
    padding: 2px;
    width: 100%;
    background-color: #ffffff;
    width: 100%;
    overflow: auto;
}

In this case, since #facility_general_info already takes up width and height, it is not necessary to set #facility_container 's width to 100%, but the above-mentioned property values can be used for elements that contain only floated elements. 在这种情况下,由于#facility_general_info已经占用了宽度和高度,因此不必将#facility_container的宽度设置为100%,但是上述属性值可用于仅包含浮动元素的元素。

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

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