Nearby lessons
3 of 24π HTML Attributes
π What are HTML Attributes?
HTML attributes give extra information about elements. They are added inside the start tag, usually as name/value pairs like name="value".
Attributes are like extra settings for HTML elements. They help define how an element behaves or appears. They are always written in the opening tag, like this: <p style="color:red">.
π Common HTML Attributes
| Attribute | Description |
|---|---|
href | π Sets the URL for a link |
src | πΌοΈ Sets the image file path |
width & height | π Set image or element size |
alt | π Text shown if image can't load |
style | π¨ Adds inline CSS styling |
lang | π Sets the page language |
title | π‘ Tooltip shown on hover |
class | π·οΈ Assigns a class name |
id | π Gives a unique ID to the element |
data-* | π¦ Stores custom data in an element |
β Attribute Best Practices
- βοΈ Always use quotes:
name="value"(not justname=value) - βοΈ Use
altfor images to support accessibility - βοΈ Use
titleto give helpful tooltips on hover - βοΈ Prefer
classwith external CSS instead ofstyle - βοΈ Use
rel="noopener noreferrer"withtarget="_blank"for security - βοΈ Add
loading="lazy"to images to improve page speed - βοΈ Always set the
langattribute in the<html>tag (likelang="en")
π» HTML Attributes
Code Example
PREVIEW READY
π§ Test Your Knowledge
5 QuestionsProgress: 0 / 5
Keep Going!HTML Paragraphs