Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nesting CSS styling elements

I am trying to create a style sheet for an image gallery. I want to nest the styling elements so that they only apply when they are children of elements with a certain class. I’ve written the CSS like this:

.media {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
display: flex;
justify-content: center;
align-items: center;
img {
    max-width: 100%;
    height: auto;
  }
}

Where the corresponding HTML looks like this:

  <div class="media" style="margin: 3px;">
    <div class="layer">
        <p><b>Yamaha R3 </b><br> 300cc - quick & agile <br> Great for beginners</p>
    </div>
    <img src="{% static 'random_image.png' %}" alt="" />
  </div>

But none of the styling related to img that I’ve specified in the style sheet is actually being used. Am I nesting the styling rules incorrectly?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

CSS nesting isn’t yet available, unless you use CSS Pre-processor like SASS/LESS

You have to do this:

.media {
  width: 300px;
  height: 200px;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.media img {
  max-width: 100%;
  height: auto;
}

Or set a specific a class in your img

<img class="media-image" src="{% static 'random_image.png' %}" alt="" />

And in the CSS do this:

.media-image {
  max-width: 100%;
  height: auto;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading