How can I do width = 100% - 100px in CSS?

前端 未结 18 856
借酒劲吻你
借酒劲吻你 2020-12-12 13:28

In CSS, how can I do something like this:

width: 100% - 100px;

I guess this is fairly simple but it is a bit hard to find examples showing

18条回答
  •  遥遥无期
    2020-12-12 13:45

    CSS can not be used to animation, or any style modification on events.

    The only way is to use a javascript function, which will return the width of a given element, then, subtract 100px to it, and set the new width size.

    Assuming you are using jQuery, you could do something like that:

    oldWidth = $('#yourElem').width();
    $('#yourElem').width(oldWidth-100);
    

    And with native javascript:

    oldWidth = document.getElementById('yourElem').clientWidth;
    document.getElementById('yourElem').style.width = oldWidth-100+'px';
    

    We assume that you have a css style set with 100%;

提交回复
热议问题