I can’t access the child (div) to change the background image. I want to change it by it’s parent. How?
<section class="l-section wpb_row frenach-header">
<div class="l-section-img" role="img" data-img-width="1587" data-img-height="700" style="background-image: url(https://example.com/wp-content/uploads/2024/03/french-header.webp);"></div>
</section>
How can I access div tag through its parent (section) to change the background image?
3
Answers
You would need to create a certain CSS snippet to create a hierarchy of parent to child properly.
See this example for reference:
Here, the format of hierarchy is —
.[parent class] [child class]
I am not sure if I understood your question correctly.
Solution #1:
Have you tried variables? something like that:
Solution #2:
You can use selectors:
You can use
querySelector
. The followingquery
search for adiv
insidesection
that has a attribute named ‘role’, which is set to ‘img’ and returns the first one. Then you can access the background image URL though.style
.