Bera
Bera

Reputation: 1282

Deal with css html div align vertically and horizontally

I have this layout:

<div id="container">
<div id="left">LEFT</div>
<div id="mtop">MIDTOP</div>
<div id="mbotton">MIDBOT</div>
<div id="right">RIGHT</div>
<div id="botton">BOTTON</div>
</div> 

#container {
    height:200px;
    width:300px;
    vertical-align:middle;
    display:table-cell;
    background-color:yellow;
}
#left {
    height:100px;
    color:white;
    background-color:blue;
    font-size:20px;
    width:100px;
}
#right {
    height:100px;
    color:white;
    background-color:red;
    font-size:20px;
    width:100px;
}
#botton {
    height:20px;
    display: block;
    vertical-align: botton color:white;
    background-color:green;
    font-size:20px;
    width:100%;
}
#mtop {
    height:50px;
    color:white;
    background-color:orange;
    font-size:20px;
    width:100px;
}
#mbotton {
    height:50px;
    color:white;
    background-color:pink;
    font-size:20px;
    width:100px;
}
#left, #right {
        display: inline-block;
        vertical-align: middle
    }
#mtop, #mbotton {
        display: inline-block;
        vertical-align: top
}

Live Demo jsFiddle

But I need this layout

Layout

Thanks for help me.

Upvotes: 2

Views: 1748

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74048

When you move the #right div above the #mtop div, then you just need a float: left for the #left div and a float: right for the #right div

HTML:

<div id="container">
    <div id="left">LEFT</div>
    <div id="right">RIGHT</div>
    <div id="mtop">MIDTOP</div>
    <div id="mbotton">MIDBOT</div>
    <div id="botton">BOTTON</div>
</div>

CSS:

#left {
    float: left;
}
#right {
    float: right;
}

You should also remove all those vertical-aligns and change

#container {
    height:200px;
    width:300px;
    vertical-align:bottom;
    display:table-cell;
    background-color:yellow;
}

All divs will then neatly align at the bottom of the #container.

Modified JSFiddle

Upvotes: 2

zxqx
zxqx

Reputation: 5205

Wrap your middle divs in a parent div, and give the left, middle and right divs float: left so they display next to each other.

CSS for middle div:

#middle {
  width: 100px;
  float: left;
}

See DEMO.

Upvotes: 3

Shabab
Shabab

Reputation: 249

You should create a <div> to contain both MIDTOP and MIDBOT

Better yet, avoid the Container <div> altogether and use a <table> with 0 px border.

Upvotes: 1

Related Questions