Friday, May 17, 2024
20
rated 0 times [  22] [ 2]  / answers: 1 / hits: 80121  / 12 Years ago, wed, february 29, 2012, 12:00:00

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.



    button 
{
width:40px;
height:40px;
border: 0
}


However, some of the pieces that were designed for this earlier chessboard are still 60px by 60px. Is there any way in Javascript to make the images shrink proportionally to fit the square size? Currently, the images fit the square size, but do not shrink, so when I say,



    square.style.backgroundImage = imgLink; // square is the button, imgLink is WhiteKing.jpg for example.


I get pieces like this -



enter



If the WhiteKing.jpg had shrunk proportionally, it would have fit nicely. Is there a way to do this in Javascript? Any help would be really appreciated.


More From » background-image

 Answers
26

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%';

[#87133] Tuesday, February 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;