âš ī¸ Warning âš ī¸ Deprecated Code! This video tutorial contains outdated code.
💡 If you wish to update it, any AI assistant will update the code for you in seconds.

Custom Data Attributes HTML JavaScript CSS Tutorial

Published : April 30, 2015   •   Last Edited : November 24, 2025   •   Author : Adam Khoury
Learn to use custom data attributes. Custom data attributes allow authors to create custom data to apply to their HTML elements when no other attributes make sense to use to store extra data. They help extend the information that html elements can communicate to a program or script. Custom data attributes can be accessed through both JavaScript and CSS. example.html
<style>
body { background: #F3F3F3; }
div.rockets {
	position: absolute;
	background:url(rocket.jpg) no-repeat;
	width: 125px;
	height: 50px;
	left: 0px;
	transition: left 0s linear 0s;
}
div.rockets[data-altitude="100"] {
    transform: rotate( 10deg );
}
</style>
<script>
var rockets, altitude, speed, distance;
function activateRockets(){
	rockets = document.querySelectorAll('div.rockets');
	for(var i=0; i < rockets.length; i++){
		altitude = rockets.dataset.altitude;
		speed = rockets.dataset.speed;
		distance = rockets.dataset.rocketDistance;
		rockets.style.top = altitude+"px";
		rockets.style.transitionDuration = speed+"s";
		rockets.style.left = distance+"px";
	}
}
window.addEventListener("load", activateRockets);
</script>
<div class="rockets" data-altitude="100" data-speed="3.5" data-rocket-distance="700"></div>
<div class="rockets" data-altitude="200" data-speed="5.0" data-rocket-distance="700"></div>