三行CSS代码搞定垂直对齐
2014-08-09 17:40
288 查看
.text p { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); } .image img { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); } .block-of-text p { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); } @mixin vertical-align { position: relative; top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); } .mixin p { @include vertical-align; } /* ==================================== Base styling, to make the demo more fancy ==================================== */ body { font-family: Helvetica Neue, Helvetica, sans-serif; background: #59488b; padding: 1em; -webkit-font-smoothing: antialiased; } h1, h2 { text-align: center; color: white; font-weight: 200; margin-top: 0; } h1 { margin-bottom: 0; } h2 { margin-bottom: 1em; } section { display: block; max-width: 500px; background: #433669; margin: 0 auto 1em; height: 140px; border-radius: .5em; color: white; img, p { padding: 1em; } }
Vertical align anything with just 3 lines of CSS
Published January 13, 2014 — A 2 minute read
发表于2014年1月3号— 短时阅读
With just 3 lines of CSS (excluding vendor prefixes) we can with the help of transform: translateY vertically center whatever we want, even if we don’t know its height.
The CSS property transform is usally used for rotating and scaling elements, but with its translateY function we can now vertically align elements. Usually this must be done with absolute positioning or setting line-heights, but these require you to either
know the height of the element or only works on single-line text etc.
<h1>Vertical center with only 3 lines of code</h1> <h2>(Excluding vendor prefixes)</h2> <section class="text"> <p>I'm vertically aligned! Hi ho Silver, away!</p> </section> <section class="image"> <img src="http://placekitten.com/70/70"> </section> <section class="block-of-text"> <p> I'm a block of text! Lorem ipsum dolor sit amet, consectetur adipisicing elit. At quia doloremque tempora placeat officia ex obcaecati tenetur deserunt repudiandae praesentium.</p> </section> <section class="mixin"> <p>I'm being aligned with a mixin! At quia doloremque tempora placeat officia ex obcaecati tenetur deserunt repudiandae praesentium.</p> </section>
So, to do this we write:
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
That’s all you need. It is a similar technique to the absolute-position method, but with the upside that we don’t have to set any height on the element or position-property on the parent. It works straight out of the box, even in IE9!
To make it even more simple, we can write it as a mixin with its vendor prefixes:
@mixin vertical-align {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.element p {
@include vertical-align;
}
Or you can use the Sass placeholder selector to reduce code bloat in the output CSS:
%vertical-align {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.element p {
@extend %vertical-align;
}
You can find a demo of it here:
Update (April 25th)
As a few people have pointed out, this method can cause elements to be blurry due to the element being placed on a “half pixel”. A solution for this is to set its parent element to preserve-3d. Like following:
.parent-element {
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
相关文章推荐
- css 非表格垂直对齐效果代码
- CSS三行代码搞定三角
- 垂直居中对齐CSS代码
- 三行css代码实现垂直居中
- CSS让图片垂直居中和底端对齐的代码
- css 非表格垂直对齐效果代码
- 如何使div垂直水平居中的css代码
- CSS 解决未知高度垂直居中实现代码
- 用CSS实现文字垂直居中的代码
- CSS代码之DIV垂直居中
- DIV水平垂直居中css实现代码
- CSS处理div中对象垂直对齐
- 用CSS实现文本左右对齐且首行缩进的代码
- 实现div垂直居中的CSS代码
- 【css基础】文本对齐,水平对齐,垂直对齐
- 用javascript 来将word 转成 html 更加简单,三行代码搞定
- 横向三级展开的CSS垂直菜单_网页代码站(www.webdm.cn)
- 用CSS实现文字垂直居中的代码
- css实现文字垂直居中的代码第1/2页
- css控制层绝对垂直居中的正规代码