0

I'm working with a framework where a global css rule

img {
        width: 100%;
    }

is set - that I cannot change. Now I'm working with a WYSIWYG editor that resizes images by setting their (old) width property. Is there any chance to reset it to the element's size via css?

e.g.

.container img {
    width: unset; // (or initial)
}

But that obviously doesn't work. Element inspector tells me

initial
removed: 100%
removed: 100px (value of the width property)

Here is a Stackblitz showing the issue: https://stackblitz.com/edit/web-platform-rjtfrt?file=styles.css

I want the images' width to be 200 by just changing .container img.

3
  • 1
    You can override the property value using !important Commented Oct 5, 2021 at 19:24
  • What do you mean by 'old' width property? Commented Oct 5, 2021 at 19:31
  • Added an example - sorry, should have done this sooner. Commented Oct 5, 2021 at 20:53

2 Answers 2

0

Initial or fit-content are values your are looking for. Depends if you want to set real image size or width attribute value.

Check two examples below.

img {width: 100%;}
.container #img1 {width: initial;} /* taking real image width (300px) */
.container #img2 {width: fit-content;} /* taking width attribute value (200px) */
<div class=container>
  <img id="img1" width=200 height=200 src="https://picsum.photos/300/200">
  <img id="img2" width=200 height=200 src="https://picsum.photos/300/200">
</div>

A brief about browser support at https://caniuse.com/?search=fit-content

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

6 Comments

Sorry, the question was a bit unclear - I added an example.
@tris: I think I gave you a solution. Check code snippet here. First image has 300px width (real image size), the second one 200px (attribute width). Why do you think it isn't what you asked?
It somehow isn't working for me - check my stackblitz.
@tris: Hmm, it works correctly in Mac/Safari, in Mac/Chrome doesn't... My fault, sorry.
How I love that we still haven't left these browser dependent struggles 😉 But that you for checking! So I guess there is no solution? Super hacky: it works for width-style (but not property) when setting a broken value ... stackblitz.com/edit/web-platform-rjtfrt?file=styles.css But I guess so there is no solution? Really?
|
0

Just found this questions: CSS, how to size an img back to its width attribute and it really seams like there is no solution. Please correct me if I'm wrong!

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.