How would you create a decorative underline using a pseudo-element?

Prepare for the CSS Mastery Test through study materials and mock questions. Enhance your CSS skills and get ready to ace your exam with detailed explanations and hints. Elevate your mastery in CSS design!

Multiple Choice

How would you create a decorative underline using a pseudo-element?

Explanation:
The concept being tested is creating a decorative underline with a pseudo-element that doesn’t affect the text layout. You attach a pseudo-element to the element, give it empty content, and absolutely position it at the bottom so it acts like a line drawn inside the element’s box. By setting the parent to position: relative, the pseudo-element’s absolute positioning uses the element as its containing block, keeping the line in the right place regardless of size. The line’s thickness comes from height (2px), its width from width: 100% so it spans the full element, and its color from background: currentColor so it inherits the text color automatically. Placing the decorative line with ::after is a reliable choice because it sits after the element’s content in the DOM, making layering predictable and avoiding interference with the text itself. In contrast, text-decoration would apply the underline semantically to the text and offers less control, while border-bottom would affect the element’s box and layout, making it harder to customize independently.

The concept being tested is creating a decorative underline with a pseudo-element that doesn’t affect the text layout. You attach a pseudo-element to the element, give it empty content, and absolutely position it at the bottom so it acts like a line drawn inside the element’s box. By setting the parent to position: relative, the pseudo-element’s absolute positioning uses the element as its containing block, keeping the line in the right place regardless of size. The line’s thickness comes from height (2px), its width from width: 100% so it spans the full element, and its color from background: currentColor so it inherits the text color automatically. Placing the decorative line with ::after is a reliable choice because it sits after the element’s content in the DOM, making layering predictable and avoiding interference with the text itself. In contrast, text-decoration would apply the underline semantically to the text and offers less control, while border-bottom would affect the element’s box and layout, making it harder to customize independently.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy