简体   繁体   English

HTML div 标签相互重叠

[英]HTML div tags overlapping each other

I have a problem with my div tags that my sidebar and content appears under my header when I resize window.我的 div 标签有问题,当我调整窗口大小时,我的侧边栏和内容出现在我的标题下。

When the window is maximized当窗口最大化时

When the window is resized to a smaller size当窗口被调整为更小的尺寸时

Here is my CSS code:这是我的 CSS 代码:

    .header {
        height: 15%;
        width:100%;
        margin-bottom:0px;
        padding-bottom:0px;
    }

    .sidebar {
        padding: 0px 0px 0px 0px;
        margin: 0px 0px 0px 0px;
        height: 85%;
        float: left;
        width: 15%;
    }

    .content {
        float: right;
        padding: 0px 0px 0px 0px;
        margin: 0px 0px 0px 0px;
        height: 85%;
        width: 85%;
        text-align: center;
    }

I am also using a container to wrap the page content:我还使用容器来包装页面内容:

    .container {
        margin: 0px 0px 0px 0px;
        height: 100%;
        width: 100%;
    }

Put position: relative;放置position: relative; on sidebar and content:在侧边栏和内容上:

.sidebar {
        padding: 0px 0px 0px 0px;
        margin: 0px 0px 0px 0px;
        height: 85%;
        float: left;
        width: 15%;
        position: relative;
    }

.content {
    float: right;
    padding: 0px 0px 0px 0px;
    margin: 0px 0px 0px 0px;
    height: 85%;
    width: 85%;
    text-align: center;
    position: relative;
}

You shouldn't use float for making layouts, it's not ment to do that, and gets you into problems like this.你不应该使用 float 来制作布局,它不是这样做的,并且会让你陷入这样的问题。 Float is ment to place block-elements in a text like this: Float 用于在文本中放置块元素,如下所示: 在此处输入图片说明

you could simply fix this by using position: absolute;你可以通过使用position: absolute;来简单地解决这个问题position: absolute;

 .header { position: absolute; top: 0; left: 0; height: 15%; width:100%; margin-bottom:0px; padding-bottom:0px; background: red; color: white; } .sidebar { position: absolute; left: 0; top: 15vh; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px; height: 85vh; width: 15vw; background: orange; color: white; } .content { position: absolute; top: 15vh; left: 15vw; right: 0; bottom: 0; padding: 0; margin: 0; text-align: center; }
 <div class="header"> This is the header </div> <div class="sidebar"> This is the sidebar </div> <div class="content"> This is the content </div>

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

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