Why do you need to go back and forth between CSS and HTML to enter code for CSS?/Why can’t it be done in just CSS?

Hello, I’m new to coding and am currently using Codecademy to learn the basics. I’ve already learned HTML and am now on my second lesson of CSS.

I am confused as to why I’m being taught to put certain style effects in style.css and separate styling effects in index.html.

Wouldn’t it make more sense to do all the styling in CSS?

Hello @design4985813316!! Welcome to the forums!! :grinning:

If I remember correctly, they were showing things like classes and ids.
If this is the case, for CSS to style something, it need an HTML element to work with, so they have you changing the HTML to specify which element will be styled.

One way to picture it would be CSS as paint. If a person wants their house painted but only tells the worker, “Go paint.” He has no clue what to paint. So giving HTML elements special attribute is like telling the man what to paint.

If this was not what you are reffering to let me know.

It could also have been that they were talking about inline styling, which is how you can keep your HTML and CSS in a single file. If so they were showing you multiple ways of styling, both have their own pros and cons.

Hi there, welcome to the forums!

Without a link to the specific exercise that you’re working on, it’s difficult to be sure, but…

If I remember correctly, the CSS lessons start off with some exercises where you style individual elements by doing something like this:

<p>This is some text and <span style="text-transform: uppercase; font-size: 36px;">this is some text I'd like larger and uppercase</span> compared to the rest of my paragraph</p>

which results in


(I’ve just put that paragraph element into an existing page I had, hence why it’s not the default black text on a white background).

The purpose of the early exercises getting you to style individual elements like this, is precisely to show you how cumbersome (and just how much work it would be) to define and maintain styles in this way.

As you progress, you will move away from inline styles - as I demonstrated above - and towards defining your CSS in a dedicated file and linking it to your HTML document.

You will, as @8-bitgaming has said, continue to rely on class and id attributes in your HTML though. These are used to allow you to be precise with which elements get which styles.

1 Like

Hello, I’m new to coding and am currently using Codecademy to learn the basics. I’ve already learned HTML and am now on my second lesson of CSS.

I am confused as to why I’m being taught to put certain style effects in style.css and separate styling effects in index.html.

Wouldn’t it make more sense to do all the styling in CSS?

issue got solved!!!

Out of curiosity, how did your quote of me end up with two links, to websites I never even mentioned? :thinking:

1 Like

Not anymore… Links removed.

2 Likes