0

I have a lot of classes that end up like.

.img-small {
  color: #202124;
  display: block;
  font-size: 10px;
  font-weight: 700;
  line-height: 12px;
  max-width: 13px;
}

.img-med {
  color: #202124;
  display: block;
  font-size: 10px;
  font-weight: 700;
  line-height: 12px;
  max-width: 23px;
}

In this case the only difference being the width and very often these classes are used for exactly one image.

I'm tempted to use the width property but I know that's deprecated, so then I'm tempted to use a class that all it does is set the width (i.e. class="img-base w-23"), but is there really no better way so that I can override just one field? If possible I'd like to do something like.

.img-small {
  color: #202124;
  display: block;
  font-size: 10px;
  font-weight: 700;
  line-height: 12px;
  max-width: 13px;  
}

.img-med = img-small {
  max-width: 23px;
}

Is there something like this in vanilla CSS?

2
  • 1
    Why not .img-small, .img-med {…all properties except max-width} .img-small { max-width: 13px; } .img-med { max-width: 23px; }? Commented Mar 6, 2021 at 3:52
  • 1
    Maybe another approach could be making a generic .img related item that contains the settings that would be common for all or most of your image related styles. Then, you could make specific .img-small (using your naming) type items with settings that are different. Later, when you want to use the CSS somewhere, just make sure to include both the common .img part as well as the specific .img-small part in your HTML or wherever. Commented Mar 6, 2021 at 3:53

1 Answer 1

3

You could use 2 classes per element. (side note: why are you setting your images to be only 13 pixels wide? that's very tiny)

.img {
  color: #202124;
  display: block;
  font-size: 10px;
  font-weight: 700;
  line-height: 12px;  
}
.img-med {
  max-width: 23px;
}
.img-small {
  max-width: 13px;
}
<img class="img img-med" src="https://via.placeholder.com/300/09f/fff.png"/>
<img class="img img-small" src="https://via.placeholder.com/300/09f/fff.png"/>

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

1 Comment

I figured that'd be the answer. These are logos at the bottom of emails so not meant to to look at for detail. I guess my fear of using two classes stems simply from what if-ism where I'm thinking about how most images will need to be tagged as both img-base and a size, rather than as just a size.

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.