简体   繁体   中英

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.

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 .

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-align s and change

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

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

Modified JSFiddle

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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