Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

<HOT>Learn CSS {Cascading Style Sheets} Basics -to- Professional </HOT>

Cascading Style Sheets

What is CSS?

CSS stands for Cascading Style Sheets
Styles define how to display HTML elements
Styles were added to HTML 4.0 to solve a problem
External Style Sheets can save a lot of work
External Style Sheets are stored in CSS files


The syntax for CSS is different than that of (X)HTML markup. Though it is not too confusing, once you take a look at it. It consists of only 3 parts.

Code
selector { property: value }

The selector is the (X)HTML element that you want to style. The property is the actual property title, and the value is the style you apply to that property.

Each selector can have multiple properties, and each property within that selector can have independent values. The property and value are separated with a colon and contained within curly brackets. Multiple properties are separated by a semi colon. Multiple values within a property are sperated by commas, and if an individual value contains more than one word you surround it with quotation marks. As shown below.

Code
body {
  background: #ee;
  font-family: “Trebuchet MS”, Verdana, Arial, serif;
}

As you can see in the above code I have separated the color from the font-family with a semi-colon, separated the various fonts with commas and contained the “Trebuchet MS” within quotations marks. The final result sets the body color to light grey, and sets the font to ones that most users will have installed on there computer.

I have changed the way I layout my code, but you can arrange it in one line if you choose. I find that it is more readable if I spread each property to a separate line, with a 2 space indention.

Inheritance
When you nest one element inside another, the nested element will inherit the properties assigned to the containing element. Unless you modify the inner elements values independently.

For example, a font declared in the body will be inherited by all text in the file no matter the containing element, unless you declare another font for a specific nested element.

Code
body {font-family: Verdana, serif;}

Now all text within the (X)HTML file will be set to Verdana.

If you wanted to style certain text with another font, like an h1 or a paragraph then you could do the following.

Code
h1 {font-family: Georgia, sans-serif;}
p {font-family: Tahoma, serif;}

Now all <h1> tags within the file will be set to Georgia and all <p> tags are set to Tahoma, leaving text within other elements unchanged from the body declaration of Verdana.

There are instances where nested elements do not inherit the containing elements properties.

For example, if the body margin is set to 20 pixels, the other elements within the file will not inherit the body margin by default.

Code
body {margin: 20px;}

Combining Selectors
You can combine elements within one selector in the following fashion.

Code
h1, h2, h3, h4, h5, h6 {
  color: #009900;
  font-family: Georgia, sans-serif;
}

As you can see in the above code, I have grouped all the header elements into one selector. Each one is separated by a comma. The final result of the above code sets all headers to green and to the specified font. If the user does not have the first font I declared it will go to another sans-serif font the user has installed on their computer.

Comment tags
Comments can be used to explain why you added certain selectors within your css file. So as to help others who may see your file, or to help you remember what you we’re thinking at a later date. You can add comments that will be ignored by browsers in the following manner.

Code
/* This is a comment */

You will note that it begins with a / (forward slash) and than an (asterisks) then the comment, then the closing tag which is just backward from the opening tag (asterisks) then the / (forward slash).

The class selector allows you to style items within the same (X)HTML element differently. Similiar to what I mentioned in the introduction about inline styles. Except with classes the style can be overwritten by changing out stylesheets. You can use the same class selector again and again within an (X)HTML file.

To put it more simply, this sentence you are reading is defined in my CSS file with the following.

Code
p {
  font-size: small;
  color: #33
}

Pretty simple, but lets say that I wanted to change the word “sentence” to green bold text, while leaving the rest of the sentence untouched. I would do the following to my (X)HTML file.

Code
<p>
To put it more simply, this <span class=”greenboldtext”>sentence</span> you are reading is styled in my CSS file by the following.
</p>

Then in my CSS file I would add this style selector:

Code
.greenboldtext{
  font-size: small;
  color: #008080;
  font-weight: bold;
}

The final result would look like the following:

To put it more simply, this sentence you are reading is styled in my CSS file by the following.

Please note that a class selector begins with a (.) period. The reason I named it “greenboldtext” is for example purposes, you can name it whatever you want. Though I do encourage you to use selector names that are descriptive. You can reuse the “greenboldtext” class as many times as you want.

IDs are similar to classes, except once a specific id has been declared it cannot be used again within the same (X)HTML file.

I generally use IDs to style the layout elements of a page that will only be needed once, whereas I use classes to style text and such that may be declared multiple times.

The main container for this page is defined by the following.

Code
<div id=”container”>
Everything within my document is inside this division.
</div>

I have chosen the id selector for the “container” division over a class, because I only need to use it one time within this file.

Then in my CSS file I have the following:

Code
#container{
  width: 80%;
  margin: auto;
  padding: 20px;
  border: 1px solid #666;
  background: #ff;
}

You will notice that the id selector begins with a (#) number sign instead of a (.) period, as the class selector does.

Ok so you have finished the first 4 chapters in my series. You have learned the very basics of CSS, how the syntax works and a bit about classes and IDs. Now we are gonna take a quick break from CSS and focus on the (X)HTML side of using it.

Divsions
Divisions are a block level (X)HTML element used to define sections of an (X)HTML file. A division can contain all the parts that make up your website. Including additional divisions, spans, images, text and so on.

You define a division within an (X)HTML file by placing the following between the <body></body> tags:

Code
<div>
Site contents go here
</div>

Though most likely you will want to add some style to it. You can do that in the following fashion:

Code
<div id=”container”>
Site contents go here
</div>

The CSS file contains this:

Code
#container{
  width: 70%;
  margin: auto;
  padding: 20px;
  border: 1px solid #666;
  background: #ff;
}

Now everything within that division will be styled by the “container” style rule, I defined within my CSS file. A division creates a linebreak by default. You can use both classes and IDs with a division tag to style sections of your website.

Spans are very similar to divisions except they are an inline element versus a block level element. No linebreak is created when a span is declared.

You can use the span tag to style certain areas of text, as shown in the following:

Code
<span class=”italic”>This text is italic</span>

Then in my CSS file:

Code
.italic{
  font-style: italic;
}

The final result is: This text is italic.

The purpose of the last 2 chapters was to provide you with a basis for using CSS in an (X)HTML file. For a more detailed explaination of XHTML please visit W3Schools

As you may have guessed, the margin property declares the margin between an (X)HTML element and the elements around it. The margin property can be set for the top, left, right and bottom of an element. (see example below)

Code
margin-top: length percentage or auto;
  margin-left: length percentage or auto;
  margin-right: length percentage or auto;
  margin-bottom: length percentage or auto;

As you can also see in the above example you have 3 choices of values for the margin property

length
percentage
auto
You can also declare all the margins of an element in a single property as follows:

margin: 10px 10px 10px 10px;

If you declare all 4 values as I have above, the order is as follows:

top
right
bottom
left
If only one value is declared, it sets the margin on all sides. (see below)

margin: 10px;

If you only declare two or three values, the undeclared values are taken from the opposing side. (see below)

Code
margin: 10px 10px; /* 2 values */
  margin: 10px 10px 10px; /* 3 values */
You can set the margin property to negative values. If you do not declare the margin value of an element, the margin is 0 (zero).

Code
margin: -10px;

Elements like paragraphs have default margins in some browsers, to combat this set the margin to 0 (zero).

Code
p {margin: 0;}

Note: You do not have to add px (pixels) or whatever units you use, if the value is 0 (zero).

You can see in the example below, the elements for this site are set to be 20px (pixels) from the body

Code
body{
  margin: 20px;
  background: #ee;
  font-size: small;
  font-family: Tahoma, Arial, “Trebuchet MS”, Helvetica, sans-serif;
  text-align: left;
}

Padding is the distance between the border of an (X)HTML element and the content within it.

Most of the rules for margins also apply to padding, except there is no “auto” value, and negative values cannot be declared for padding.

Code
padding-top: length percentage;
  padding-left: length percentage;
  padding-right: length percentage;
  padding-bottom: length percentage;

As you can also see in the above example you have 2 choices of values for the padding property

length
percentage
You can also declare all the padding of an element in a single property as follows:

padding: 10px 10px 10px 10px;

If you declare all 4 values as I have above, the order is as follows:

top
right
bottom
left
If only one value is declared, it sets the padding on all sides. (see below)

padding: 10px;

If you only declare two or three values, the undeclared values are taken from the opposing side. (see below)

Code
padding: 10px 10px; /* 2 values */
  padding: 10px 10px 10px; /* 3 values */
If you do not declare the padding value of an element, the padding is 0 (zero).

Note: You do not have to add px (pixels) or whatever units you use, if the value is 0 (zero).

You can see in the example below, the main container for this site has 30px (pixels) of padding between the border and the text.

Code
#container{
  width: 70%;
  margin: auto;
  padding: 30px;
  border: 1px solid #666;
  background: #ff;
}

You can set the color of text with the following:

Code
color: value;

Possible values are

color name – example:(red, black…)
hexadecimal number – example:(#ff0000, black)
RGB color code – example:(rgb(255, 0, 0), rgb(0, 0, 0))

Letter Spacing
You can adjust the space between letters in the following manner. Setting the value to 0, prevents the text from justifying. You can use negative values.

letter-spacing: value;

Possible values are

normal
length
Example:

These letters are spaced at 5px.

Text Align
You can align text with the following:

text-align: value;

Possible values are

left
right
center
justify
Examples:

This text is aligned left.

This text is aligned in the center.

This text is aligned right.

This text is justified.

Text Decoration
You can decorate text with the following:

text-decoration: value;

Possible values are

none
underline
overline
line through
blink
Examples:

This text is underlined.

This text is overlined.

This text has a line through it.

This text is blinking (not in internet explorer).

Text Indent
You can indent the first line of text in an (X)HTML element with the following:

text-indent: value;

Possible values are

length
percentage
Examples:

This text is indented 10px pixels.

Text Transform
You can control the size of letters in an (X)HTML element with the following:

text-transform: value;

Possible values are

none
capitalize
lowercase
uppercase
Examples:

This First Letter In Each Word Is Capitalized, Though It Is Not In My File.

THIS TEXT IS ALL UPPERCASE, THOUGH IT IS ALL LOWERCASE IN MY FILE.

this text is all lowercase. though it is all uppercase in my file.

White Space
You can control the whitespace in an (X)HTML element with the following:

white-space: value;

Possible values are

normal
pre
nowrap

Word Spacing
You can adjust the space between words in the following manner. You can use negative values.

word-spacing: value;

Possible values are

normal
length
Example:

These words are spaced at 5px.

I Hope you Liked the Tutorials. Please Don't leech the Tuts. Say thanks if it helped you out

Credits : Google

Code
Coming Soon another HTML Tuts with CSS, Stay Tuned -

thank you very munch really need it !!
This is really indepth, I've bookmarked it for referance later. Although the one thing I think you didn't cover is how to markout CSS from html/php :/
Helped me alot. nice reference anyways
Thanks bro .. :D
(Mar 20, 2012 11:12 AM)BaqarNaqvi2 Wrote:  Helped me alot. nice reference anyways
Thanks bro .. :D

I Liked It helped you out.!

(Mar 20, 2012 09:05 AM)DarkClouds Wrote:  thank you very munch really need it !!

Yes, Everyone got Basics of HTML so i thought of Posting Something about CSS

(Mar 20, 2012 09:13 AM)Rita Ora Wrote:  This is really indepth, I've bookmarked it for referance later. Although the one thing I think you didn't cover is how to markout CSS from html/php :/

Sure sir, I have marked out ONLY CSS. Your Demand will be fulfilled soon.! 🤝
This looks like a very good starting place for CSS, keep up the good work x.Robot.x!
Seeking professional doxers & hackers, PM me by
Clicking Here
(Mar 21, 2012 04:06 AM)♕αтσмsαι♕ Wrote:  This looks like a very good starting place for CSS, keep up the good work x.Robot.x!

Thank you sir :) I Liked you liked it ,!
Liked the Tuts, Really helped me. Thanks
(Mar 21, 2012 08:03 PM)kunalbhai Wrote:  Liked the Tuts, Really helped me. Thanks

Thanks :) Liked you liked it.! 35 Chars
Very nice read thanks for gettin' me started with CSS.
(Mar 26, 2012 02:01 PM)JumpWar Wrote:  Very nice read thanks for gettin' me started with CSS.

Liked it help you out,! Enjoy the tuts.! Gonna get some upgraded Tuts too,! Thanks
Very informative thread.
I like what I see, keep it up.
(Apr 7, 2012 01:27 AM)Webmasters Wrote:  Very informative thread.
I like what I see, keep it up.

Thank you sir, Liked that you Liked it :)
Hope it Helps All :)
Next step: Turn the tutorial into a video for visual learners like me. :P Pming you now for some questions.
For example, if the body margin is set to 20 pixels, the other elements within the file will not inherit the body margin by default.

Code
body {margin: 20px;}

i didnt got what u said in this please help me

i want to learn css


Possibly Related Threads...
Thread / Author Replies Views Last Post
by Camf ∆Jan 6, 2021 01:53 PM 0 75 Jan 6, 2021 01:53 PM
Last Post: Camf ∆
by severosMay 3, 2018 10:18 PM 8 185 May 3, 2018 10:18 PM
Last Post: Invincible
by HelltoyMar 9, 2018 12:30 PM 5 181 Mar 9, 2018 12:30 PM
Last Post: Rushax