Jump to content

How to assign a width to parent (!) of <img> element?


pstein

Recommended Posts

Assume I have the following simplified code
 

<div>

<img class="dummy">....</img>

</div>

Ok, I can now assign a new width (overwrite) to the <img> element by entering

img.dummy { width: 50% !important; }

But what if I want to assign the width to its parent element (here the <div>)?

How can I assign an attribute+value to this unknown element (without ID or class)?

Keep in mind I have no access to the original webpage+CSS source but want to assign it later from script.

 

Link to comment
Share on other sites

CSS only selects downwards and forwards so that the rendering engine only needs to do a single pass through the HTML document.

What you have to do is find the closest ancestor with a class or ID and reach the div through a series of child and element selectors. The resulting selector might look something like this:

.something > section:nth-child(2) > div:nth-child(4)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...