The Wayback Machine - https://web.archive.org/web/20240727172428/https://www.geeksforgeeks.org/css-attr-function/
Open In App

CSS attr() Function

Last Updated : 04 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The attr() function is an inbuilt function in CSS that returns the value of an attribute of the selected elements.

Syntax: 

attr( attr_name )

Parameter: This function accepts a single parameter attr_name which is used to hold the name of the attribute in an HTML element. It is a mandatory parameter.

Return Value: This function returns the value of the attribute of selected elements.

Example: The below example illustrates the attr() function in CSS:

html




<!DOCTYPE html>
<html>
 
<head>
    <title>attr function</title>
    <style>
        a:before {
            content: attr(href) " =>";
        }
         
        a {
            text-decoration: none;
        }
         
        .gfg {
            font-size: 40px;
            color: green;
            font-weight: bold;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <div class="gfg">GeeksforGeeks</div>
    <h1>The attr() Function</h1>
    <a href=
        GeeksforGeeks
    </a>
</body>
 
</html>


Output: 

Supported Browser:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari  


Similar Reads

D3.js selection.attr() Function
The selection.attr() function is used to set the attribute of the element that is selected. The name of the attribute and value of the attributes are to be set using this function. Syntax: selection.attr(name[, value]); Parameters: This function takes two parameters as shown above and described below: name: It is the name of the attribute.value: It
1 min read
jQuery attr() Method
The attr() method in jQuery is used to set or return the attributes and values of the selected elements. Syntax: To return the value of an attribute:$(selector).attr(attribute)To set the attribute and value:$(selector).attr(attribute, value)To set attribute and value using a function:$(selector).attr(attribute, function(index, currentvalue))To set
3 min read
Difference between prop() and attr() Methods in jQuery
In this article, we will learn about the differences between the prop() and the attr() in JQuery. jQuery is the fastest and lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. JQuery is widely famous for its motto of “Write less, do
2 min read
jQWidgets jqxDraw attr() Method
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxDraw is a small jQuery-based plugin that is used to draw shapes and texts using VML, SVG and HTML5 The attr() method is used for setting attributes of an elem
2 min read
jQuery Data vs Attr
jQuery is a powerful JavaScript library that simplifies web development tasks, making it easier for developers to manipulate HTML elements and create dynamic web pages. One of the most useful features of jQuery is its ability to handle data and attributes of HTML elements. In this article, we will compare and contrast jQuery Data vs Attr and see ho
4 min read
Difference between bootstrap.css and bootstrap-theme.css
Bootstrap is an HTML, CSS and JS front-end-framework used for developing responsive and mobile-first projects on the web. It is the framework that comes up with predefined terms, thus giving you the ability to use these codes instead of having to create the codes from scratch. It is a collection of HTML, CSS and JS code that is designed to help bui
5 min read
Difference between CSS Grid and CSS Flexbox
Grid:CSS Grid Layout, is a two-dimensional grid-based layout system with rows and columns, making it easier to design web pages without having to use floats and positioning. Like tables, grid layout allow us to align elements into columns and rows. To get started you have to define a container element as a grid with display: grid, set the column an
4 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
CSS Cheat Sheet - A Basic Guide to CSS
What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is a CSS Cheat Sheet? CSS Cheat Sheet provides you wi
13 min read
How to identify unused CSS definitions from multiple CSS files in a project ?
In this article, we will learn how to identify unused CSS definitions from multiple CSS files in a project. What are unused CSS definitions? Unused CSS definitions are the stylesheets that are completely useless for rendering and loading the page. If you remove these unused CSS definitions then nothing will happen to your webpage. Why you should re
2 min read
Practice Tags :
three90RightbarBannerImg