Kadeem L
Kadeem L

Reputation: 863

Make text relative to responsive image

I'm having trouble making some text responsive to the browser, and relative to the image.

This is what I have so far

As you can see, there's a responsive image, but when the browser goes smaller, the text goes out of proportion, I'm trying to make the process more seamless than it currently is. I hope I have explained this properly. It's all a bit confusing.

Here's what I have so far:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>

.image {position: relative; text-align: center;}

.image span {position: absolute; line-height: 20px; display: block; top: 50%; 
    margin-top: -10px; width: 100%; color: white;}

img.ri
 {
 position: relative;
 max-width: 75%;
 display: inline-block;
 vertical-align: middle;
 }

 @media screen and (orientation: portrait) {
 img.ri { max-width: 90%; }
 }
 @media screen and (orientation: landscape) {
 img.ri { max-height: 90%; }
 }

 img#hv {
 filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
 filter: gray; /* IE6-9 */
-webkit-filter: grayscale(1); /* Google Chrome & Safari 6+ */
 }

 img#hv:hover {
filter: none;
-webkit-filter: grayscale(0);}

</style>
</head>
<body>

<div class="image">
 <img src="http://static.inqmind.co/content/2013/05/aap-mob-always-strive-and- prosper/feature.jpg" class="ri" id="hv">
 <span>ASAP ROCKY - Jack New City</span>
</div>
</body>
</html>

Upvotes: 1

Views: 1655

Answers (2)

yckart
yckart

Reputation: 33438

There's currently no comfortable css-solution, you've to use some simple javascript calculations:

http://fiddle.jshell.net/yJ8wh/4/

var img = $("img");
var compressor = 14;
$("span").css({
    fontSize: Math.max(Math.min(img.width() / (compressor * 1), parseFloat(Number.POSITIVE_INFINITY)), parseFloat(Number.NEGATIVE_INFINITY)) + 'px'
});

The above code is adopted from fitText.js: http://fittextjs.com/

Upvotes: 1

G-Cyrillus
G-Cyrillus

Reputation: 106058

You already use mediaqueries, why not as well for font-size ?
example : http://jsfiddle.net/GCyrillus/yJ8wh/6/

@media screen and (max-width:500px) {.image {font-size:0.5em;}

Upvotes: 1

Related Questions