freeCodeCamp Challenge Guide: Use a CSS Class to Style an Element

Use a CSS Class to Style an Element

Problem Explanation

In CSS, we can target the styling of specific elements that match the specified class attribute.

For example, if you have an element with a class of button, then we can style the look & feel as follows:

  • Start with a . (period) character followed by the class name and add your style
.button {
  border: 2px solid black;
  text-align: center;
  display: inline-block;
  padding: 5px 10px;

Now, the real benefit of using class to style an element is to target multiple elements that have the matching class attribute. For example, if there are 2 buttons on a webpage and they both look similar in style but only differ in size, then we can use a common class to give them common styles and a unique class for each button to give them different size values.

The following HTML code snippet depicts 2 buttons:

  • Sign up button that should have common button style + should be large in size
  • Login button that should have common button style + should be small in size
<div class="button large">Sign up</div>
<div class="button small">Login</div>

Using the above defined .button class as a common style for both buttons, and using .large and .small class attributes to give them different sizes, we can achieve the look we want without duplicating our code.

.large {
  font-size: 20px
.small {
  font-size: 10px

I still cannot tell what I’m doing wrong. My text is still black.

h2 .red-text { color: red; }


Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.


I was having trouble too. The instructions seem a bit choppy. What I ended up doing was copy pasting from the example and then editing in red for blue. Here is what mine looks like:

.red-text { color: red; }


Notice how the top CSS portion has a period before the red-text part but not in the html bit?

Mine is still wrong. I was following the lesson fine until this point. Now, I’m just guessing and putting anything in anywhere. How do we get help? I can’t move forward without getting this right and the hint didn’t help me.


I was also stuck for a bit but I figured it out after about 10min.
Here’s a hint guys, you have to change/add code to lines 3, 4 and 8. Happy coding.

1 Like
  .red-text {
    color: red;
<h2 class="red-text">CatPhotoApp</h2>

Ive been stuck on this problem for a few days now and nothing that I have seen has really helped out. I got everything but where it says “Your stylesheet should declare a red-text class and have its color set to red.” Can someone please help? thanks

Follow what this guy has put down.


Thanks so much, everybody. I compared my code with Untrue Hunter’s code letter by letter and noticed that I’d forgotten to delete a set of brackets from the previous exercise. Lesson learned; I really need to work on my attention to detail.

Thanks again for responding so quickly.


1 Like

what is the difference between Use a CSS Class to Style an Element and Use CSS Selectors to Style Elements??

1 Like

My code broke down because I didn’t recognize the differences between attributes, classes and elements and their definitions. This is important.

CSS attributes typically are applied globally to all headers or sets of headers.

.blue-text { color: blue; }

I assumed the above is a CSS class statement for all headers but I wanted to test it

By the way this code could have been written on one line:

.red-text { color: red; }

Okay, so we defined the class “red-text” is red, but we still have to call it up by its class name in order to see it applied

Let’s do that:


That worked. (No it didn’t work. The HTML for this page wouldn’t allow the < > bracketed entries to display)

Element styles can be specific to one particular header in its unique application and that should depend on exactly where we place the class call but it would not work here for me. I also tried a different color by adding the header

Blue Dog Democrat

thinking it would come back in blue text. Instead it came back in black text.
Then I tried replacing it with

Blue Dog Democrat

guessing it would be in red, and that came back in black text

Finally I tried going back and changing the initial class entry to blue and instead everything came back in black. Conclusion? The problems editor works but it wasn’t programmed to display the color blue. I guess we can’t stray too far when we experiment. And I’m guessing the style in angle brackets or colors displayed didn’t print in my code above because the HTML coding for this page took precedence.

1 Like

OMG, that is so helping. Thank you so much. How can you know that answer?

Okay, so I’ve been jamming along with these lessons this morning, but this problem is giving me a headache.

My code is as follows:
.red-text {
color: red;


Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.


Minus the start and end quotes, that’s what I have on my editor. It will not change the text color to red. Do we have to specify it as text-color, or what? Is it automatically heeding it as background-color? Why isn’t anything happening?

Dang, it looks like even with the start and end quotes it takes away the bracketed items. Oh well.

Your code is correct. You have defined a class of text but you need to state the name its stored under. In this example Style is the name your code will be available under and it will apply only to the elements which call for it, so lets do that.

style .red-text{
now we have to have the h2 headers make the class call red-text

Sorry, I’m only a day ahead of you guys. The next line of code didn’t print. Not sure how to enter the code so that it prints here.
The next line of code before CatPhotoApp is the instruction for h2 headers with class equal to red dash text This is the class name we assigned it and now we have to call for it to get it to work…

My code looks just like this but for some reason it is not working. Any tips or feedback?


I changed someting in code and I wrote like below and it’s working :wink:

‘red-text’ not “red-text”


thanks everyone ,this really helped because I was stuck on this challenge for a while

here’s my code. It’s still not correct???


Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.

the problem is between the style class.
here’s the code. { color:red; }

don’t put a space between h2 and the dot.

1 Like