Skip to content

Instantly share code, notes, and snippets.

@igorpronin
Created August 15, 2015 14:53
Show Gist options
  • Save igorpronin/42089c2314c2c3ec82c4 to your computer and use it in GitHub Desktop.
Save igorpronin/42089c2314c2c3ec82c4 to your computer and use it in GitHub Desktop.
HTML, CSS / Выравнивание элементов по центру по вертикали, горизонтали. Способ 3. Современный, математический.
Работает в новых браузерах. 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