CSS Tutorial

Ratings:
(4.1)
Views: 1848
Banner-Img
Share this blog:

Cascading Style Sheets (CSS) Tutorial Overview

Welcome to CSS Tutorials. The objective of these tutorials is to provide in depth understanding of Cascading Style Sheets (CSS). In these tutorials we will cover topics such as CSS Tables, CSS Images, CSS Fonts, CSS Styling, CSS Attributes, Fonts etc.

In addition to free tutorials, we will cover common interview questions, issues and how to's of CSS.

Index

CSS Introduction

What is CSS?

CSS stand 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.

Styles Solved a Big Problem

HTML was never intended to conation tags for formatting a document.

HTML was intended to define the content of a document, like:

<h1>This is a heading</h1>

<p> This is a paragraph</p>

When tags like <font>, and color attribute were added to the HTML 3.2 specification, it started a nightmare for web `developers. Development of large websites, where fonts and color information were added to every single page, became a long and expensive process.

To solve this problem, the World Wide Web Consortium (W3C) created CSS.

In HTML 4.0 all formatting could be removed from the HTML document, and stored in a separate CSS file.

All browsers support CSS today.

CSS Saves a Lot of Work!

CSS defines HOW HTML elements are to be displayed.

Styles are normally saved in external.css files. External style sheets enable you to change the appearance and layout of all the pages in a Web site, just by editing one single file!

CSS Syntax

A CSS rule has two main parts: a selector, and one or more declarations:

Screenshot_1

The selector is normally the HTML element you want to style.

Each declaration  consists of a property and a value.

The property is the style attribute you want to change .Ecah prpoeryty has value.

A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document .A style rule is made of three parts:

Selector: A selector is an HTML tag at which style will be applied. This could be any tab like <hl> or <table>etc.

Property: A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS properties .They could be color or border

Value: Values are assigned to properties .For example color property can have value either red or #F1F1F1

You can put CSS style Rule Syntax as follows:

Screenshot_3

Here table is a selector and border is a property and give value 1px solid #C00 is the value of that property.

You can define selectors in various simple ways based on your comfort. Let me put these selectors one by one.

CSS Type Selectors

This is the same selector we have seen above .Again one more example to give color to all level 1 headings:

Screenshot_4

CSS Universal Selectors

Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type:

Screenshot_5

This rule renders the content of every element in our document in black.

CSS Descendant Selectors

Suppose you want to apply a style to a particular element only when it lies inside a particular element. As given in the following example, style rule will apply to <em> element only when it lies inside <ul> tag.

Screenshot_6

CSS Class Selectors

You can define style rules based on the class attribute of the elements. All the elements having that class will be formatted according to the defined rule.

Screenshot_7

This rule renders the content in black for every element with class attribute set to black in our document. You can make it a bit more particular .For example:

Screenshot_8

This rule renders the content in black for only <h1> elements with class attribute set to black.

You can apply more than one class selectors to given element. Consider the following example:

Screenshot_9

CSS ID Selectors

You can define style rules based on the id attribute of the elements. All the elements having that id will be formatted according to the defined rule.

Screenshot_10

This rule renders the content in black for every element with id attribute set to black in our document .You can make it a bit more particular .For example:

Screenshot_11

This rule render the content in black for only <h1> elements with id attribute set to black.

The true power of id selectors is when they are used as the foundation for descendant selectors, For example:

Screenshot_12

In this example all level 2 headings will be displayed in black color only when those headings will lie within tags having id attribute set to black.

CSS Child Selectors

You have seen descendant selectors. There is one more type of selectors which is very similar to descendants but have different functionality. Consider the following example:

Screenshot_13

This rule will render all the paragraphs in black if they are direct child of <body> element. Other paragraphs put inside other elements like <div> or <td> etc. would not have any effect of this rule.

CSS Attributes Selectors

You can also apply styles to HTML elements with particular attributes. The style rule below will match all input elements that has a type attributes with a value of text:

Screenshot_14

The advantage to this method is that the <input type=”submit”/> element is unaffected, and the color applied only to the desired text fields.

There are following rules applied to attribute selector.

  • p[lang]-Selects all paragraphs elements with a lang attribute.
  • p[lang=”fr”]- Selects all paragraphs elements  whose lang attribute has a value of exactly”fr”.
  • p[lang~=”fr”]- Selects all paragraphs elements  whose lang attribute contains the word”fr”.
  • p[lang|=”en”]- Selects all paragraphs elements  whose lang attribute contains values that are exactly “en” or begin with “en-“.

CSS Multiple Style Rules

You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example:

Screenshot_15

Here all the property and value pairs are separated by a semi colon (;) You can keep them in a single line or multiple lines. For better readability we keep them into separate lines.

Grouping Selectors

You can apply a style to many selectors if you like. Just separate the selectors with a comma as given in the following example:

Screenshot_16

This define style rule will be applicable to h1,h2, and h3 element as well. The order of the list is irrelevant. All the elements in the selector will have the corresponding declaration applied to them.

You can combine various class selectors together as shown below:

Screenshot_17

There are four ways to associate styles with your HTML document .Most commonly used methods are inline CSS and External CSS>. We will talk about this in next tutorial

You liked the article?

Like: 1

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox