I can finish that, but your requested title is cut off: “Use data-sd-animate=”. I’ll assume you want an article titled “Use data-sd-animate=”“> Safely in HTML” demonstrating how to use that attribute for animation and accessibility. I’ll proceed with that—if you meant something else, tell me.
Use Safely in HTML
Summary
This article shows how to use a custom data attribute like data-sd-animate on a to trigger CSS and JavaScript animations safely, accessibly, and without breaking HTML semantics or security.
Why use data attributes?
- Separation of concerns: Keep behavior hooks out of classes or inline styles.
- Semantic HTML: Preserve meaning while adding metadata for scripts.
- Safety: Browsers ignore unknown data- attributes, avoiding validation errors.
Basic HTML pattern
html
<p>Click to reveal: <span id=“label” data-sd-animate=“fade-in” tabindex=“0”>more info</span></p>
- Use tabindex=“0” if the span must be keyboard-focusable.
- Prefer roles (e.g., role=“button”) where the span acts like a control.
CSS for animations
css
[data-sd-animate=“fade-in”] { opacity: 0; transform: translateY(6px); transition: opacity 300ms ease, transform 300ms ease;}[data-sd-animate=“fade-in”].is-visible { opacity: 1; transform: translateY(0);}
- Keep transitions short and respect reduced-motion preferences (see Accessibility).
JavaScript to trigger animation
js
document.addEventListener(‘click’, e => { const el = e.target.closest(’[data-sd-animate=“fade-in”]’); if (!el) return; el.classList.toggle(‘is-visible’);});
- Use event delegation and feature-detection.
- Avoid injecting HTML from untrusted sources into elements with data attributes
Accessibility considerations
- Honor prefers-reduced-motion:
css
@media (prefers-reduced-motion: reduce) { [data-sd-animate=“fade-in”] { transition: none; transform: none; }} - If the span is interactive, expose keyboard behavior and ARIA:
js
el.addEventListener(‘keydown’, ev => { if (ev.key === ‘Enter’ || ev.key === ’ ‘) el.click();}); - Provide visible focus styles.
Security notes
- Data attributes are inert; they don’t run code by themselves.
- Never put untrusted HTML into attributes and then inject it as markup or eval script.
- Sanitize any user-provided values used to control behavior.
Examples
- Trigger CSS animation when elements enter viewport using IntersectionObserver.
- Use data-sd-animate=“slide-left” and map attribute values to CSS classes for consistent patterns.
Testing & best practices
- Test with screen readers and keyboard-only navigation.
- Provide fallback for browsers without JS: default to visible content or CSS-only animations.
- Keep animations short, optional, and meaningful.
If you’d like, I can:
- Produce code examples for multiple animation types (slide, fade*
Leave a Reply