How to override max-width for specific div?

前端 未结 5 2077
后悔当初
后悔当初 2020-12-18 21:58

I am searching for the following solution. In my CSS I have the following:

img, a img { 
    max-width: 100%;
    vertical-align: middle; 
}
<
相关标签:
5条回答
  • 2020-12-18 22:35
    max-width: auto !important;
    

    auto in max-width is not a valid property. You should replace it with

    max-width: none !important;
    
    0 讨论(0)
  • 2020-12-18 22:40

    Are you just looking to unset the max-width property? If so, try:

    #pixel-perfect img {
        max-width: none;
    }
    
    0 讨论(0)
  • 2020-12-18 22:42

    I think it's because a img is more specific than #pixel-perfect img. Element selector is more specific than id selector (So in this example you have 2 elements vs 1element and 1 id).

    In order to fix it, you'd have to add an element to your declaration, for example:

    a#pixel-perfect img {
        max-width: auto !important;
    }
    
    0 讨论(0)
  • 2020-12-18 22:49

    Ensure that your #pixel-perfect img CSS declaration is BELOW the img, a img declaration, otherwise it will get overwritten.

    #pixel-perfect img {
        max-width: none;
        width: auto;
        position: absolute;
        margin: -200px 0 0 -140px;
        z-index: -9999;
    }
    
    0 讨论(0)
  • 2020-12-18 22:54

    Use inherit, none, or initial to reset it.

    jsFiddle example - it works.

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

    According to MDN, none is the default value of max-width.

    Also, since you are overwriting properties, make sure the desired property precedes the initial value in which you want overwritten. Cascade stylesheets are read from top to bottom, thus, if the max-width:100% appears below max-width:inherit in the stylesheet, it will overwrite it rendering max-width:inherit ineffective.

    Aside from that, specificity may be an issue. However, it isn't very likely as your initial declaration isn't very specific.

    0 讨论(0)
提交回复
热议问题