Tuesday, April 22, 2025
HomeCSSFirst Have a look at The Fashionable attr()

First Have a look at The Fashionable attr()


Intro

You might need used the attr() perform in CSS earlier than to get the textual content content material for a pseudo-element.

<p data-title="This textual content is earlier than the title">Hiya World</p>
.title:earlier than {
  content material: attr(data-title);
}

The result’s as follows:

There’s nothing new or groundbreaking right here, it’s helpful for some circumstances.

Nevertheless, once we need to use a price apart from textual content, it won’t work. Within the following HTML, I added data-color attribute to the p ingredient.

<p data-color="purple">CSS is superior</p>

After which used the attribute in CSS.

.title:earlier than {
  coloration: attr(data-color);
}

It’s not working as a result of the attr() perform solely helps textual content values.

Fashionable attr()

Now, we are able to use attr() and outline the kind of worth we need to use.

The attr syntax

For instance, on this case, we have to get the colour worth.

<p data-color="purple">CSS is superior</p>
.title:earlier than {
  coloration: attr(data-color sort(<coloration>));
}

In line with MDN, the brand new information sort can take completely different worth sorts:

The sort() perform takes a <syntax> as its argument that specifies what information sort to parse the worth into. The <syntax> might be <angle>, <coloration>, <custom-ident>, <picture>, <integer>, <size>, <length-percentage>, <quantity>, <share>, <decision>, <string>, <time>, <transform-function>, or a mixture thereof.

That offers us a whole lot of new flexibility that we didn’t have earlier than. The place that is perhaps helpful? That’s what I’ll discover on this article.

Characteristic detection

We are able to test for the fashionable attr() help with CSS @helps. Right here is an instance:

@helps (x: attr(x sort(*))) {
  
}

In case you are utilizing Sass, the above received’t be parsed. I escaped it to make it work in .scss recordsdata:

@helps (#{"x: attr(x sort(*))"}) {
  
}

Use circumstances for the fashionable attr()

Assign column numbers

When utilizing a CSS grid, we are able to assign column numbers to every baby of the grid.

On this instance, I outlined data-col-start and data-col-end on a grid merchandise to set the beginning and finish of the column.

In CSS, I can retrieve the numbers like so:

.format {
  > * {
    grid-column-start: attr(data-col-start sort(<quantity>), 2);
    grid-column-end: attr(data-col-end sort(<quantity>), 3);
  }
}

If the attribute isn’t set, it is going to fall again to 2 and 3 as I specified. Play with the next demo and see the way it works:

<div data-col-start="1" data-col-end="-1">

<img src="desk.jpg" alt="" />

</div>

Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quam deleniti officia aut nulla vero cumqu.

Veritatis, tempore itaque accusamus alias voluptate illum cupiditate eius incidunt et laudantium?

If the attributes aren’t set, the grid-column-start and grid-column-end will default to the fallback values.

See the next demo:

<div data-col-start="1" data-col-end="-1">

<img src="desk.jpg" alt="" />

</div>

Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quam deleniti officia aut nulla vero cumqu.

Veritatis, tempore itaque accusamus alias voluptate illum cupiditate eius incidunt et laudantium?

Entry textarea rows

We are able to use the rows attribute and multiply it by a threshold quantity.

textarea {
  min-height: calc(attr(rows sort(<quantity>)) * 50px);
}

Try the demo under:

<textarea rows="3"></textarea>

Write a short description about your product concept:

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments