【问题标题】:How to override max-width for specific div?如何覆盖特定 div 的最大宽度?
【发布时间】:2019-07-11 20:45:39
【问题描述】:

我正在寻找以下解决方案。在我的 CSS 中,我有以下内容:

img, a img { 
    max-width: 100%;
    vertical-align: middle; 
}

此代码是我的 WordPress 主题和响应式网页设计所必需的。现在我想将max-width 属性覆盖为auto。当我这样做时,它不会覆盖:

#pixel-perfect img {
    max-width: auto !important;
    position: absolute;
    margin: -200px 0 0 -140px;
    z-index: -9999;
}

我做错了什么?

【问题讨论】:

    标签: css


    【解决方案1】:

    您只是想取消设置max-width 属性吗?如果是这样,请尝试:

    #pixel-perfect img {
        max-width: none;
    }
    

    【讨论】:

      【解决方案2】:

      我认为这是因为a img#pixel-perfect img 更具体。元素选择器比 id 选择器更具体(所以在这个例子中你有 2 个元素 vs 1 个元素和 1 个 id)。

      为了修复它,您必须在声明中添加一个元素,例如:

      a#pixel-perfect img {
          max-width: auto !important;
      }
      

      【讨论】:

        【解决方案3】:

        确保您的#pixel-perfect img CSS 声明低于img, a img 声明,否则它将被覆盖。

        #pixel-perfect img {
            max-width: none;
            width: auto;
            position: absolute;
            margin: -200px 0 0 -140px;
            z-index: -9999;
        }
        

        【讨论】:

          【解决方案4】:
          max-width: auto !important;
          

          最大宽度中的自动不是有效属性。您应该将其替换为

          max-width: none !important;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-03
            • 2017-12-24
            • 1970-01-01
            相关资源
            最近更新 更多