DEV Community

Dima Prohorenko
Dima Prohorenko

Posted on

CSS tricks you probably didn't know about.

Today I'm gonna share with you some cool css selectors.

Let's start with counter.

To style numbers in a numbered list, we need to play with properties called CSS counters. CSS counters let you adjust the appearance of content based on its location in a document.

<ul class="list">
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
</ul>
Enter fullscreen mode Exit fullscreen mode
li {
    font-size: 40px;
    margin-bottom: 20px;
    counter-increment: li;
}

.list li::before {
    content: counter(li);
    margin-right: 10px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: inline-block;
    background-color: #f3b70f;
    color: white;
    text-align: center;
    line-height: 50px;
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

::selection.

The ::selection pseudo-element applies to highlighted elements on the DOM. This is one of my favorite pseudo-elements. The syntax looks like this:
Alt Text

p::selection {
  color: white;
  background-color: green;
}
Enter fullscreen mode Exit fullscreen mode

Next is the attr() function.

It's used to retrieve the value of an attribute of the selected element and use it in the stylesheet.

<h2 class="title" data-count="01">Section</h2>
<h2 class="title" data-count="02">Section</h2>
<h2 class="title" data-count="03">Section</h2>
Enter fullscreen mode Exit fullscreen mode
.title {
    font-size: 35px;
    letter-spacing: 3px;
}

.title::before {
    content: attr(data-count);
    font-size: 1.2em;
    color: steelblue;
    margin-right: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Adding Stroke to Web Text

<h2>Css is Awesome</h2>
Enter fullscreen mode Exit fullscreen mode
h2 {
    -webkit-text-fill-color: transparent;
    -webkit-text-stroke: 3px tomato;
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (18)

Collapse
ย 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ โ€ข โ€ข Edited

I knew about them so I'm going to up the stakes.

How about this:

background: background;

It's a fun trivia one that many people do not know about.

border-radius: 9e9rem;

Now that's what I call round

๐Ÿ˜‚

Collapse
ย 
sasscrafter profile image
Dima Prohorenko โ€ข

๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚ now I do;)

Collapse
ย 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ โ€ข

I have plenty more of this stuff in my posts, the JS labeled loops one is a popular read

Thread Thread
ย 
sasscrafter profile image
Dima Prohorenko โ€ข

Iโ€™ll definitely check it out

Collapse
ย 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ โ€ข

Mad as this sounds. It takes the colour of your systems desktop background colour, the colour you would see without a background image.
So in other words it's different depending on which OS and platform you use.

Collapse
ย 
btlm profile image
btlm โ€ข

Wow, attr() is new for me. Thank you for your post! Very useful

Collapse
ย 
sasscrafter profile image
Dima Prohorenko โ€ข

No problem bro๐Ÿ‘Œ

Collapse
ย 
junliangong01 profile image
JunliangOng01 โ€ข

Custom Software Development Services for business needs from software development company groupbwt.com. We deliver software products the way you want them to be.

Collapse
ย 
ironcladdev profile image
IroncladDev โ€ข

The last one was the one I've been looking for! before I saw this, I used four "text-shadow"s to stroke the text.
Thank you so much for showing this to DEV!

Collapse
ย 
sasscrafter profile image
Dima Prohorenko โ€ข

Glad to help

ย 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ โ€ข

Not quite the colour comes from the colour behind the image, it's an old fashioned thing back in the days of xp

Collapse
ย 
mikechrupcala profile image
Michael Chrupcala โ€ข
Collapse
ย 
sasscrafter profile image
Dima Prohorenko โ€ข

Glad to help

Collapse
ย 
imcodebreaker profile image
im_codebreaker โ€ข

Thank you for your sharing๐Ÿ‘Œ

Some comments may only be visible to logged-in visitors. Sign in to view all comments.