CSS: Width and Max-Width

后端 未结 9 1522
無奈伤痛
無奈伤痛 2020-12-02 18:01

Why does:

width: 98%;
max-width: 1140px;

do the same as

width: 1140px;
max-width: 98%;

The first one make

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 18:16

    From my understanding of the properties:

    if width > max-width use max-width
    if max-width > width use width
    

    Therefore, using your example, this must mean that 1140px is strictly less than 98% at the screen resolution you are viewing at.

    Shrink your browser screen and you will get different results.

    It's somewhat unrelated, but I've found max-width (and the corresponding property max-height) to be a problem with images in Internet Explorer, and found this to be helpful in coaxing it to use the correct values:

    img {
        max-width: 150px;
        max-height: 120px;
        width: auto !important;
        height: auto !important;
    }
    

    Without the last two properties, most standard-compliant browsers correctly maintain aspect ratio, but Internet Explorer will not unless you do that.

    Edit: It looks like I've said basically the same answer as everyone else.

提交回复
热议问题