[ start | index | login ]
start > Development > Cascading Style Sheets

Cascading Style Sheets

Created by mpecher. Last edited by mpecher, 2 years and 260 days ago. Viewed 481 times. #8
[diff] [history] [edit] [rdf]
labels
attachments
Good Online Reference (some material taken from here) >>w3schools

How to Insert a Style Sheet

External Stylesheet

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>

Internal Stylesheet

<head>
<style type="text/css">
hr {color: sienna}
<style>
</head>

Inline Styles

<p style="color: sienna; margin-left: 20px">This is a paragraph</p>



Three levels of defining a style:
1: HTML element (ie override all <p>
2: class (.something). All elements of that class
3: id (#something). This should be unique per page

CSS Syntax

The CSS syntax is made up of three parts: a selector, a property and a value:
selector {property: value}
The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon and surrounded by curly braces:
body {color: black}
If the value is multiple words, put quotes around the value:
p {font-family: "sans serif"}
Note: If you wish to specify more than one property, you must separate each property with a semi-colon. The example below shows how to define a center aligned paragraph, with a red text color:
p {text-align:center;color:red}
To make the style definitions more readable, you can describe one property on each line, like this:
p
{
text-align: center;
color: black;
font-family: arial
}

Grouping

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be green:

h1,h2,h3,h4,h5,h6 
{
color: green

}

The class Selector

With the class selector you can define different styles for the same type of HTML element. Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:

p.right {text-align: right}
p.center {text-align: center}
You have to use the class attribute in your HTML document:
<p class="right">
This paragraph will be right-aligned.
</p>

<p class="center"> This paragraph will be center-aligned. </p>

Note: Only one class attribute can be specified per HTML element! The example below is wrong:
<p class="right" class="center">
This is a paragraph.
</p>
You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:
.center {text-align: center}
In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:
<h1 class="center">
This heading will be center-aligned
</h1>

<p class="center"> This paragraph will also be center-aligned. </p>

* Do NOT start a class name with a number! It will not work in Mozilla/Firefox.

The id Selector

With the id selector you can define the same style for different HTML elements.

The style rule below will match any element that has an id attribute with a value of "green":

#green {color: green}
The rule above will match both the h1 and the p element:
<h1 id="green">Some text</h1>
<p id="green">Some text</p>
The style rule below will match a p element that has an id with a value of "para1":
p#para1
{
text-align: center;
color: red
}
The style rule below will match any p element that has an id attribute with a value of "green":
p#green {color: green}
The rule above will not match an h1 element:
<h1 id="green">Some text</h1>

* Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.

CSS Comments

You can insert comments into CSS to explain your code, which can help you when you edit the source code at a later date. A comment will be ignored by the browser. A CSS comment begins with "/*", and ends with "*/", like this:
/* This is a comment */
p
{
text-align: center;
/* This is another comment */
color: black;
font-family: arial
}
null
    null
  1. class="heading-1">Change hyperlinks when selecting
  2. null
nullMost sites seem to change the link once the mouse hovers over it. A lot of Vision compliancy also requires that this same behavious is replocated if tabbing through the links via the keyboard.
.someStyle a:hover, .someStyle a:focus, .someStyle a:active {
    text-decoration: underline
}
First style causes hover with mouse, :focus tells mozilla to also highlight during tabbing, while :active is for IE to react during a tab.

Changing the Look of Input Buttons

Set the CSS for inputtype=submit. This does not work for IE though. It does not currently recognise it.
To make it work for all browsers, need to define a custom class for a button and define it at that level. eg:
.buttons {
    padding: 0px 5px 1px 5px;
    margin: 0px 3px 0px 3px;
    background: #3165CE;	
    color: #FFFFFF;
    border: none;
    cursor: pointer;
    font-size: 12px;
    font-weight: bold;
    text-align: center;
    font-family:  Verdana, Arial, Helvetica, sans-serif;
}

Cases where Internet Explorer doesn't accept something, this can be overriden:

#RightImage
{
float: right;
max-width: 400px;
max-height: 400px;
margin: 10px 10px 10px 10px;
}

#LeftImage { float: left; max-width: 400px; max-height: 400px; margin: 10px 10px 10px 10px; }

/* fix IE (doesn't like max-width and max-height)*/ *html #RightImage, #LeftImage { width: 400px; height: 300px; }

no comments | post comment

Menu:
Java & J2EE
Development
Books

Help:
Help FAQ
Formatting


< September 2010 >
SunMonTueWedThuFriSat
1234
567891011
12131415161718
19202122232425
2627282930


Logged in Users: (0)
… and 19 Guests.



Disclaimer: Views and opinions are that of the individual author, and not that of Marand Custom Solutions. This site is an open forum for technical content, and the company accepts no liability for any content or view expressed.