I have a div
container where I want to put in a centered image and a small description to the right. The specifications are:
I've tried using a table
in the container and using div
s, but I can't find a clean solution. I can show you the non-working code I've tried on request.
This one was a tricky one. The actual tags used don't really matter, just the number of elements and how they are nested.
The vertical centering of the text is done via Flexbox, which has limited support (Chrome, IE10, Opera, Safari, most mobile browsers). Firefox (with 2009 Flexbox properties) would normally be lumped in here, but it has a bug that prevents Flexbox from working if it is applied to an absolutely positioned element.
http://cssdeck.com/labs/iu0gx2wk
Markup:
<div class="gallery">
<article>
<h1>My image title</h1>
<img src="http://placekitten.com/640/480" alt="A really cute kitten" />
</article>
</div>
CSS:
.gallery {
padding: 0 230px;
text-align: center;
}
article {
display: inline-block;
max-width: 100%;
position: relative;
}
img {
max-width: 100%;
vertical-align: text-bottom;
}
h1 {
position: absolute;
text-align: left;
right: -230px;
top: 0;
bottom: 0;
width: 200px;
margin: 0;
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
Alternately, you could add one extra element and use the table/table-cell display properties to get your vertical centering.
http://cssdeck.com/labs/lqgjgghw
Markup
<div class="gallery">
<article>
<h1><span>My image title</span></h1>
<img src="http://placekitten.com/640/480" alt="A really cute kitten" />
</article>
</div>
CSS
.gallery {
padding: 0 230px;
text-align: center;
}
article {
display: inline-block;
max-width: 100%;
position: relative;
}
img {
max-width: 100%;
vertical-align: text-bottom;
}
h1 {
position: absolute;
text-align: left;
right: -230px;
top: 0;
bottom: 0;
width: 200px;
margin: 0;
display: table;
}
h1 span {
display: table-cell;
vertical-align: middle;
}
* note that the demos have their padding/positioning off by a little bit, the code listed here is correct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.