Reputation: 15824
In a web app I'm developing, users can upload photos (with captions) and share with one another.
I display each photo like a polaroid, using CSS to create the effect. For instance:
The design is responsive to varied screen-sizes (it's a requirement):
Here's the CSS code:
.canvas{
background-color:white;
padding:10px 10px 30px 10px;
display:inline-block;
border-radius:6px;
}
.image{
max-width:400px;
width:100%;
border-radius:12px;
}
.caption-canvas{
max-width:400px;
text-align:center;
margin-bottom:-10px;
margin-top:1em;
}
.caption{
font-size:130%;
color:grey;
}
<div class="canvas">
<img src="umb.png" class="image">
<div class="caption-canvas">
<span class="caption">This is an orange umbrella</span>
</div>
</div>
So far so good.
But problems arise if the caption is a long string without breaks. E.g.:
However not to worry, one can add overflow-wrap: break-word;
(to .caption
) to take care of this:
But this gives birth to a new problem: The polaroid canvas doesn't scale down with reduction in resolution any more, and overflows its container. I.e.:
I can fix this responsiveness problem if I use word-break: break-all;
instead. However I don't want to use that. It doesn't perform well for English text, so it's out:
However, that's precisely the kind of responsiveness I need. Can someone help me find a workaround that incorporates overflow-wrap: break-word;
and is fully responsive? Or is that not possible?
Ideally, I don't want to have to change my CSS too much - it's working as expected in most cases, and it doesn't employ CSS3 (which I need to avoid to cater to older devices - another one of my requirements). I'd prefer well-supported CSS properties as per caniuse.com.
Update: Adding width:100%
and max-width:400px;
to .canvas
almost solves the problem, except the polaroid canvas starts overflowing the containing div even though it does get smaller with res. Behold:
That blue strip in the background is the containing div (and it's bigger than that, but cropped in the image).
Upvotes: 1
Views: 185
Reputation: 693
♦ in your (.canvas
) Add this :
-webkit-box-sizing : border-box;
-moz-box-sizing : border-box;
box-sizing : border-box;
✘ and from your html its better if you remove this :
<div class="card" style="margin-top:1.3em;background-color:#BDEBFE;text-align:right;">
✔ and replace it with this iD :
<div id="card">
then in your css use :
#card {
margin-top:1.3em;
padding:6px;
background-color:#BDEBFE;
text-align:right;
max-width:400px;
}
.canvas{
-webkit-box-sizing : border-box;
-moz-box-sizing : border-box;
box-sizing : border-box;
background-color:white;
padding:10px 10px 30px 10px;
display:inline-block;
border-radius:6px;
max-width:400px;
width:100%;
}
Example : Click here
Note : why we used ID in the first container instead of class ?
in your specific settings and because your using 2 containers one inside another and both uses 'class',
somehow it seems to not allow that first container to show the background even when usingpadding
,thats why we use ID in the First Container instead , and thats why i told you before to use border in your.canvas
better and get ride of thatcard
div container . but if u want it with 2 containers then u most use an ID first .
Upvotes: 3
Reputation: 2597
You can use media query and something like this for small screens:
text-align: justify;
overflow-wrap: break-word;
I do not see Your HTML but overflow-wrap only for .caption
shouldn't have impact on image resize if image is in different div ... ok I see it...
just add this two rules not for .canvas
but for .caption-canvas
or even for .caption-canvas .canvas{ }
Ps. You have two .canvas
classes, one for container and one for it child.
Upvotes: 0
Reputation: 101
If I understand correctly, you are looking for a way to show long words without breaking them. At some point there will be no more room, so the only other way to show long words without breaking them is to use a smaller font on smaller screens.
What you can do is use hyphens
to allow hyphenation. Just add hyphens: auto
to your .caption
More info on hyphens: https://css-tricks.com/almanac/properties/h/hyphenate/
About that container overflow: I added word-break: break-word
to your .caption
class, which made the polaroid scale down but break the long words - better the long words than all words i guess ;) See everything in the codepen.
https://codepen.io/anon/pen/aEvzwM
Upvotes: 0