Table of contents | |
Introduction | |
What are HTML Attributes? | |
Commonly Used Attributes | |
Applying Attributes in HTML | |
Sample Problems and Solutions |
HTML (Hypertext Markup Language) is the standard markup language for creating web pages. It provides a structure and layout for the content on the web. In HTML, attributes are used to modify the behavior or appearance of elements. In this article, we will explore various attributes that can be used in HTML to enhance the functionality and presentation of web pages.
HTML attributes provide additional information about elements. They are specified within the opening tag of an element and are defined using the syntax: 'attribute_name="value" '. Attributes can be used to control the appearance, behavior, and interaction of elements on a web page.
Let's explore some commonly used attributes in HTML with examples and explanations.
The 'id' attribute is used to uniquely identify an element on a web page. It can be used to style or manipulate specific elements using CSS or JavaScript.
Example:
<p id="my-paragraph">This is a paragraph.</p>
The 'class' attribute is used to define a class for an element. It allows multiple elements to be styled or targeted together using CSS or JavaScript.
Example:
<p class="highlight">This is a highlighted paragraph.</p>
<p class="highlight">This is another highlighted paragraph.</p>
The 'src' attribute is used to specify the source URL of an external resource, such as an image or a video.
Example:
<img src="https://edurev.gumlet.io/mage.jpg" alt="A beautiful image">
The 'href' attribute is used to define the URL of a hyperlink. It specifies the destination of a link when it is clicked.
Example:
<a href="https://www.example.com">Visit Example</a>
The 'alt' attribute is used to provide alternative text for an image. It is displayed when the image cannot be loaded or accessed by assistive technologies.
Example:
<img src="https://edurev.gumlet.io/mage.jpg" alt="A beautiful image">
The 'style' attribute is used to define inline CSS styles for an element. It allows you to customize the appearance of elements directly within the HTML.
Example:
<p style="color: red; font-size: 16px;">This is a styled paragraph.</p>
To apply attributes, you simply need to add them within the opening tag of an element. Here's an example of how multiple attributes can be applied to an element:
Example:
<a href="https://www.example.com" id="my-link" class="highlight">Visit Example</a>
In the above example, the '<a>' element has an 'href' attribute for the link destination, an 'id' attribute for identification, and a 'class' attribute for styling.
Problem 1: Add an image to your web page with the source URL "image.jpg" and an alternate text "A beautiful image".
<img src="https://edurev.gumlet.io/mage.jpg" alt="A beautiful image">
Problem 2: Create a paragraph with the text "This is a styled paragraph." Apply a red color and a font size of 16 pixels.
<p style="color: red; font-size: 16px;">This is a styled paragraph.</p>
HTML attributes play a crucial role in enhancing the functionality and presentation of web pages. By utilizing attributes effectively, you can control the behavior, appearance, and interaction of elements within your web page. In this article, we explored some commonly used attributes along with examples and explanations. As you continue to learn HTML, make sure to experiment with different attributes to create dynamic and visually appealing web pages.
20 videos|15 docs|2 tests
|
|
Explore Courses for Software Development exam
|