Wenn der Daumen eine bekannte Breite hat, dann kann man display:inline-block;
, vertical-align:middle
, text-align:center;
und eine negative margin
verwenden. Dies kann absolute
und transform
vermeiden.
.project-thumb,
.project-thumb h3 {
display: inline-block;
vertical-align: middle;
position: relative;
width: 150px;/* thumb's width */
box-sizing: border-box;/* if you add borders or padding*/
color: initial
}
.thumb {
vertical-align: middle;
margin-right: -150px;/* reduce virtually width to 0 to pull text */
}
.thumb-title {
z-index: 1;/* make sure it stands on top of img*/
text-align: center;
}
<a class="project-thumb" href="ktc.html">
<img class="thumb" src="http://dummyimage.com/150x150/aaaaaa/aaaaaa&text=."><!--
mind this white space for inline-block elements, comment erase it
--><h3 class="thumb-title">
Sample text</h3>
</a>
<a class="project-thumb" href="ktc.html">
<img class="thumb" src="http://dummyimage.com/150x150/aaaaaa/aaaaaa&text=."><!--
mind this white space for inline-block elements, comment erase it
--><h3 class="thumb-title">
2 lines <br/> of text</h3>
</a>
Edit: sieht aus wie Sie eine andere Antwort kommentiert, wo man über eine Flex-Box gesprochen? hier eine Flex-Version, aber nur eine Vermutung von Ihrem realen Layout & CSS
.project-thumb,
.project-thumb h3 {
display: inline-flex;
position: relative;
width: 150px;/* thumb's width */
box-sizing: border-box;/* if you add borders or padding*/
color: initial;
justify-content:center;
}
.thumb {
margin-right: -150px;/* reduce virtually width to 0 to pull text */
}
.thumb-title {
z-index: 1;/* make sure it stands on top of img*/
margin:auto 0;
}
<a class="project-thumb" href="ktc.html">
<img class="thumb" src="http://dummyimage.com/150x150/aaaaaa/aaaaaa&text=."><!--
mind this white space for inline-block elements, comment erase it
--><h3 class="thumb-title">
Sample text</h3>
</a>
<a class="project-thumb" href="ktc.html">
<img class="thumb" src="http://dummyimage.com/150x150/aaaaaa/aaaaaa&text=."><!--
mind this white space for inline-block elements, comment erase it
--><h3 class="thumb-title">
2 lines <br/> of text</h3>
</a>