I’m trying to create a responsive rectangle that has:
height
that is 62% ofwidth
background: linear-gradient
- inside the rectangle is an image centered vertically and horizontally with a maximum size, all images have the same
width: 400px
but a differentheight
What I have done before:
- for getting responsive rectangle I used this approach:
.responsive-rectangle {
width: 100%;
max-width: 450px;
background: -webkit-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -moz-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -o-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: linear-gradient(to top, #0071B4, rgba(0, 113, 180, .8));
}
.responsive-rectangle:before {
content: "";
display: block;
padding-top: 62%;
}
<div class="responsive-rectangle"></div>
- to align the image inside rectangle I have used
display: flex;
andtext-align:center;
with an.img-wrapper
:
.responsive-rectangle {
width: 100%;
max-width: 450px;
background: -webkit-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -moz-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -o-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: linear-gradient(to top, #0071B4, rgba(0, 113, 180, .8));
display: flex;
text-align: center;
}
.responsive-rectangle:before {
content: "";
display: block;
padding-top: 62%;
}
.image-wrapper {
margin: auto;
}
img {
width: 100%;
height: 100%;
}
<div class="responsive-rectangle">
<div class="image-wrapper">
<img src="https://res.cloudinary.com/zeek/image/upload/v1429436724/whiterryxmsuesx78joy9n9sa.png" alt="">
</div>
</div>
This works perfect in case with image 400px x 220px
,
but with images that have a bigger height
the correct aspect ratio is not used:
.responsive-rectangle {
width: 100%;
max-width: 450px;
background: -webkit-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -moz-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -o-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: linear-gradient(to top, #0071B4, rgba(0, 113, 180, .8));
display: flex;
text-align: center;
}
.responsive-rectangle:before {
content: "";
display: block;
padding-top: 62%;
}
.image-wrapper {
margin: auto;
}
img {
width: 100%;
height: 100%;
}
<div class="responsive-rectangle">
<div class="image-wrapper">
<img src="https://res.cloudinary.com/zeek/image/upload/v1444889083/o67qntlwitbxnqz5qyjn.png" alt="">
</div>
</div>
Is there any approach that can solve my problem?
Edit:
Oh I forgot to note that background-image
is not good solution because it does not support SEO.
2
Answers
I had to lay them out next to each other to see the issue. Here is a solution to your problem, just use the image as a background:
and then center it
http://jsfiddle.net/bmdqsqx1/1/
The problem with your final code is that the pseudo element used for keeping the aspect ratio is not actually performing its desired role. This is because
.responsive-rectangle
is set todisplay: flex;
. To maintain the aspect ratio you can make the following modifications:.responsive-rectangle:before
rule as the calculation can be done on.image-wrapper
itselfdisplay: flex;
from.responsive-rectangle
.image-wrapper
and replace them with:padding-top: 62%;
to ensure the correct aspect ratio is usedposition: relative;
to allowimg
to be positioned relative to.image-wrapper
width: 100%;
to ensure it will fill the entire width of.responsive-rectangle
img
and replace them with:bottom: 0;
,left: 0;
,margin: auto;
,right: 0;
andtop: 0;
to allow the image to be centered in.image-wrapper
position: absolute;
to position it relative to.image-wrapper
max-height: 100%;
andmax-width: 100%;
to ensure the image does not exceed its naturalheight
andwidth
while keeping its aspect ratiohttp://jsfiddle.net/p23pgqp3/