Place background image 1em from the right?

后端 未结 4 1013
抹茶落季
抹茶落季 2021-02-12 21:58

As far as I can tell, it is not possible to place a CSS background image 1em from the right border of any block, neither is it possible to place a image 1em from the bottom.

4条回答
  •  猫巷女王i
    2021-02-12 23:01

    After some research the actual x pixel length of the background position is always counted from the left side of the element. The only way to make this work (without using other elements) would be to use javascript, calculate the left length given the elements width:

    var rightMargin = "10"; // in pixels
    var imageWidth = "16";
    var left = element.style.clientWidth - imageWidth - rightMargin;
    
    element.style.backgroundPosition = "0px " + left + "px";
    

提交回复
热议问题