I recently started doing small projects using HTML& CSS but I find one thing doubtful when I look at other projects! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I recently started doing small projects using HTML& CSS but I find one thing doubtful when I look at other projects!

So, I use the class attribute in div tag and also in form tag and give their properties..but many people are writing the class in as many tags as possible.. Is what I am doing is correct? Or should I mention a class attribute for every tag in div tags or form tags and mention their properties..like many others do!! Can anyone explain why do anyone keep many class attributes?

21st Jul 2022, 5:32 PM
Prasuna
Prasuna - avatar
9 Answers
+ 3
An ID can only reference one html element, however a class can 'group' elements so they can be referenced as a group. For instance, you have 5 divs, you want all the divs the same size so you give all of them a class which is written the exact same in all divs <div class="sizeclass">. This let's you style these in css by using one '.sizeclass {width...}. What if you want 3 of the divs purple and 2 green along with the uniform size? Then you do this: <div class="sizeclass purpleclass" or add an extra class="" but dont have to> for all the divs you want purple or green.
21st Jul 2022, 11:31 PM
pHANTOM|sTEELE
+ 1
I would say using a class in multiple Tags isn't necessary right or wrong but Just more comfortable. Let's say you make a web-code which have four quarders with different lenght but same color, margin, allignment and etc. at that Point would I make one class named "Quarder" and use in line css to Change the size. When you multiply a existing element like with a Blog multiple text "fields" why make always something new when you already have it and when you change size or something you need to Change the value one time instead of multiple Times.
21st Jul 2022, 6:50 PM
Felix Alcor
Felix Alcor - avatar
+ 1
Oh! Ok. If it is, I have to use it then! It will be much easier I guess. I think it might make sense or I can understand this usage when I make a big project or projects which require every element property to change! Thanks!! Felix.
21st Jul 2022, 6:59 PM
Prasuna
Prasuna - avatar
+ 1
Set only class for elements you need to style, and make sure your code is dry (dont repeat yourself). If you type same styling or group of styling more than 2 time it is good to use class and dont repeat yourself. So if you have many elements with for example red color, make class for it.. If you ever used bootstrap you will see benefit of classes because it allow you to type style much organized and faster. You can also make your own styling template and reuse it in many codes, like color palete, font styling, spacing (padding, margin...)
21st Jul 2022, 10:48 PM
PanicS
PanicS - avatar
+ 1
PanicS I'm still learning CSS.. haven't started Bootstrap yet.. I will try for my next project and yes the code should be dry!.. to get a better understanding. Thanks!
22nd Jul 2022, 5:48 AM
Prasuna
Prasuna - avatar
+ 1
pHANTOM|sTEELE Yes, I got this..in many projects..I have seen people using classes like the above "sizeclass purpleclass" I understood, will try for my next project. Thanks!
22nd Jul 2022, 5:51 AM
Prasuna
Prasuna - avatar
+ 1
Prasuna For some feature you need JavaScript but not all of tham, better learn JavaScript before so you wont have problem to understand some of this. Also there are similar libraries as bootstrap, like tailwind css, materialize and many more, bootstrap is very popular. https://www.spec-india.com/blog/bootstrap-alternatives My suggestion is to practice css and build lot of things before learning bootstrap, so you will have a strong css knowledge.
22nd Jul 2022, 7:41 AM
PanicS
PanicS - avatar
0
PanicS should I learn bootstrap after CSS or after JavaScript?
22nd Jul 2022, 7:02 AM
Prasuna
Prasuna - avatar
0
Ok! Thanks a lot!!PanicS
22nd Jul 2022, 7:48 AM
Prasuna
Prasuna - avatar