Created
August 15, 2015 14:53
-
-
Save igorpronin/42089c2314c2c3ec82c4 to your computer and use it in GitHub Desktop.
HTML, CSS / Выравнивание элементов по центру по вертикали, горизонтали. Способ 3. Современный, математический.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Работает в новых браузерах. IE - от 10. | |
--- | |
HTML | |
<div class="wrap"> | |
<div class="content"> </div> | |
</div> | |
<div class="wrap"> | |
<img src="http://lorempixel.com/200/200/sports/" width="200" height="200"> | |
</div> | |
CSS | |
.wrap { | |
width: 400px; | |
height: 400px; | |
position: relative; | |
} | |
.content { | |
width: 200px; | |
height: 300px; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translateX(-50%) translateY(-50%); | |
} | |
.wrap img { | |
width: 200px; | |
height: 200px; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translateX(-50%) translateY(-50%); | |
display: block; | |
/* Для изображений в целях профилактики ставится "display: block", поскольку браузер по умолчанию добавляет к ним небольшую границу, которую лучше удалить */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment