Dynamically adjust the styling of paragraphs within a section based on their content to create a

In one section on my page, I have both text and images wrapped in paragraphs.

My issue is that I want the text paragraph to be narrower (e.g. 450px) while keeping the image at its full width (e.g. 640px if screen resolution allows or full screen width).

The challenge lies in differentiating between the text and image paragraphs without any distinguishing classes due to dynamic content.

Initially, I set the paragraph width to 450px, but this constrained the image as well. Removing max-width:100% from the image made it larger than the text (640px vs. 450px), but responsiveness was lost on lower resolutions.

Any suggestions or solutions for this problem?


View Example on JsFiddle. The demonstration includes 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 similar to this example link, except ensuring the image remains responsive by taking full screen width on lower resolutions.


Update

I am looking for something akin to Is there a CSS parent selector?. However, as of now, CSS lacks such functionality.

Possibly, CSS4 featuring has may prove helpful, resembling jQuery's has().

Answer №1

Give this a shot:

.section-class p {
   width:90%;
   max-width:450px;
}

Check it out here in the fiddle!


update

If you want to ensure that an image remains responsive on your site (starting from a specific breakpoint), you can include a media query similar to this one:

@media (max-width: 767px) {
  /* ... */
      .section-class > p > img {
         width:100%;
         height:auto
      }
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changes on services do not affect the Angular component

Currently facing an issue with my Angular assignment where changing an element's value doesn't reflect in the browser, even though the change is logged in the console. The task involves toggling the status of a member from active to inactive and ...

Guide to including a class within an "else" statement in JavaScript

function check_empty() { if (document.getElementById('CompanyName').value == "" || document.getElementById('ContactName').value == "" || document.getElementById('Address').value == "" || document.getElementById('PhoneNumb ...

What are the advantages of using clearfix for clearing floats in CSS?

Recently, I encountered an issue with using clearfix to clear the floating effect in CSS. Someone mentioned that it may not be the best technique and suggested using alternative methods. Is this information accurate? If so, what are some other options tha ...

JavaScript: Modifying the Style of a :lang Selector

Currently, I am working on creating a basic multilingual static website using only HTML5, CSS3, and vanilla JavaScript. The structure of the HTML looks something like this: <html> <head> <title>WEBSITE</title> ...

Retrieve the input value without a specified ID in HTML

After searching extensively, I have not been able to find a solution to my issue. I have a table that displays data from a database and in jQuery, I have successfully implemented a functionality where a row can be added with empty inputs and then submitted ...

Is your CSS font italic shorthand not functioning properly?

I attempted to add italic styling to text using the font shorthand property. This is what I currently have: font: 36px italic normal Georgia; However, the italic styling is not being applied. When I specifically set font-style: italic;, it does work. ...

Issues with Collision Detection between Canvas Rectangles and Balls

I am developing an HTML5 canvas game and encountering an issue with collision detection. The problem occurs when the ball collides with any platform - the ball's gravity should change to -1 and move upwards at the same velocity as the platforms. Howev ...

I found myself continuously revolving four images along a circular border, each time revealing the corresponding content. However, I only required the content from one of the images at a time

I attempted to animate the rotation of a circle and display the content of a specific image by pausing it for a period of time. However, I am unsure how to halt the animation and display the content of the specified image. Here's what I tried: HTML C ...

Transfer an HTML file generated in real-time to an external server using SFTP

I am currently working on creating an HTML file dynamically using the file_put_contents function and the ssh2 library for SFTP. However, I am facing an issue where Apache is reporting that the file cannot be sent because it does not exist on the local dis ...

What is the best way to switch the CSS style of a button that has been mapped

I'm currently working on toggling the CSS class for an individual button that is created from a mapped array. Although my code is functional, it toggles the CSS class for all buttons in the mapped array instead of just the one selected. ...

Applying Vue Quill CSS to the initial Quill Editor component exclusively and tips for personalizing the toolbar

I have encountered an odd CSS issue while using the vue-quill package in my project. Each comment has its own quill editor for replying, and I temporarily set the isOpen prop to true for debugging purposes. This results in a quill editor being displayed un ...

What are some strategies to prevent a fixed sidebar from obscuring content while zooming in?

I've been working on adding a fixed sidebar for a table of contents (TOC) to a website, where the sidebar stays in place as the user scrolls up and down. My implementation includes the following HTML code: https://i.sstatic.net/jAY8l.png and CSS co ...

Having trouble converting the value into Vue2Editor

I'm currently utilizing vue2editor and encountering an issue on the edit page where I am attempting to pass the current content value to the vue2editor. Despite my efforts, the vue2editor still displays blank content as if no value was passed. Below ...

Experience the convenience of Visual Studio Code's auto-completion feature for HTML tags even when working within an

My HTML file has a babel script embedded within it. <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React tutorial</title> <script src="https://unpkg.com/react@16/umd/react.development.js" ...

Issue encountered when attempting to remove an element from an array using the delete method

Whenever I attempt to remove an input that has been added, an error message saying "Value is NULL" pops up. I'm looking for a solution where only the selected inputs are deleted when I click on DELETE, instead of all inputs being removed. The myFuncti ...

CSS responsive grid - adding a horizontal separator between rows

I currently have a responsive layout featuring a grid of content blocks. For desktop view, each row consists of 4 blocks. When viewed on a tablet, each row displays 3 blocks. On mobile devices, each row showcases 2 blocks only. I am looking to add a ho ...

Ensure that the columns are consistently displayed on a single row

On larger screens, everything displays correctly. However, when viewing on a phone, the issue arises. I want these two columns to always stay on one row, but currently they stack on mobile devices. How can I ensure that they resize and remain on the same ...

Even after attempting to conceal objects using display:hidden, there is still whitespace present on the webpage

Check out my code in action on JsFiddle by visiting the following link: https://jsfiddle.net/2yvjm4h1/ Even after using display:none; to hide my objects rather than visibility:hidden before showing them with JavaScript, I still notice a significant amount ...

Any issues with the color scheme and text size in the main.css file? Seeking solutions!

Image version: https://i.sstatic.net/ExaGb.png I'm encountering some difficulties when trying to update the appearance of my page PrintExtraInfo.aspx. In PrintExtraInfo.aspx: <asp:Table ID="Table1" runat="server"> <asp:TableRow> < ...

Safari on IOS transition transform feature malfunctions

Every time I try to apply some code to make a div move, for example using the latest iOS Safari browser, it fails to transition between the two rules set. I have experimented with different values other than percentage but still haven't been able to m ...