views:

24

answers:

3

Hi All,

Say I have a class name person with foll style in css-

.person { font: 10px Arial black; }

now if i want to assign as h1 tag to this class so for any instance of h1 tag in class person i want the some styles to be processed

i tried

.person h1 { color: red; }

but its not working.

Iam bit confused with the syntax

+2  A: 

When two selectors address the same element, you write them together, no spaces.

For a h1 of the class person use

h1.person

For a h1 with the class person and the ID jay you'd use

h1.person#jay
Pekka
what if i want to say in class person any occurance of h1 use the foll style
kjkjl
That is as you say above. `.person h1` = Any h1 that is the child of an element with the class "person". That should definitely work.
Pekka
ok let me double check. Thanks Pekka
kjkjl
not working heres the code CSS - .infolink h4{ font: 10px Arial, Helvetica, sans-serif; color: green;}HTML - <p class="infolink"><h4>This is h4</h4></p>
kjkjl
Tom
Can you try `color: green !important;`? If that does not help, check whether the style sheet is applied at all. Maybe update your question with your full CSS.
Pekka
CSS - body { margin:0; padding: 0; }#container { margin: 10px 50px 10px 50px; border: 1px dotted black; width: 150px; height: 200px;}h2 { font: 12px Arial, Helvetica, sans-serif bold; border-bottom:2px solid black; text-align: center;}/*h4 { font: 12px Arial, Helvetica, sans-serif black;}*/ul li { text-align:left; font-size: 10px;}a { text-decoration: none;}.review { font: 10px Arial, Helvetica, sans-serif normal; text-align:left; margin: 25px 5px 10px 10px;}.infolink h4{ font: 10px Arial, Helvetica, sans-serif; color: red !important;}
kjkjl
check the CSS code i commented the the previous h4
kjkjl
And that style sheet is definitely being included? The other definitions work?
Pekka
yup its included <link rel="stylesheet" type="text/css" href="styles/style1.css" />
kjkjl
Can you post a live link?
Pekka
And do you have visual confirmation that some of the other styles are working as well? Is there a container with a 1px black dotted border, etc.?
Pekka
yup it has all other styles work i even included an inline style for the h4 tag with h4.infolink and it works
kjkjl
sorry i dont have a link link for this. Thanks Pekka for everything
kjkjl
i got it pekka ... i cannot use <p> tag for that just changed the p to div and works fine.
kjkjl
I don't really understand what the problem was, but if it works, all right! :)
Pekka
A: 

You can't assign classes to elements in CSS. You can only do it in the DOM tree, i.e. either in the HTML or by postprocessing it in JavaScript. For what you really want to know, see Pekka's answer.

reinierpost
A: 

.person h1 refers to this sort of element:

<div class="person">
    <h1>This is getting the style</h1>
</div>

To get an element like this:

<h1 class="person">This is getting the style</h1>

You need to use h1.person.

Macha
what if i want to say in class person any occurance of h1 use the foll style
kjkjl
Then you use the first example, `.person h1`
Macha