Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

html - How to expand parent DIV by relative child DIV?

I need to expand parent div by child div, please look at this code:

HTML

<div class="wrapper">
    <header class="header"> </header>
    <div class="middle">
            <div class="container">
                    <main class="content">
                            <div id="child">
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            <p>1</p>
                            </div>
                    </main>
            </div>
            <aside class="left-sidebar"></aside>
    </div>
    <footer class="footer"></footer>
</div>

CSS

.wrapper {
    width: 500px;
    margin: 0 auto;
    min-height: 100%;
}
.header {
    height: 100px;
    background: blue;
}
.footer {
    height: 60px;
    width: 100%;
    bottom: 0;
    position: relative;
    background:yellow;
    clear:left;
}
.middle {
    width: 100%;
    min-height: 300px;
    position: relative;
}
.container {
    min-height: 300px;
    width: 100%;
    float: left;
}
.content {
    width: 800;
    min-height: 300px;
    left: 280;
    position: relative;
    background:red;
    padding:10px;
}
#child {
    position:relative; 
    top:100px;
    left:160px;
    min-height:500px;
    width: 200px;
    border: solid 1px white;
    background:green;
}
.left-sidebar {
    float: left;
    width: 100px;
    min-height: 500px;
    margin-left: -100%;
    position: relative;
    background:  black;
}

DEMO: JSFIDDLE

the problem is that main.content is not fully expanded by #child vertically on the value of "top:200" that is in #child relative positioning properties, how can I fix it? because it currently overlaps the footer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actualy, when ou apply the "top" property with position: relative, it doesn't interfeer on the other elements of the page. so it will just overlap the parent div. Try using margin-top instead:

#child {
    margin-top: 200px;
    left:60;
    min-height:500;
    border: solid 1px white;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...