CSS 撤消高度:auto;宣言
我正在向现有的文字新闻主题添加一小段代码。新代码所在的区域在样式表中被覆盖:
#content img {
height: auto; }
在插件/小部件中执行的代码中,我检索缩略图图像的信息,然后将它们显示在 img 标签中。根据图像的大小,我添加高度或宽度属性。 (顺便说一句,宽度效果很好)。当涉及到 height 属性时,由于 height: auto 声明,它会忽略它。
#content 中我的小盒子是#CPGimage。我可以覆盖高度,但在读取图像之前不知道适当的高度值。 (如果最大所需宽度为 100,最大所需高度为 100,则图像为 100x200,我将高度属性添加到 100 的 img 标签中)。
有没有办法撤消 #content 下的高度声明或以某种方式设置一些东西来告诉它从 img 标签获取高度?
I am adding a small bit of code to an existing word press theme. The area where the new code is is covered by this in the style sheet:
#content img {
height: auto; }
Within my bit of code which is executed within plugin/widget, I retrieve information on thumbnail images and then display them within an img tag. Depending on the size of the image I add either a height or a width attribute. (the width works fine btw). When it comes to the height attribute it ignores it due to the height: auto declaration.
My smaller box within #content is #CPGimage. I can overwrite height, but do not know the appropriate height value until I read the image. (if the max desired width is 100 and the max desired height is 100 image is 100x200 I add an height attribute to the img tag of 100).
Is there a way to undo the height declaration under #content or somehow set something that tells it to get the height from the img tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的目标只是限制图像的最大高度,您是否尝试过使用
最大高度
?If your goal is just to limit the maximum height of the image, have you tried using
max-height
?使用 css: 代替设置宽度或高度属性:
或者如果您有 jQuery :
instead of setting width or height attributes use css:
or if you have jQuery :
你尝试过吗
?华泰
Have you tried
? HTH
如果您使用 jQuery,您可以像这样
http://jsfiddle 那样获取
img
高度.net/jasongennaro/ELaeV/If you are using jQuery, you can grab the
img
height like sohttp://jsfiddle.net/jasongennaro/ELaeV/
感谢您的指点。我最终做的是在 img 标签上使用内联 css:
在我做类似的事情之前
img height="100" ....
总体问题是我从 php 脚本编写 img 标签,因此在编写标签时我知道我想要的宽度或高度组件,但由于有多个 img 标签并且可能不同大小要求信息无法提前写入CSS。
内联 css 样式优先,而 img height html 方法则不然。
Thanks for the pointers. What I ended up doing was using in-line css on the img tag with this:
before I was doing something like
img height="100" ....
The overall problem was that I write the img tag from a php script so when writing the tag I know the width or height components I want but since there are several img tags and there may be different size requirements the info could not be written to the css ahead of time.
The inline css style taxes precedence where the img height html method does not.