What is the purpose of the `class` attribute in HTML?
A
To define the ID of an element
B
To assign a JavaScript function
C
To apply styles or identify elements
Analysis & Theory
The `class` attribute is used to apply CSS styles or identify elements for JavaScript.
Which symbol is used in CSS to select a class?
Analysis & Theory
The dot `.` is used in CSS to select elements by class, like `.myClass`.
Can multiple HTML elements share the same class name?
A
No, class names must be unique
B
Yes, classes can be shared
Analysis & Theory
Yes, the same class can be used on multiple elements to apply shared styles or behaviors.
Which of the following is a correct way to use a class in HTML?
A
<p class='highlight'>Text</p>
B
<p class=highlight()>Text</p>
D
<p id='highlight'>Text</p>
Analysis & Theory
`<p class='highlight'>` correctly applies the class named `highlight`.
How do you apply multiple classes to a single HTML element?
Analysis & Theory
Separate multiple class names with a space: `class='one two'`.
Which HTML elements can have a `class` attribute?
A
Only `<div>` and `<span>`
Analysis & Theory
All HTML elements can use the `class` attribute.
What will `.box { border: 1px solid black; }` do?
A
Apply a border to elements with ID `box`
B
Apply a border to elements with class `box`
C
Apply padding to all elements
D
Change the background color
Analysis & Theory
The CSS selector `.box` targets elements with the class `box` and adds a black border.
Can class names contain numbers and hyphens?
A
No, only letters are allowed
C
Yes, both numbers and hyphens are allowed
Analysis & Theory
Yes, class names can include letters, numbers, hyphens, and underscores.
How can JavaScript select elements by class name?
A
getElementById('class')
B
getElementsByClassName('myClass')
Analysis & Theory
JavaScript uses `document.getElementsByClassName('myClass')` to select elements by class.
Why use classes instead of IDs when styling multiple elements?
C
Classes can be reused across elements
D
IDs are not allowed in CSS
Analysis & Theory
Classes are reusable and ideal for styling multiple elements, unlike unique IDs.