Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

p.classname or .classname p, any difference?

So, I am a bit confused over this simple thing, i've googled as much as i could, but i just dont know the right keywords for googling it, i tried CSS selectors, etc, no answer was enough to clear my confusion. So i've also tested and p.classname doesn't even seem to work, but by definition in a book i'm reading ( updated 2012 )

To create a class in CSS and select an element in that class, you write a class selector, like this:

p.classname{ stuff }

So now you have a way of selecting

elements that belong to a certain class to style them.

I couldn't find a definition for

classname p, but myself would give something like this definition "select all p elements that belong to classname", which is basicly the same.

the p.classname works when i give the "classname" to a im still confused, at the moment i would suppose im just going to perma use the .classname p, which works and is basically the same.

So, please help me out to clear this confusion, i've googled, i tried to help this confusion but it didn't work, it only made more.

Thanks

like image 657
Elias Zan Avatar asked Apr 18 '13 22:04

Elias Zan


People also ask

What does p mean in class name?

In the course schedule, the "P" denotes that this person is the Primary Instructor for the course.

What is class p in CSS?

when you do .classname p. You are looking for a p that is a descendant of the classes classname . In CSS . is used as a selector for a class name.

What is p class tag in HTML?

HTML <output> HTML <param> The class attribute assigns one or more classnames to the <p> tag. Classnames are defined in a stylesheet or in a local <style> element. Classes, i.e. classnames, are used to style elements.

Can you add a class to p tag in HTML?

To add a class, you go to your element, <p>, and just like IDs, you add class= (Instead of id=) and insert your class name.


2 Answers

So when you do

p.classname{
  ... 
}

You are looking for a <p> with class classname

when you do

.classname p

You are looking for a p that is a descendant of the classes classname.

In CSS . is used as a selector for a class name.

like image 91
karthikr Avatar answered Sep 27 '22 20:09

karthikr


CSS works by reading each rule in order, example.

p {font-weight: bold;}

p span {font-weight:normal;}

<p>Hiya</p> // BOLD

<p><span>HIYA</span></p> // NORMAL

Same goes for classes

.bold {font-weight: bold;}

span {font-weight:normal;}

<p class="bold">I AM BOLD <span>I AM NOT</span> BOLD AGAIN</p>

<p class="bold"><span> I AM ALL NORMAL</span></p>

So in your example defining a class first will target each and every element that has that class.

By defining something after that class .class p it will target all elements inside that class which are p.

Hope this helped

UPDATE

so you can understand better,

div {color: blue;}
div p {color: red;}
div p span {color: yellow;}
div ul {color: purple;}

<div>I AM BLUE <p>I AM RED <span> I AM YELLOW</span></p>I AM BLUE</div>

<p>I HAVE NO CSS ATTACHED TO ME</p>

<div><ul><li>I AM PURPLE</li></ul> I AM BLUE</div>

It works exactly the same for classes as it does with elements.

like image 29
GriffLab Avatar answered Sep 27 '22 18:09

GriffLab