Learning to code and have a theoretical question

Hey guys, i’m fairly new to programming and codding and etc, and I’ve been doing the free code camp challenges for the past 2 days, and I’ve just entered CSS and I stumbled upon something that made me wonder, and I couldn’t find anything in other forums etc.

Why is that when you intend to style your text by using a block, we use it like this

<style>
  .red-text {
    color: red;
  }
</style>

and not like this?

<style> .red-text {color: red;}
</style>

Thanks in advance everyone :slight_smile:

1 Like

This is for readability.

Having only few words on a line is much easier to read than having more words in a single line.

2 Likes

As said. Because it is more readable and better syntax style. Some things are conventions, like how we use spaces, punctuation, or separate paragraphs (youcanreadthisbutitgetsreallyannoying)

Also, think about how the second example you have would look with more the just one style.

<style> .red-text {color: red;}
        .blue-text {color: blue;}
</style>

Or how is looks with more properties

<style> .red-text {color: red;}
        .blue-text {color: blue; font-size: 16px; text-align: center; padding: 10px; margin-bottom: 20px;}
</style>
1 Like

Aside from readability, it’s also easier to copy/paste sections when the tags are on separate lines. Ideally, you should use an editor that does syntax highlighting and formatting, so you can just hit the “reformat” key/button and not worry about getting it exactly right by hand.

1 Like

Thanks everyone, it was really helpful!! :slight_smile:

1 Like

Just to glom on, you’re encountering “block indentation,” a common styling convention wherein we use indentation to show which commands are grouped together, like this algorithm:

while (teeth are not clean)
  Apply brush to a dirty area
  For 10 times
    Move brush left
    Move brush right
  end for
  Spit
  Rinse brush for 2 seconds
end while

I hope you can see how the layout of the code shows you what to do.

2 Likes

Thanks! Really helped me out with the concept, tou not that complicated, as a beginner I’m still getting some things figured out, so thanks, really appreaciate it

1 Like

Its a very good question, wondering why we do things a certain way demonstrates critical thinking on your part. We should not just do things because that is how they are done, we should do things because we understand why they are done that way.

1 Like