html - 是否可以通过 CSS 中的比较大小来选择图像(或其他元素)

标签 html css css-selectors

是否可以根据某些尺寸标准选择图像或其他元素,例如,在 X 或 Y 维度上大于某些像素?

如何?

我发现@media 描述符确实允许比较运算符,但这些描述符适用于整个显示区域。

我的目标是为仅适用于超过特定阈值(例如 100 像素)的图像编写样式规则。比方说,这种风格对内容图像有意义,但对页面其他地方的小元素没有意义。样式表被应用到外部页面,我无法控制它们的内容并且通常不能重写或脚本处理它们:

img {
    float: right;
    padding: 10px;
    margin: 20px;
    border: solid 1px #888;
    /* Grey */
    box-shadow: 6px 10px 8px rgba(0, 0, 0, 0.7);
    /* Sepia */
    box-shadow: 6px 10px 8px rgba(128, 128, 96, 0.7);
}

最佳答案

jQuery 函数可以执行您想要的工作:

function check_image_width (image, length, type, className)
{
   if(type='greater') // then compare by the minimum width
   {
      if($(image).width() > length)
        {
          $(image).addClass(className);
        }
    }
    if(type='lesser') // then compare by the minimum width
   {
      if($(image).width() < length)
        {
          $(image).addClass(className);
        }
   }
}

我已经非常快地完成了上述功能。它可以更加通用...

关于html - 是否可以通过 CSS 中的比较大小来选择图像(或其他元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20701202/

相关文章:

html - 如何使用 CSS 仅将 HTML 选项元素的首字母大写?

html - 对 css nth-child() 和 nth-of-type 的说明

asp.net - ajax文本框水印

javascript - 在 URL 栏中按下 ENTER 时如何更正 Firefox 中的#anchor 滚动位置

html - 边框超过两个 rowspans

jquery - 为什么 slider 中的最后一个列表不是淡入/淡出/显示/隐藏?

jquery - 选择单选按钮时,在 div 内选择时放置文本。

css - :first-of-type in table with multiple classes doesn't work

php - 使用不同的 PHP 变量(包括 if 语句)创建表

html - 其父级底部的文本没有上边距并且文本在添加内容时向下流动?