您的位置:首页 > Web前端 > CSS

如何实现CSS居中?–CSS居中常用方法

2014-09-27 21:04 330 查看
来源:http://www.ido321.com/824.html

一、水平居中

1、内联元素居中:相对父级块级元素居中对齐

.center-children {

text-align: center;

}


2、块级元素居中:设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果)
[code] .center-me {

margin: 0 auto;

}

[/code]

如果有很多块级元素需要水平居中成一行,最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。

演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/embedded/result/

[code] <main class="inline-block-center">

<div>

I'm an element that is block-like with my siblings and we're centered in a row.

</div>

<div>

I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.

</div>

<div>

I'm an element that is block-like with my siblings and we're centered in a row.

</div>

</main>

<main class="flex-center">

<div>

I'm an element that is block-like with my siblings and we're centered in a row.

</div>

<div>

I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.

</div>

<div>

I'm an element that is block-like with my siblings and we're centered in a row.

</div>

</main>

[/code]

css:

[code] body {

background: #f06d06;

font-size: 80%;

}

main {

background: white;

margin: 20px 0;

padding: 10px;

}

main div {

background: black;

color: white;

padding: 15px;

max-width: 125px;

margin: 5px;

}

.inline-block-center {

text-align: center;

}

.inline-block-center div {

display: inline-block;

text-align: left;

}

.flex-center {

display: flex;

justify-content: center;

}

[/code]

二、垂直居中

1、内联元素:设置相等的上下padding,或者利用line-height

[code] .link {

padding-top: 30px;

padding-bottom: 30px;

}

[/code]

文本不会换行的情况下,可以使用line-height,让其与height相等去对齐文本。

.center-text-trick {

height: 100px;

line-height: 100px;

white-space: nowrap;

}


多行的文本也可以利用上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,
演示地址:http://jsfiddle.net/Web_Code/5fvrwwk1/1/embedded/result/
html:
[code] <table>

<tr>

<td>

I'm vertically centered multiple lines of text in a real table cell.

</td>

</tr>

</table>

<div class="center-table">

<p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>

</div>

[/code]

css

[code] body {

background: #f06d06;

font-size: 80%;

}

table {

background: white;

width: 240px;

border-collapse: separate;

margin: 20px;

height: 250px;

}

table td {

background: black;

color: white;

padding: 20px;

border: 10px solid white;

/* default is vertical-align: middle; */

}

.center-table {

display: table;

height: 250px;

background: white;

width: 240px;

margin: 20px;

}

.center-table p {

display: table-cell;

margin: 0;

background: black;

color: white;

padding: 20px;

border: 10px solid white;

vertical-align: middle;

}

[/code]

2、块级元素

若元素有固定高度,可以这样垂直居中

[code].parent {

position: relative;

}

.child {

position: absolute;

top: 50%;

height: 100px;

margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */

}

[/code]

如果不知道元素高度,则这样

[code].parent {

position: relative;

}

.child {

position: absolute;

top: 50%;

transform: translateY(-50%);

}

[/code]

也可以使用flexbox

[code] <main>

<div>

I'm a block-level element with an unknown height, centered vertically within my parent.

</div>  

</main>

[/code]

[code] body {

background: #f06d06;

font-size: 80%;

}

main {

background: white;

height: 300px;

width: 200px;

padding: 20px;

margin: 20px;

display: flex;

flex-direction: column;

justify-content: center;

resize: vertical;

overflow: auto;

}

main div {

background: black;

color: white;

padding: 20px;

resize: vertical;

overflow: auto;

}

[/code]

三、同时水平和垂直居中

1、元素有固定高度和宽度:先绝对居中,然后上移和左移50%的宽度即可

[code] //这种方案有极好的跨浏览器支持。

.parent {

position: relative;

}

.child {

width: 300px;

height: 100px;

padding: 20px;

position: absolute;

top: 50%;

left: 50%;

margin: -70px 0 0 -170px;

}

[/code]

2、元素的高度和宽度未知或可变的:使用transofrm属性在两个方向都平移负50%

.parent {

position: relative;

}

.child {

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: