简体   繁体   中英

Setting section width and height in Javascript

Why don't <section> elements accept width and height attributes just as <canvas> elements do?

( JSFiddle )

This is because section is a direct subelement of HTMLElement whereas Canvas is a HTMLCanvasElement which also extends HTMLElement but adds those 2 properties.

A section element is meant to be styled used CSS (similar to divs) whereas canvas is supposed to more of a HTML + Javascript interaction

I'd never recommend using javascript to style the look and feel of a HTML element, that's why CSS got invented for, right?

If you're really pushed for any reason to use it, then take a look at the js alternative below.

Try declaring the properties as you'd do with a normal HTML element (cross browser way to do it):

section.style.width = '250px';
section.style.height = '250px';

Have a look at this working fiddle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM