亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Web開(kāi)發(fā)中盒子居中的幾種方法

  發(fā)布時(shí)間:2016-06-17 09:15:01   作者:佚名   我要評(píng)論
這篇文章主要為大家詳細(xì)介紹了Web開(kāi)發(fā)中讓盒子居中的8種方法,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下

一、記錄下幾種盒子居中的方法:
 
1.0、margin固定寬高居中;
 
2.0、負(fù)margin居中;
 
3.0、絕對(duì)定位居中;
 
4.0、table-cell居中;
 
5.0、flex居中;
 
6.0、transform居中;
 
7.0、不確定寬高居中(絕對(duì)定位百分?jǐn)?shù));
 
8.0、button居中。
 
二、代碼演示(html使用同一個(gè)Demo):
 
html Demo:
 
<body>
    <div id="container">
        <div id="box"></div>
    </div>
</body>
 
 
1.0、margin固定寬高居中(演示)
 
這種定位方法純粹是靠寬高和margin拼出來(lái)的,不是很靈活。
 
CSS:
 
#container {
    width: 600px;
    height: 500px;
    border: 1px solid #000;
    margin: auto;
}
#box {
    width: 200px;
    height: 200px;
    margin: 150px 200px;
    background-color: #0ff;

 
2.0、負(fù)margin居中(演示)
 
利用負(fù)的margin來(lái)進(jìn)行居中,需要知道固定寬高,限制比較大。
 
CSS:

#container {
    position: relative;
    width: 600px;
    height: 500px;
    border: 1px solid #000;
    margin: auto;
}
#box {
    position: absolute;
    width: 200px;
    height: 200px;
    left: 50%;
    top: 50%;
    margin: -100px -100px;
    background-color: #0ff;
}
 
3.0、絕對(duì)定位居中(演示)
 
利用絕對(duì)定位居中,非常常用的一種方法。
 
CSS:

#container {
    position: relative;
    width: 600px;
    height: 500px;
    border: 1px solid #000;
    margin: auto;
}
#box {
    position: absolute;
    width: 200px;
    height: 200px;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    background-color: #0ff;
}
 
4.0、table-cell居中(演示)
 
利用table-cell來(lái)控制垂直居中。
 
CSS:
 
#container {
    display: table-cell;
    width: 600px;
    height: 500px;
    vertical-align: middle;
    border: 1px solid #000;
}
#box {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    background-color: #0ff;
}
 
5.0、flex居中(演示)
 
CSS3中引入的新布局方式,比較好用。缺點(diǎn):IE9以及IE9一下不兼容。
 
CSS:
 
#container {
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: center;
            align-items: center;
    -webkit-justify-content: center;
            justify-content: center;
    width: 600px;
    height: 500px;
    border: 1px solid #000;
    margin: auto;
}
#box {
    width: 200px;
    height: 200px;
    background-color: #0ff;

 
6.0、transform居中(演示)
 
這種方法靈活運(yùn)用CSS中transform屬性,較為新奇。缺點(diǎn)是IE9下不兼容。
 
CSS:

#container {
    position: relative;
    width: 600px;
    height: 600px;
    border: 1px solid #000;
    margin: auto;
}
#box {
    position: relative;
    top: 50%;
    left: 50%;
    width: 200px;
    height: 200px;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    background-color: #0ff;
}
 
7.0、不確定寬高居中(絕對(duì)定位百分?jǐn)?shù))(演示)
 
這種不確定寬高的居中,較為靈活。只需要保證left和right的百分?jǐn)?shù)一樣就可以實(shí)現(xiàn)水平居中,保證top和bottom的百分?jǐn)?shù)一樣就可以實(shí)現(xiàn)垂直居中。
 
CSS:

#container {
    position: relative;
    width: 600px;
    height: 500px;
    border: 1px solid #000;
    margin: auto;
}
#box {
    position: absolute;
    left: 30%;
    right: 30%;
    top: 25%;
    bottom: 25%;
    background-color: #0ff;
}
 
8.0、button居中(演示)
 
利用button做外容器,里邊的塊元素會(huì)自動(dòng)垂直居中,只需要控制一下水平居中就可以達(dá)到效果。
 
HTML:
 
<button>
    <div></div>
</button>

CSS:

button {
    width: 600px;
    height: 500px;
    border: 1px solid #000;
}
div {
    width: 200px;
    height: 200px;
    margin: 0 auto;
    background-color: #0ff;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

原文鏈接:http://www.cnblogs.com/likar/archive/2016/06/16/5590948.html

相關(guān)文章

最新評(píng)論