Skip to content

Instantly share code, notes, and snippets.

@kizu
Forked from vitkarpov/gist:9908836
Created April 1, 2014 07:39
Show Gist options
  • Save kizu/9909593 to your computer and use it in GitHub Desktop.
Save kizu/9909593 to your computer and use it in GitHub Desktop.
Untitled
.image-container
{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 50%;
background: lightyellow;
overflow: hidden;
text-align: center;
white-space: nowrap;
}
.image-container:after
{
content: "";
height: 100%;
display: inline-block;
vertical-align: middle;
}
.image-container:before
{
content: "Я";
position: absolute;
left: 50%;
top: 50%;
margin-left: -20px;
margin-top: -20px;
width: 40px;
height: 40px;
border: 4px solid white;
background: darkred;
border-radius: 100%;
color: white;
font-weight: bold;
line-height: 40px;
text-align: center;
}
.image
{
vertical-align: middle;
max-height: 100%;
max-width: 100%;
}
.info
{
position: absolute;
padding: 10px;
top: 0;
right: 0;
left: 50%;
border: 1px dotted lightgray;
font-size: 14px;
line-height: 1.5;
}
<div class="image-container">
<img class="image" src="http://festival.1september.ru/articles/594478/presentation/23.jpg">
</div>
<div class="image-container">
<span class="image-wrapper">
<img class="image" src="http://festival.1september.ru/articles/594478/presentation/23.jpg">
</span>
</div>
<p class="info">
<b>Задача:</b> Отцентрировать иконку относительно тянущейся картинки. <br />
<b>Решение:</b> Обернуть картинку в блок, который будет повторять размеры картинки и спозиционировать иконку относительно него. <br />
<b>В чем проблема:</b> При первой отрисовке обертка соответствует размерам картинки, однако при ресайзе все портится — по высоте блок соответствует, а по ширине нет.
</p>
.image-container
{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 50%;
background: lightyellow;
}
.image-wrapper
{
position: relative;
display: inline-block;
height: 100%;
background: yellow;
}
.image-wrapper:after
{
content: "Я";
position: absolute;
left: 50%;
top: 50%;
margin-left: -20px;
margin-top: -20px;
width: 40px;
height: 40px;
border: 4px solid white;
background: darkred;
border-radius: 100%;
color: white;
font-weight: bold;
line-height: 40px;
text-align: center;
}
.image
{
display: block;
max-height: 100%;
width: auto;
}
.info
{
position: absolute;
padding: 10px;
top: 0;
right: 0;
left: 50%;
border: 1px dotted lightgray;
font-size: 14px;
line-height: 1.5;
}
{"view":"split-vertical","fontsize":"100","seethrough":"","prefixfree":"1","page":"css"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment