Я пытаюсь достичь следующего визуального эффекта:Как выровнять по вертикали изображение относительно другого элемента?
Я пробовал различные вещи, но не смогли сделать это. Вот наиболее упрощенная версия этого на codepen:
Ссылка на это codepen. HTML является:
<div class="outerContainer">
<button>LOREM IPSUM</button>
<!-- This is the question mark image -->
<img src="http://i60.tinypic.com/adoqiv.png">
</div>
CSS является:
// Just to see the border for testing
$test-border-width: 3px;
$test-border-style: dotted;
.outerContainer {
border: $test-border-width $test-border-style green;
// Don't span entire width because later will center container
display: inline-block;
button {
border: $test-border-width $test-border-style red;
// I would like to be able to change the font size
// without having to worry about the question mark image,
// that is, for the image to be automatically vertically aligned
// in the middle.
font-size: 4em;
}
img {
border: $test-border-width $test-border-style cyan;
}
}
Совет высоко оценили. Также, если это возможно, рассуждения позади.
Отлично работает. Сможете ли вы объяснить, почему это работает? – SBel
Он просто использует свойство [vertical-align] (http://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align). Я добавил его к обоим элементам, чтобы вертикальное выравнивание относилось к более высокому элементу. –