1

Here is a simplified page struture, where I want to select all images inside the "page"-div and enclosed elements, but not those that are in either "keepoffa" or "keepoffb".

<!DOCTYPE html>
<html lang="en">
  <body>
    <div class="page">
       <img/> <!-- OK -->
       <div class="keepoffa">
          <img/> <!-- never take this-->
       </div>
       <div class="keepoffb">
          <img/> <!-- never take this-->
       </div>
       <img/> <!-- OK -->
       <div class="subpage">
           <img/> <!-- OK -->
           <ul>
               <li>
                   <img/> <!-- OK -->
               </li>
           </ul>
       </div>
       <ul>
           <li>
               <img/> <!-- OK -->
           </li>
       </ul>
    <div>
  </body>
</html>

Here's what I have thought:

.page img:not(.keepoffa):not(.keepoffb) {
    max-width: 300px;        
}

But the unwanted divs are not excluded.

How to effectively select the images but exclude the images inside those unwanted divs? CSS-only required.

3 Answers 3

1

Use > operator to select the child of .page div. And .subpage img to the another image

.page > img,
.subpage img{
  border: 1px solid red;
}
  
  <div class="page">
       <img src="http://placehold.it/100x100"/> <!-- OK -->
       <div class="keepoffa">
          <img src="http://placehold.it/100x100"/> <!-- never take this-->
       </div>
       <div class="keepoffb">
          <img src="http://placehold.it/100x100"/> <!-- never take this-->
       </div>
       <img src="http://placehold.it/100x100"/> <!-- OK -->
       <div class="subpage">
          <img src="http://placehold.it/100x100"/> <!-- OK -->
       </div>
<div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that is a good idea. Unfortunately, the real page is more complicated than my simplified example above. Thus, working with immediate children selectors does not work that easy. I would have to include paths for every possible layout path. This will get too complicated. I will update the example a bit to represent this better.
0

You can't use two pseudo-selectors simultaneously (and your selectors weren't correct anyway).

EDIT: Probably the easiest way is something like this:

    .page img {
        max-width: 300px;
    }

    .keepoffa img, .keepoffb img {
        max-width: initial;
    }

5 Comments

this also needs .page > img added on
@PraveenKumar Yep :) CSS is often quite funky
@Mike Yep you're correct - misread his original question. Going to update my answer when I get a chance
Hm, this seems not to exactly work, since it an image is only inside one of them, it still gets selected with the other one.
@Marcel Oh yes, I forgot about that. I ran into a similar issue a few months back. Let me re think this
0

Try this:

.page>img,
.page div:not(.keepoffa):not(.keepoffb) img {
    max-width: 300px;        
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.