-2

For example I want this:

<link rel="stylesheet" href="stylus.css">

only for this code:

<a href="yeet">Buy!</a>

6 Answers6

3

If you use it for only one, then use id with #, for multiple elements you will need to use class with .

#only-this {
  color: red;
}
<a id="only-this" href="yeet">Buy!</a>
<a href="yeet">Sell!</a>
xxx
  • 3,315
  • 5
  • 21
  • 40
0

You can just have a class in your CSS and then you apply it to your object:

.my-stylus-class {
  color: blue;
}
<a class="my-stylus-class" href="yeet">Buy!</a>

I wanted to add that a class or ID is not neccesseraly required, you can of course also just define the element itself in CSS, so for example this would be also possible:

a {
  color: blue;
}
<a href="yeet">Buy!</a>
Daniel Resch
  • 584
  • 3
  • 12
nck
  • 1,673
  • 16
  • 40
  • 2
    A class isn't specific to *one* element! – Johannes Apr 15 '21 at 22:32
  • 1
    @johanness If you just use it in one element it ist. – nck Apr 15 '21 at 22:33
  • ID would give a better specificity weight aswell as makign sure that it is not accidentally sued more then once. Also it coming back to specificity weight, an ID would make sure that another stylign would not interfer for that single element. As such an ID should have been used. – tacoshy Apr 15 '21 at 22:35
  • @tacoshy i agree with you in what you say, but it also makes your code less resusable. If you then want to apply the same styling in the future to another element, which I think is more likely to happen that making a mistake applying the classes which anyway should be easy to catch by just exploratory testing. – nck Apr 15 '21 at 22:40
  • yes but the question is not about further elements it is specifically for only styling one element. – tacoshy Apr 15 '21 at 22:41
  • 1
    you can of course also add a simple element referrer like a in your css I will add this to my answer – Daniel Resch Apr 15 '21 at 22:49
  • @tacoshy in that case always use ID, I also mentioned this in my original answer - I said there use either class or ID, idk why but some admins changed that - pretty annoying stack! – Daniel Resch Apr 17 '21 at 02:22
0

You need to make an identification on your html and then select it on the CSS page , like :

HTML :

<a id="buyButton">Buy!</a>

Then on CSS:

#buyButton
{
  color : blue;
}

izzypt
  • 170
  • 2
  • 16
-1

you can't tell the browser to use a specific stylesheet (file) only for a certain element.

you have to give the element a specific ID, or class, and use that in your css for example:

.yeet {
  /* CSS Properties go here */
}
<a href="yeet" class="yeet">Buy!</a>
Daniel Resch
  • 584
  • 3
  • 12
-2

Inline css.

<a href="yeet" style="css here">Buy!</a>
Cole Henrich
  • 155
  • 3
  • 17
  • The question was specifically `how to add CSS ...` not how to apply styling for one element. inline-style is not css. – tacoshy Apr 15 '21 at 22:33
  • No, the question was **"for one element"** – Cole Henrich Apr 15 '21 at 22:33
  • read the title... CSS is specififcally mentioned. Also the sample indiciates CSS. Frankly CSS is not another name for inline-style so using `style="css here"` makes no sense. 2 completely different thigns with different specificity weight. – tacoshy Apr 15 '21 at 22:36
-2

This is what the question was: one element. A class can be more than one element. Here, using inline CSS which is not any worse than stylesheet CSS, you can make your yeets differently styled.

<a href="yeet" style="color:red;">Buy!</a>
<br>
<a href="yeet" style="color:orange;">Buy!</a>
<br>
<a href="yeet" style="color:green;">Buy!</a>
<br>
<a href="yeet" style="color:blue;">Buy!</a>
<br>
<a href="yeet" style="color:indigo;">Buy!</a>
<br>
<a href="yeet" style="color:violet;">Buy!</a>
<br>
<a href="yeet" style="color:chartreuse;">Buy!</a>
Manas Khandelwal
  • 3,790
  • 2
  • 11
  • 24
Cole Henrich
  • 155
  • 3
  • 17
  • 1
    Why are promoting inline styles when OP mentioned linking the stylesheet! . . . Don't promote bad practices to beginners! – Manas Khandelwal Apr 15 '21 at 22:41
  • and frankly, why give it as a 2nd anwser while calling inline-style -> inlien css? CSS (cascade style sheet) is always an external file. CSS is not a short term for style (espacially not ehad style or inline style... – tacoshy Apr 15 '21 at 22:55