简体   繁体   English

CSS定位:彼此间隔3个div

[英]CSS positioning: 3 divs on each other

在此处输入图片说明

This is the situation: 这种情况:

I have a main div with 2 div parts(red and orange), both have width: 100% and height: 90%. 我有一个包含2个div部分(红色和橙色)的主div,它们的宽度分别为100%和高度90%。 (should be responsive!) (应该有所反应!)

Inside the red div there is a nav bar (top-right-pink), and 3 buttons in the middle. 红色div内部有一个导航栏(右上角粉红色),中间有3个按钮。

The aqua div has to be above both red and orange divs. 水色div必须高于红色和橙色div。

What is the right way to position everything? 放置所有东西的正确方法是什么?

using relative on the red and orange divs doesnt work because of the '%' in the heights. 在红色和橙色div上使用相对值不起作用,因为高度为'%'。

<div class="main">
    <div class="thedude"></div>
    <div class="first">
            <ul>
                <li> <a href="#"> Clients </a> </li>
                <li> <a href="#"> About Us </a> </li>
                <li> <a href="#"> Contact </a> </li>
                <li class="hasImage"><a href="#"> <img src="logo.png"> </a></li>
            </ul>
            <div class="timages">
                <a href="#"><img src="icon1.png"></a>
                <a href="#"><img src="icon2.png"></a>
                <a href="#"><img src="icon3.png"></a>
            </div>
    </div>

    <div class="second">

    </div>

</div>




*{
    margin: 0;
    padding: 0;
}

body{
    font-size: 100%; 
    font-family: arial;
}

.first{
    width: 100%; 
    height: 90%;
    background-color: #2acecd;
}

.thedude{
    width: 95em;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    background-image: url('yellow_creature.png');
    background-repeat: no-repeat;
    background-size: 100%, 100%;
    z-index: 500;
}

.second{
    width: 100%; 
    height: 90%;
    background-color: #f49900;
}

.third{
    width: 100%;
    height: 90%;
    background-color: #fbc00a;
}

.timages{
    margin:0 auto;
    width: 81%;
    padding-top: 23%;
    text-align: center;
    max-width: 62%;
}

.timages img{
    text-align: center;
    max-width: 100%;
}

ul{
    z-index: 540;
    position: absolute;
    right: 0;
    text-transform: uppercase;
}

li{
    float: left;
    padding: 2em 0.5em;
}

li a{
    text-decoration: none;
    color: white;
}

li img{
    max-width: 10em;
}

.hasImage{
    padding: 0.6em 0.5em;
}

http://jsfiddle.net/4z55sjn0/ http://jsfiddle.net/4z55sjn0/

Your HTML structure is the main problem. 您的HTML结构是主要问题。

HTML HTML

<div class="main">
    <div class="thedude">
    <div class="first">

    </div>

    <div class="second">
            <ul>
                <li> <a href="#"> Clients </a> </li>
                <li> <a href="#"> About Us </a> </li>
                <li> <a href="#"> Contact </a> </li>
                <li class="hasImage"><a href="#"> <img src="logo.png"/> </a></li>
            </ul>
            <div class="timages">
                <a href="#"><img src="icon1.png"/></a>
                <a href="#"><img src="icon2.png"/></a>
                <a href="#"><img src="icon3.png"/></a>
            </div>
    </div>

    <div class="third">

    </div>
</div>
</div>

If you want the menu on the orange div you need to move it...inside the orange block! 如果您想要橙色div上的菜单,则需要将其移动到橙色块内!

CSS CSS

    .first {
    width: 30%;
    height: 90%;
    background-color: #2acecd;
    float:left;
    position:absolute;
    top:5%;
    z-index: 999 !important;
}
.thedude {
    width: 95em;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    background-image: url('yellow_creature.png');
    background-repeat: no-repeat;
    background-size: 100%, 100%;
    z-index: 500;
}
.second {
    width: 100%;
    height: 90%;
    background-color: #f49900;
    position:relative;
}
.third {
    width: 100%;
    height: 90%;
    background-color: #fbc00a;
}
.timages {
    position: absolute;
    top: 50%;
    width: 100%;
    text-align: center;
}
.timages img {
    text-align: center;
    max-width: 100%;
}
ul {
    z-index: 540;
    position: absolute;
    right: 0;
    text-transform: uppercase;
    list-style: none;
}
li {
    float: left;
    padding: 2em 0.5em;
}
li a {
    text-decoration: none;
    color: white;
}
li img {
    max-width: 10em;
}
.hasImage {
    padding: 0.6em 0.5em;
}

Check the updated fiddle . 检查更新的小提琴 Is that close to what you're after? 这与您追求的目标接近吗?

UPDATE (following comments to this answer) 更新 (以下评论此答案)

I've swapped the styles to overcome the misunderstanding. 我交换了样式以克服误解。

Check updated fiddle . 检查更新的小提琴

I hope it helps. 希望对您有所帮助。

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

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