CSS Help

lew

New Member
Hello,

I have multiple h2 headings on my html document, but only want
to center align one of them using CSS. How do I do this without
center aligning all of the headings?
 

jnjc

New Member
There are (at least) two ways to do this:


1) Define your heading as follows:

<h2 id="h2Centered">Test</h2>

then in your style sheet define the styles for this element as

#h2Centered {
...................

}

the # signifies that the style is for an element Id.


2) Define your heading as follows:

<h2 class="h2Centered">Test</h2>

then in your style sheet define the styles for this class as

.h2Centered {
...................

}




HTH,
JC
 

AusQB

New Member
Another way is to give the parent element a class or id and then set the CSS for the h2 tag underneath it.

HTML:
<div class="parent">
     <h2>Test</h2>
</div>

Code:
div.parent {
}

div.parent h2 {
     text-align: center;
}


This means that all instances of the h2 tag within a div with class="parent" will be center aligned.
 

mezangath

New Member
Both ways are fine and should work.

I'd go with jnjc though, instead of id, go for class. That way you can re-use it at another place.
 
Top