About CSS, can I write the code all in one line or I need to put how they show in the course, like in 4 lines? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

About CSS, can I write the code all in one line or I need to put how they show in the course, like in 4 lines?

Example: p{ color: red; } Can I write all in the same line?

5th May 2019, 9:10 PM
Joana Casais
Joana Casais - avatar
8 Answers
+ 6
Thanks!!!
5th May 2019, 9:22 PM
Joana Casais
Joana Casais - avatar
+ 5
you can write it all in the same line https://code.sololearn.com/WKuVTiJyYEcF/?ref=app
5th May 2019, 9:18 PM
Pikachu
Pikachu - avatar
+ 4
you can write it all in 1 line. in fact, you can even write all your CSS code in one line... like this: body { color:#000; font:12px monospace; } p { background:#fff; } however, like Adam said, your code will quickly become illegible and not even you will be able to understand it after you write it. I suggest you only write everything in one line if it is a very simple line, such as: p { color:#ff0; } if you wish to obfuscate your code, then that's also fine, but you should only do so once you've written everything and your code is completed.
6th May 2019, 9:50 AM
🐙evil octopus
🐙evil octopus - avatar
+ 4
As your css gets more complex, better to use multiple lines to improve readability.
7th May 2019, 1:48 AM
Sonic
Sonic - avatar
+ 3
The best practices is, you should write it with clear readable format with multi lines and spaces. So the file is manageable in the long future. And you have use the tools like webpack or rollup or gulp to compress and minify the files to reduce the file size when you deploy. You can also do the same approach for Javascript files.
7th May 2019, 5:42 PM
R R
+ 2
You CAN write it in one line but I highly recommend you don't. When you have hundreds of style definitions and each has dozens or more settings one line code is really difficult to read and edit. I recommend multiline, alphabetize the attributes. And indent them as they are nested in your code. At a glance you can find what you need far easier. Also makes finding missing semi-colons or close braces much easier.
6th May 2019, 3:59 AM
Adam
Adam - avatar
+ 2
You can later on optimize the CSS code for minimal space by removing whitespace. That's what the .min.css files e.g. on GitHub do.
7th May 2019, 7:00 AM
::sк::
::sк:: - avatar
+ 2
You can do this but when you put a lot of code in this way you will not know that reading will destroy your mind but you should write it this way in order to be better and when you work in programming there is a team that will work with you but will not be able to read the code because you are able to make it in one line This is not good I will never graduate from work because of this and writing so relaxing nerves
14th May 2019, 4:35 PM
Abduo Saber
Abduo Saber - avatar