I have a section with text and images inside it, each wrapped in paragraphs.
The problem is that I want to make the text paragraph narrower (ex. 450px
) but keep the image as it is (full image width ex. 640px
if screen resolution allows it or full screen width).
It would be easy if i could tell which paragraph holds the image and which holds the text so i can style it. However the content is dynamic and there is no class to tell them appart.
The first thing i did was fix the paragraph width (to 450px
for example) but that made limits the image too. If i remove the max-width:100%
from the image then the image is larger than the text(640px, text stays at 450) but it is no longer responsive on lower resolutions.
Do you have any suggestions ?
Js fiddle link .The basic example (js fiddle contains the working version with base64 code)
HTML:
<div class="wrapper">
<section class="section-class">
<p>Make this 450px wide for text only. Image needs to stay full width when resolution permits and scale to 100% with lower res<p>
<p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
<p>
<!-- How to style this pargraph dynamically / with a css selector ? -->
<img src="data:image/png;base64,.../>
</p>
</section>
</div>
CSS:
.section-class {width:100%;}
.section-class img {max-width:100%;}/*keeps the image responsive on lower res*/
.section-class p {
/* Text needs to be narrower than the image, say 450px. The image needs to stay full width or scalable with screen width for lower res.*/
/* Tried to make text only: max-width:50%; or width:450px;*/
}
.wrapper {width:700px;}
@media (max-width: 767px) {.wrapper {width:100%;}}
@media (min-width: 1200px) {.wrapper {width:860px;}}
Expected output: something like this http://jsfiddle.net/qvkzLs3x/1/ except the image should be responsive / i.e. have the full screen width for lower res.
Update
I believe what i am trying to achieve is similar to Is there a CSS parent selector? To the best of my knowledge CSS at this point does not have such a feature.
Perhaps CSS4 will be of use with has
, similar to jQuery's has()
.
` tag before the `
` ? That's why the image gets affected by adding `max-width` to paragraphs..
– Razvan B. Jun 24 '15 at 10:13