How to make background image shrink proportionally to fit button size in javascript?

后端 未结 4 1054
庸人自扰
庸人自扰 2021-01-07 18:50

I am making a chessboard in javascript. The chessboard\'s squares (buttons) were originally designed to be 60px by 60px, but now they are 40px by 40px.

             


        
4条回答
  •  猫巷女王i
    2021-01-07 19:38

    Most modern browsers have support for CSS background-image options like background-size, so you may really use something like this:

    button{
      width:40px; 
      height:40px; 
      border: 0;
      background-size: 100%; /* To fill the dimensions of container (button), or */
      background-size: 40px auto; /* to specify dimensions explicitly */
    }
    

    Of course you can use that in JavaScript too (but it's better to add it to already existing CSS for button):

    square.style.backgroundSize = '100%';
    

提交回复
热议问题