In one of my codes, using css to establish width and height for an HTML5 canvas isn't working. Can someone tell me why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In one of my codes, using css to establish width and height for an HTML5 canvas isn't working. Can someone tell me why?

I was trying to use css to scale my canvas to fit on multiple devises, but height won't work(width does). If it works on your devices, I'll know my tablet is glitching, but If it is an error in my code, please tell me. I've looked back at game development with JS multiple times to try to figure this out. https://code.sololearn.com/W3peEabGy8xC/?ref=app

8th Nov 2023, 12:47 PM
Paxton Programer
6 Answers
+ 5
HiPaxton Programer Placing a canvas within a button may cause issues with rendering, styling, event handling, and accessibility. But to continue to with your example. The properties don't work as intended because the canvas is confined within the button. To resolve this, edit the containing button. Refer to this example. https://code.sololearn.com/WcNYuq2EbiR4/?ref=app
9th Nov 2023, 4:45 PM
Chris Coder
Chris Coder - avatar
+ 6
If you set the width and height in CSS, then remove the same attributes from the HTML canvas tag. Use only one place to define style, so you get a consistent result regardless of browser and device. Decide if you want to set it in pixels or percentage, use only one of them. Styling elements in relation to the screen edge or to other elements, can get very tricky. Often another container such as div is used around the canvas to achieve some positioning effect. Mastering this is an art... takes a lot of research and practice, to see what are the commonly used patterns and best practices.
8th Nov 2023, 2:56 PM
Tibor Santa
Tibor Santa - avatar
+ 6
You don't need to set the height for canvas in css. You already set it to be a square in html. why the canvas inside a button? why h1 & h2 inside head! canvas{ width:100%; background-color:blue; } the button should not exceed 100% width .jump{ padding:2% 0; width:100%; }
8th Nov 2023, 3:05 PM
Mafdi
Mafdi - avatar
+ 1
Thanks for the help
9th Nov 2023, 6:02 PM
Paxton Programer
0
The reason the canvas is inside a button is so you can tap on it to attack. Instead of having two buttons for attack and jump, U tap the screen for attack and tap jump for jump.
8th Nov 2023, 5:13 PM
Paxton Programer
0
Thanks everyone
8th Nov 2023, 5:15 PM
Paxton Programer