What is the purpose of using attributes in HTML elements?
Attributes provide additional information about an element, such as its behavior, appearance, or identity. They help browsers understand how to display or interact with the element.
Click to reveal answer
beginner
Why should attribute values be enclosed in quotes?
Enclosing attribute values in quotes ensures the browser correctly reads the value, especially if it contains spaces or special characters. It also improves code readability and consistency.
Click to reveal answer
intermediate
What is the best practice for boolean attributes like 'disabled' or 'checked'?
Boolean attributes should be written without a value or with the attribute name as the value (e.g., <input disabled> or <input disabled="disabled">). This clearly indicates the attribute is active.
Click to reveal answer
intermediate
How can you improve accessibility using attributes?
Use attributes like aria-label, alt for images, and title to provide descriptive information. These help screen readers and assistive technologies understand content better.
Click to reveal answer
beginner
Why avoid using inline styles as attributes like 'style' for layout?
Using inline styles mixes content with design, making code harder to maintain and less flexible. It's better to use CSS files or <style> blocks for styling.
Click to reveal answer
Which of the following is the correct way to write an attribute with a value?
AAll of the above
B<input type=text>
C<input type='text'>
D<input type="text">
✗ Incorrect
All these are valid ways to write attribute values in HTML, but using quotes (double or single) is recommended for clarity.
What does the 'alt' attribute in an <img> tag do?
AProvides alternative text for screen readers
BLinks the image to another page
CAdds a border around the image
DSpecifies the image size
✗ Incorrect
The 'alt' attribute gives descriptive text for images, helping users who cannot see the image.
How should you write a boolean attribute like 'checked' in a checkbox input?
A<input checked="true">
B<input checked>
C<input checked="checked">
DBoth B and D
✗ Incorrect
Both and are correct ways to indicate a boolean attribute.
Which attribute helps improve accessibility by labeling elements for screen readers?
Aid
Bclass
Caria-label
Dstyle
✗ Incorrect
The 'aria-label' attribute provides accessible names for elements, improving screen reader support.
Why is it better to avoid using the 'style' attribute for layout?
AIt makes the page load slower
BIt mixes content with design, making maintenance harder
CIt is not supported by browsers
DIt disables accessibility features
✗ Incorrect
Using 'style' inline mixes content and design, which is harder to maintain than using separate CSS.
Explain why enclosing attribute values in quotes is a good practice in HTML.
Think about how browsers read attribute values.
You got /3 concepts.
Describe how attributes can help make a webpage more accessible.
Consider users who rely on screen readers.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the best practice for writing HTML attributes?
easy
A. Always use quotes around attribute values, even if not required.
B. Omit quotes to make the code shorter.
C. Use uppercase letters for attribute names.
D. Write attribute values without spaces only.
Solution
Step 1: Understand attribute syntax rules
HTML allows attribute values without quotes only if they contain no spaces or special characters, but quotes improve clarity and prevent errors.
Step 2: Recognize best practice for readability and compatibility
Using quotes consistently around attribute values ensures the code works well across browsers and is easier to read.
Final Answer:
Always use quotes around attribute values, even if not required. -> Option A
Quick Check:
Quotes improve clarity and prevent errors [OK]
Hint: Always put quotes around attribute values for safety [OK]
Common Mistakes:
Skipping quotes causes parsing errors
Using uppercase attribute names (should be lowercase)
Assuming quotes are optional always
2. Which of the following is the correct syntax for adding an alt attribute to an image?
easy
A.
B.
C.
D.
Solution
Step 1: Check attribute value syntax
The alt attribute must have a value describing the image, enclosed in quotes for best practice.
Step 2: Identify the correct option
<img src='photo.jpg' alt='Beautiful sunset'> uses quotes around the descriptive text, making it valid and accessible.
Final Answer:
<img src='photo.jpg' alt='Beautiful sunset'> -> Option B
Quick Check:
Alt text must be quoted and descriptive [OK]
Hint: Alt text always needs quotes and meaningful description [OK]
Common Mistakes:
Leaving alt attribute empty or missing value
Not using quotes around attribute values
Using alt without value or with empty value
3. What will be the rendered result of this HTML snippet?
<button disabled>Click me</button>
medium
A. A clickable button labeled 'Click me'.
B. A button labeled 'disabled' instead of 'Click me'.
C. An error message in the browser.
D. A button labeled 'Click me' that is grayed out and cannot be clicked.
Solution
Step 1: Understand the disabled attribute effect
The disabled attribute on a button makes it inactive and visually grayed out, preventing user interaction.
Step 2: Predict the visual and functional output
The button text remains 'Click me', but the button cannot be clicked and appears disabled.
Final Answer:
A button labeled 'Click me' that is grayed out and cannot be clicked. -> Option D