+ Reply to Thread
Results 1 to 2 of 2

Thread: CSS Syntax

  1. #1
    lhyman's Avatar
    lhyman is offline x10 Sophmore lhyman is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    198

    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. Each header element 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>


    --------------------------------------------------------------------------------

    The id Selector
    The id selector is different from the class selector! While a class selector may apply to SEVERAL elements on a page, an id selector always applies to only ONE element.

    An ID attribute must be unique within the document.

    The style rule below will match a p element that has the id value "para1":

    p#para1
    {
    text-align: center;
    color: red
    }

    The style rule below will match the first element that has the id value "wer345":

    *#wer345 {color: green}

    The rule above will match this h1 element:

    <h1 id="wer345">Some text</h1>

    The style rule below will match a p element that has the id value "wer345":

    p#wer345 {color: green}

    The rule above will not match this h2 element:

    <h2 id="wer345">Some text</h2>


    --------------------------------------------------------------------------------

    CSS Comments
    You can insert comments in 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
    }

    -----------------------------------------------------------------------------

    Inline Styles

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

    ----------------------------------------------------------------------------

    External Style Sheet

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

    An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:

    hr {color: sienna}
    p {margin-left: 20px}
    body {background-image: url("http://forums.x10hosting.com/images/back40.gif")}

    Do NOT leave spaces between the property value and the units! If you use "margin-left: 20 px" instead of "margin-left: 20px" it will only work properly in IE6 but it will not work in Mozilla or Netscape.

    --------------------------------------------------------------------

    Class vs. ID

    When to Use Each Selector
    It is often hard to decide when to use a class versus an id for an element. Here is an easy way to think of the difference:

    Use a class tag if:
    The style is used in various places throughout the document.
    The style is very general.


    Use an id tag if:
    The style is only used once ever in the document.
    The style is specific to a certain area of the document.
    Remember that an id can only appear once in any HTML document. Once you've used the id, it should not be used again on that page.

    -----------------------------------------------------------

    Edit:
    source, my outlook notes, several notes, original source unknown, collected over a few years
    Last edited by lhyman; 08-16-2009 at 10:35 AM. Reason: Automerged Doublepost
    My Name is Larry Hyman from Montreal. I fix computers on site, see my web site for more details:

    http://hyman.sytes.net

  2. #2
    alixain is offline x10Hosting Member alixain is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    32

    Re: CSS Syntax

    Nice

+ Reply to Thread

Similar Threads

  1. MDB2 Syntax Error
    By corewerx in forum Programming Help
    Replies: 2
    Last Post: 12-28-2011, 02:34 PM
  2. SQL syntax Error
    By ponypony in forum Programming Help
    Replies: 4
    Last Post: 06-20-2009, 10:27 AM
  3. Need help with Syntax Error
    By tam-iam in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 02-25-2008, 08:21 AM
  4. Mysql syntax
    By taekwondokid42 in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 09-16-2007, 09:04 AM
  5. about SQL syntax error..
    By tychicus in forum Off Topic
    Replies: 3
    Last Post: 10-03-2006, 11:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers