-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting default max-width and height for img tag #506
Comments
A couple things...
That begs the question: what unexpected results did you experience? |
Updated Codepen to point out the issue. |
I don't understand the problem. If I remove this from the Codepen:
...the result is the same. Also, did you intentionally use a different |
Yeah that's because of the wrapping flex container. If you remove this line: |
Ah, my bad. The result is not the same; my browser was too small for me to notice the difference. OK, I see what's going on. I would say that this is an edge case as it's only a problem with images in flex containers. In fact you can solve it by wrapping the
...doesn't work because in this specific case, the problem is not even |
Okay thanks. Well putting the image in an extra div or adding I really needed some time to figure out where this unexpected behavior did come from. Tailwind already provides a |
It is and it isn’t. I agree that it’s unfortunate that Flexbox behaves this way with images, but I’m pretty sure it’s per the spec (i.e. not a browser bug). The default value of
Just in case it wasn’t clear, this is not even caused by |
Here's a Codepen showing the same issue without https://codepen.io/adamwathan/pen/dKKMJb I'll admit I'm tempted to remove these styles completely and just let people use |
Removing these styles completely would also be a good option. My point was that as long as Preflight has |
In fact, I already add this to my own projects:
The |
I agree with @adamwathan setting In the Codepen by Adam the image gets another aspect ratio due to the fact it is placed in a parent div with a width of 100px. But the image stays at a height of 1000px as I would expect. If you bring back the I would solve the auto height issue mentioned by @adevade like this. This feels more natural to me instead by just setting it by default to the I also agree with Adam that it's not completely unexpected because flex items do stretch to fill the container. But ending up with a weird aspect ratio of an image isn't really desirable. |
What about adding |
Even if the |
@grund3g I agree with you. And since
In fact I ended up undoing that rule (as much as possible, because like you said you can't fully undo That being said, Preflight is completely optional and Adam has said on Twitter that he was working on a way to let plugins define custom "base styles". So you could make your own reset plugin that doesn't include the above rule, should Adam decide not to remove it from Preflight. |
If someone wants to open a PR to remove that from Preflight and target the |
Just for reference, here's a truly simplified demo that illustrates the core problem, which is that if you specify an explicit height using the https://codepen.io/anon/pen/GeOoJe You can see there I am trying to force the image to stretch but I can't make it happen. Now that I'm typing this though I'm realizing the obvious solution is to use inline styles instead of the width and height attributes: https://codepen.io/anon/pen/GeOoJe 🤦♂️ So I think I might revert this change since it's actually trivial to work around... |
This CodePen shows how inline styles fixes the original issue: |
This feels counter intuitive to me. Max width 100% and Height auto both exist as a class, why wouldn't you just add them as you see fit? Or declare them globally if really you want to. Forcing them means it's difficult to override, especially for content generated by authors through a CMS for example. We've gone from..
to
Please consider removing it again. |
Came across another reason to consider removing these default styles. max-width being the culprit this time: https://codepen.io/himynameisphil/pen/poojXwr |
That just drove me nuts. It was unexpected. That I can't use the height attribute is for me too much of an opinion as it disables a default HTML behavior. And I didn't find it documented anywhere. Seems like I can't disable it, right? So you first have to figure out what happens, which wastes time and then use inline styling to overwrite it? That is kind of counter intuitive and not very elegant. :/ Edit: Sorry for the morning rant 😅. Just wanted to put an image somewhere and it didn't work like expected. Why do you think it is necessary to have that default styling? |
Update: I now realise this is all ages old; will have to debug some more it looks like :-) |
Just spent the better part of a day figuring out why my So is there any way to stop Tailwind from adding |
This issue pops up from time to time. I'm personally fine with how it works today, but I see that some people above are having issues. Mostly with content from CMS's it seems. Could this, or some variation of it, maybe be a solution? I'm talking for the prefligh styles in the framework. img:not([height]) {
height: auto;
} https://codepen.io/adevade/pen/PopQrYZ?editors=1100 Sorry to bother you @adamwathan, but maybe you could take a quick look at this or make a new comment, since it was a couple of years ago now. Thank you. |
@adevade you're right, my use case is CMS-like as well. I have user-generated markdown content which I parse and then style with tailwindcss-typography so in order to reach in and add explicit height styles I have to parse the HTML with JavaScript, which is a shame because I love how hands-off it can be otherwise. |
Hey @adevade! That would be a great solution but it has a higher specificity than a class: That means that classes like @samtstern For this problem I've typically just recommended people use inline styles instead of the height attribute, which will always work and override the |
Is there a simple way to make this configurable? |
I think you could add this to your own CSS (haven't tested): img {
height: unset
} |
This doesn't work per spec. |
This behavior was extremely unexpected. I think it would be best to not add styles you can't easily undo. |
I also ran into this issue today when using the nuxt/image module in combination with tailwindcss |
Having the same issue that the |
I found one way to resolve this problem, maybe it may be bad, but it works
|
Here's a PNPM patch to remove these styles: # patches/[email protected]
diff --git a/lib/css/preflight.css b/lib/css/preflight.css
index e5e52cd8f32310089aebc65cfd943248cfe7b92e..c32f29ba3b8e189b0b3471c3d0d69da64fec0a50 100644
--- a/lib/css/preflight.css
+++ b/lib/css/preflight.css
@@ -362,16 +362,6 @@ object {
vertical-align: middle; /* 2 */
}
-/*
-Constrain images and videos to the parent width and preserve their intrinsic aspect ratio. (https://github.com/mozdevs/cssremedy/issues/14)
-*/
-
-img,
-video {
- max-width: 100%;
- height: auto;
-}
-
/* Make elements with the HTML hidden attribute stay hidden by default */
[hidden] {
display: none; |
The same issue, could not use a Next.js Image component |
Still facing the same issue, are there any updates? |
Any updates on how to use with Next js Image?? |
@adamwathan, The issue was opened in 2018 and still the issue persists. I am using Next js Image component and need to workaround the height auto by explicitly defining the height utility. Is there not any better way to do this?? Here is the workaround I have been using
|
up |
Any updates on this? |
The issue persists: when using the Image component in Next.js, the width and height styles are being overridden. |
Pull request #426 sets height to auto in combination with a default value of max-width to 100% to the
img
tag.This gives unexpected results.
See Codepen.
Loading a 100x200px image fills up the whole space although I am setting width and height specifically.
@adevade mentioned code from Bootsrap, but this is meant for a fluid image with class name
image-fluid
.I took a look at the source code of BeardCSS.
BeardCSS is setting only a max-width of 100%, if width or height attributes are set they will be respected by resetting max-width to none on the
img
tag.I think we should take the BeardCSS approach.
Example of same code as above with the BeardCSS approach gives the expected result.
The text was updated successfully, but these errors were encountered: