• Home
  • HTML
    • HTML Introduction
    • HTML Basic
    • HTML Elements
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Text Formatting
    • HTML Styles
    • HTML Comments
    • HTML Styles - CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Block and Inline Elements
    • HTML class Attribute
    • HTML Forms
    • HTML Media
  • CSS
    • Introduction of CSS
    • CSS Syntax
    • CSS Selectors
    • How To Add CSS
    • CSS Comments
    • CSS Colors
    • CSS Backgrounds
    • CSS Borders
    • CSS Margins
    • CSS Text
    • CSS Lists
    • CSS Tables
    • CSS Box Model
    • CSS Dimension
    • CSS Padding
    • CSS Border
    • CSS Margin
    • CSS Outline
    • CSS Cursors
    • CSS Overflow
    • CSS Units
    • CSS Visual Formatting
    • CSS Display
    • CSS Visibility
    • CSS Position
    • CSS Layers
    • CSS Float
    • CSS Alignment
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS Media Types
    • CSS Sprites
    • CSS Opacity
    • CSS Attribute Selectors
    • CSS Validation
    • CSS3 Border
    • CSS3 Gradients
    • CSS3 Text Overflow
  • JavaScript
    • JS Introduction
    • JS Getting Started
    • JS Syntax
    • JS Variables
    • JS Generating Output
    • JS Data Types
    • JS Operators
    • JS Events
    • JS Strings
    • JS Numbers
    • JS If Else
    • JS Switch Case
    • JS Arrays
    • JS Sorting Arrays
    • JS Loops
    • JS Functions
    • JS Objects
    • JS DOM Nodes
    • JS DOM Selectors
    • JS DOM Styling
    • JS DOM Get Set Attributes
    • JS DOM Manipulation
    • JS DOM Navigation
    • JS Window
    • JS Screen
    • JS Location
    • JS History
    • JS Navigator
    • JS Dialog Boxes
    • JS Timers
    • JS Date and Time
    • JS Math Operations
    • JS Type Conversions
    • JS Event Listeners
    • JS Event Propagation
    • JS Borrowing Methods
    • JS Hoisting Behavior
    • JS Closures
    • JS Strict Mode
    • JS JSON Parsing
    • JS Error Handling
    • JS Regular Expressions
    • JS Form Validation
    • JS Cookies
    • JS AJAX Requests
    • JS ES6 Features
  • jQuery
    • jQuery Introduction
    • jQuery Syntax
    • jQuery Selectors
    • jQuery Events
    • jQuery Show/Hide
    • jQuery Fade
    • jQuery Slide
    • jQuery Animation
    • jQuery Stop
    • jQuery Chaining
    • jQuery Callback
    • jQuery Get/Set
    • jQuery Insert
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
    • jQuery Filtering
    • jQuery Ajax
    • jQuery Load
    • jQuery Get/Post
    • jQuery No-Conflict
  • PHP
    • PHP Introduction
    • PHP Install
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Namespaces
  • SQL
    • Introduction to SQL
    • SQL Create Command
    • SQL ALTER Command
    • SQL Truncate Drop Rename
    • INSERT SQL command
    • UPDATE SQL command
    • DELETE SQL command
    • SQL COMMIT command
    • SQL ROLLBACK command
    • SQL GRANT and REVOKE Command
    • SQL WHERE clause
    • SQL LIKE clause
    • SQL ORDER BY Clause
    • SQL Group By Clause
    • SQL HAVING Clause
    • SQL DISTINCT keyword
    • SQL AND OR operator
    • SQL Constraints
    • SQL Functions
    • SQL JOIN
  • Python
    • Getting started with Python
    • Introduction to IDLE
    • Python 2.x vs. Python 3.x
    • Syntax Rules and First Program
    • Numbers and Math Functions
    • Python Operators
    • Python Variables
    • Python Modules and Functions
    • Python Input and Output
    • Data Types in Python
    • String in Python
    • String Functions in python
    • Lists in Python
    • Utilizing List Elements by Iterating
    • Deleting List Elements & other Functions
    • Dictionaries in Python
    • Functions for Dictionary
    • Tuples in Python
    • Relational and Logical Operators
    • Conditional Statements in Python
    • Looping in Python
    • Define Functions in Python
    • Python-Introduction to OOP
    • Object Oriented Programming in Python
    • Classes in Python
    • The concept of Constructor
    • Destructors - Destroying the Object in Python
    • Inheritance in Python
    • Access Modifers in Python
    • Types of Inheritance
    • Method Overriding in Python
    • Polymorphism
    • static Keyword
    • Operator Overloading Python
    • Introduction to Error Handling
    • Exception Handling: try and except
    • Exeption Handling: finally
    • Exception Handling: raise
    • File Handling
    • Reading and Writing File
    • Introduction to Multithreading
    • Threading Module in Python
    • Thread Object
    • Lock Object
    • RLock Object
    • Event Object
    • Timer Object
    • Condition Object
    • Barrier Object
    • __name__ Variable in Python
    • Iterable and Iterator
    • yield Keyword
    • Python Generators
    • Python Closures
    • Python Decorators
    • @property Decorator in Python
    • Assert Statement
    • Garbage Collection
    • Shallow and Deep Copy
    • Introduction to Logging
    • Configure Log LEVEL, Format etc
    • Python Logging in a file
    • Python Logging Variable Data
    • Python Logging Classes and Functions
    • Python MySQL Introduction
    • Create Database - Python MySQL
    • Create Table - Python MySQL
    • Insert Data in Table
    • Select Data from Table
    • Update data in Table
    • Delete data from Table
    • Drop Table from Database
    • WHERE clause - Python MySQL
    • Order By clause - Python MySQL
    • Limit clause - Python MySQL
    • Table Joins - Python MySQL
  • MongoDB
    • MongoDB Introduction
    • Overview of MongoDB
    • MongoDB vs SQL Databases
    • Advantages of MongoDB
    • When to go for MongoDB
    • Data Modelling in MongoDB
    • Is MongoDB really Schemaless?
    • Installing MongoDB on Windows and Linux
    • Datatypes in MongoDB
    • Create and Drop Database in MongoDB
    • MongoDB: Creating a Collection
    • CRUD Operations in MongoDB
    • Data Relationships in MongoDB
    • Indexing in MongoDB
    • Sorting in MongoDB
    • Aggregation in MongoDB
    • Data Backup and Restoration in MongoDB
    • Sharding in MongoDB
    • Java Integration with MongoDB
  • Elixir
    • Elixir Overview
    • Elixir Environment
    • Elixir Basic Syntax
    • Elixir Data Types
    • Elixir Variables
    • Elixir Operators
    • Elixir Pattern Matching
    • Elixir Decision Making
    • Elixir Strings
    • Elixir Char Lists
    • Elixir Lists and Tuples
    • Elixir Keyword Lists
    • Elixir Maps
    • Elixir Modules
    • Elixir Aliases
    • Elixir Functions
    • Elixir Recursion
    • Elixir Loops
    • Elixir Enumerables
    • Elixir Streams
    • Elixir Structs
    • Elixir Protocols
    • Elixir File I/O
    • Elixir Processes
    • Elixir Sigils
    • Elixir Comprehensions
    • Elixir Typespecs
    • Elixir Behaviours
    • Elixir Errors Handling
    • Elixir Macros
    • Elixir Libraries
  • TypeScript
    • TypeScript Overview
    • Install TypeScript
    • First TypeScript Program
    • Type Annotation
    • TypeScript Variable
    • TypeScript Data Type Number
    • TypeScript Data Type String
    • TypeScript Data Type Boolean
    • TypeScript Arrays
    • TypeScript Tuples
    • TypeScript Enum
    • TypeScript Union
    • TypeScript Any Data Type
    • TypeScript Void Data Type
    • TypeScript Never Data Type
  • Home
  • Introduction of CSS
  • CSS Syntax
  • CSS Selectors
  • How To Add CSS
  • CSS Comments
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Margins
  • CSS Text
  • CSS Lists
  • CSS Tables
  • CSS Box Model
  • CSS Dimension
  • CSS Padding
  • CSS Border
  • CSS Margin
  • CSS Outline
  • CSS Cursors
  • CSS Overflow
  • CSS Units
  • CSS Visual Formatting
  • CSS Display
  • CSS Visibility
  • CSS Position
  • CSS Layers
  • CSS Float
  • CSS Alignment
  • CSS Pseudo-classes
  • CSS Pseudo-elements
  • CSS Media Types
  • CSS Sprites
  • CSS Opacity
  • CSS Attribute Selectors
  • CSS Validation
  • CSS3 Border
  • CSS3 Gradients
  • CSS3 Text Overflow
Home >> css >> Introduction of CSS

Introduction of CSS

Cascading Style Sheets, also referred as CSS, is a stylesheet language that simplifies the process of making web pages presentable. It is a simple mechanism that lets you apply styles such as fonts, colours, spacing, etc., to web documents. Above all, this cornerstone technology enables you to do this independent of the HTML that makes up each web page. This easy-to-learn and understand language offers robust control over the presentation and styling of an HTML document. 

 

WHY CSS? 

  • Saves time: It simplifies page making and minimises the time spent as you can use one CSS file on multiple HTML pages. 
  • Easy Maintenance: Since all codes are placed in one single file, any change made in style is automatically updated in all elements across web pages, making it very easy to maintain.
  • Search Engines: CSS has a simpler structure and less coding, so search engines can easily "read" its content and index webpages faster.
  • Superior styles to HTML: With a more comprehensive array of attributes than HTML, CSS gives a better look to your HTML page than HTML attributes. 
  • Offline Browsing: With the assistance of an offline cache, CSS can store web applications locally. This lets you view offline websites.

 

CSS Syntax:

CSS is a rule-based coding language used to specify website formats. A style rule set has two parts- a selector and a declaration block.

Syntax :

Selector -- h1 
Declaration -- {color:blue;font size:12px;}

 

  • The selector is used to select the HTML element you want to style. It can choose one or more elements simultaneously.
  • The declarations determine how the elements are formatted on a webpage. The declaration block includes one or more declarations separated by semicolons. 
  • Every declaration has two parts: a CSS property name and a value separated by a colon. For example, –; colour is property, and blue is value. and –; font-size is property and 12px is value. 
  • Declaration always ends with a semicolon, and curly braces surround declaration blocks.

Example :

In the following example all p elements will be center-aligned, with a blue text color :

p {
	color: blue;
	text-align: center;
}

Try with example

Output :

 

CSS Selectors

CSS selectors define the HTML elements to which CSS rules apply. It finds the HTML elements on the basis of their element name, id, class, attribute, and more.

1. THE UNIVERSAL SELECTORS: Instead of selecting elements of a specific type, the universal selector simply matches any element type's name.

Example :

* {
	color: red;
	text-align: center;
}

Try with example

Output :

 

2. THE ELEMENT SELECTOR: It is used for selecting elements on the basis of the element name. For selecting all p elements on a page (in the example below, all p elements will be centre-aligned, with a red text colour):

Example :

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

Try with example

 

3. THE DESCENDANT SELECTOR: The descendant selector is used for matching all elements that are descendants of a particular element. On selecting a descendant selector, the style rule will apply to a specific element only when it lies inside a particular element. For example, the style rule will apply to the em element only when it lies inside the ul tag in the below-mentioned example.

Example :

<!DOCTYPE html>
<html>
<head>
<style>
ul em {
color: red;
}
</style>
</head>
<body>

<ul>
  <li><em>Coffee</em></li>
  <li>Tea</li>
  <li>Milk</li>
</ul>

</body>
</html>

Try with example

 

4. THE ID SELECTOR :  

  • It matches an element on the basis of the value of the element's id attribute. 
  • Every element id is unique within a page; hence the id selector is used for selecting one unique element! 
  • An id name cannot start with a number.
  • For choosing an element with a specific id, write a hash (#) character, followed by the element's id. 
  • In the example below, the CSS style rule will be applied to the HTML element with id=" para1″


5. THE CLASS SELECTORS :

  • It chooses HTML elements having a specific class attribute. 
  • For selecting elements with a particular class, write a period (.) character, followed by the name of the class.
  •  In the following example, all HTML elements with class= "center" will be red and center-aligned:

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

<p class="center large">This paragraph refers to two classes.</p>

Try with example


6. GROUPING SELECTORS

The CSS grouping selector chooses all the HTML elements with the same style definitions, like this:

h1 {
	text-align: center;
	color: blue;
}

h2 {
	text-align: center;
	color: blue;
}

p {
	text-align: center;
	color: blue;
}

For minimising the code, it's better to group the selectors. You can separate each selector with a comma to group selectors. In the following example, we have grouped the selectors from the code above::

h1, h2, p {
	text-align: center;
	color: red;
}

Before CSS:

<!DOCTYPE html>
<html>
<head>
	<title>Example</title>
</head>
<body>
	<main>
	<h1>HTML Page</h1>
         <p>This is a basic web page.</p>
	</main>
</body>
</html>

After CSS

In this example, we add some CSS.

<!DOCTYPE html>
<html>
<head>
	<title>Example</title>
	<style>
	main {
		width: 200px;
		height: 200px;
		padding: 10px;
		background: beige;
	}
	h1 {
		font-family: fantasy, cursive, serif;
		color: olivedrab;
		border-bottom: 1px dotted darkgreen;
	}
	p {
		font-family: sans-serif;
		color: orange;
	}
	</style>
</head>
<body>
	<main>
	<h1>HTML Page</h1>
	
       <p>This is a basic web page.</p>

	</main>
</body>
</html>

Try with example


 

CSS Versions

  1. CSS1
  2. CSS2
  3. CSS3
  4. CSS4 

Version 4 comes with:- 

  • CSS-Pro 
  • CSS-Mobile

Supported Browser:

  • Google Chrome
  • Microsoft Edge 
  • Firefox
  • Opera Safari


CSS Full Form

CSS is the abbreviation of Cascading Style Sheet. This style sheet language consists of rules that shape the HTML elements that will be displayed in the browsers as a webpage. The website created only using HTML and without using the CSS looks dull. This is because CSS gives the outer cover to any HTML elements. CSS's Internet media type (MIME type) is text/CSS. World Wide Web Consortium (W3C) developed Cascading Style Sheet in 1996. There are multiple ways in which CSS can be applied to HTML.

 

  • The inline CSS style that will look like below code:
<h1 style="color: green;">Welcome to CSS tutorial</h1>

Try with example

 

  • The internal CSS style that will look like below code:
<!DOCTYPE html>
<html>
  <head>
    <title> internal CSS </title>
    <style>
     h1 {
        color: green;
     } 
    </style>
  </head>
  <body>
    <h1>Welcome to CSS tutorial</h1>
  </body>
</html>

Try with example

 

  • The external CSS style that will look like below code:
/* this will be separate file */
<style>
h1 {
   color: green;
}
</style>
  • CSS version release year:

 

 

Characteristics of CSS: 

  • Maintenance: CSS simplifies maintaining a website because all codes are placed in one single file. Any change made in style is automatically updated in all elements across web pages, making it very easy to maintain.
  • Time-saving: It simplifies page making and minimises the time spent as a single script can be used on multiple HTML pages. 
  • Support: All browsers and search engines support CSS.
  • Cache storing: With the help of an offline cache, a CSS can store web applications locally. This lets you view websites when offline.
  • Native front-end: CSS includes a vast list of attributes and functions helpful in designing the HTML page. 
  • Selectors: CSS includes many selectors, such as ID selectors, Class Selectors, etc., that help perform specific tasks.

Advantages of CSS:

  • Website maintenance is much easy, simple, and fast with CSS. 
  • CSS helps in making spontaneous and consistent changes. 
  • CSS improves the page loading speed of a website and makes the search engine capabilities better crawl the web pages. 
  • CSS allows you to define the position of HTML elements and gives the developer the ability to re-position HTML elements to align with the page's aesthetic appeal.

Disadvantages of CSS:

  • Cross-browser issues occur in CSS. The CSS works differently on different browsers, so any change may look perfect on chrome but different on other browsers. It requires you to add the script for that browser also. 
  • CSS doesn't have built-in security. Therefore, there is a lack of security. 
  • The open text-based system of CSS makes it more vulnerable to attacks. 
  • There are fragmentation issues in CSS.

 

What are the ways to include a CSS file? Why use @import?

There are several ways to include a CSS property in the HTML page. The information in the style sheet is used to format HTML documents.

The multiple ways to include CSS files are listed below:

External style sheet (Using HTML <link> Tag): External CSS style sheet defines the style for different HTML pages. It contains a separate CSS file with only style property with the assistance of tag attributes (For example, class, id, heading, … etc.). A separate file with a .css extension is used to write a CSS property, and it must be linked to the HTML document using the link tag. Style can be set only once for each element and is applied across web pages. The external style sheet can be linked with the HTML webpage using a link tag. 

Example:

<link rel="stylesheet" href="style.css">

External style sheet (Using the @import At-Rule): You must include the at-rule method either within the <style> tag or inside the style sheet.

<style>
@import url(style.css);
</style>

Internal style sheet (Using the <style> Element): Internal style sheets are used when you must uniquely style a single HTML document. The head section within the HTML file should have the CSS rule set, i.e., the CSS is embedded within the HTML file.

<style>
element {
    // CSS property
}
</style>

Inline Style: Inline CSS means the CSS property is added in the body section attached with an element. This style is specified within an HTML tag using the style attribute. It enables the application of a unique style for a single element.

<h1 style="style property">Welcome to CSS tutorial</h1>

Best Approach: External Style Sheet (using HTML <link> Tag) is an excellent way to link the element. It is very easy and efficient to maintain and re-use the CSS file across different pages. The placement of the <link> tag is done in the HTML <head> element. For specifying a media type=" text/css" for a CSS <type> attribute, for ignoring style sheet types that are not supported in a browser.

 Example 1: The file given below contains CSS property. This file save with .css extension. For Ex: style.css

body {
    background-color:powderblue;
}
.main {
    text-align:center;   
}
.GFG {
    color:#009900;
    font-size:50px;
    font-weight:bold;
}
#style {
    font-style:bold;
    font-size:20px;
} 
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="style.css"/>
	</head>
	<body>
		<div class = "main">
		<div class ="GFG">Welcome to CSS tutorial</div>
		<div id ="style">A computer science portal for CSS</p>

		</div>
	</body>
</html>	

Try with example

 

Example 2: This example describes the Internal or Embedded CSS.

<!DOCTYPE html>
<html>
	<head>
		<title>Internal CSS</title>
		<style>
			.main {
				text-align:center;
			}
			.GFG {
				color:#009900;
				font-size:50px;
				font-weight:bold;
			}
			.style {
				font-style:bold;
				font-size:20px;
			}
		</style>
	</head>
	<body>
		<div class = "main">
		<div class ="GFG">Welcome to CSS tutorial</div>
		<div class ="style">A computer science portal for CSS</p>

		</div>
	</body>
</html>	

Try with example

 

Example 3: This example describes the inline CSS.

<!DOCTYPE html>
<html>
	<head>
		<title>Inline CSS</title>
	</head>
	
	<body>
		<p style = "color:#009900;
					font-size:50px;
					font-style:italic;
					text-align:center;">
		Welcome to CSS</p>

	</body>
</html>	

Try with example

 

@import rule: For importing one style sheet into another one, the @import rule is used. As this rule supports media queries, the user can also import the media-dependent style sheet. You must declare the @import rule at the top of the document after any @charset declaration. 

Characteristics of @import:

  • It is used for importing a style sheet into an HTML page or another style sheet.
  • It is also used to add media queries. Therefore import is media-dependent.
  • The @import rule should be declared at the top of the document.

Syntax :

@import url|string list-of-mediaqueries;


Property values: 

  • url|string: The location of the resource to be imported is represented by a URL or a string. The url may be relative or absolute. 
  • List-of-media queries: The list of media queries conditions the application of the CSS rules defined in the linked URL.

 

Example 1: Consider the two CSS files as shown below.

  • icss.css
@import url("i1css.css");
h1 {
    color: #00ff00;
}
  • i1css.css
h1 {
   text-decoration: underline;
   font-size:60px;
}

p {
   padding-left: 20px;
   font-size: 60px;
}

 

<!DOCTYPE html>
<html>
<head>
	<title>WebPage</title>
	<link href="icss.css" rel="stylesheet">
</head>

<body>
	<h1>CSS Tutorial</h1>
	
<p>A computer science portal for CSS</p>

</body>
</html>

Try with example

 

Example 2:

<!DOCTYPE html>
<html>
	
<head>
	<title>@import property</title>
	
	<style type="text/css">
		@import url(
"https://your-url.com/imp.css");
	</style>
</head>

<body>
	<div id = "CSS">
		
		<h1>Welcome to CSS</h1>
		
		<h2>External style sheet (Using @import At-rule)</h2>
	</div>
</body>

</html>

Try with example

 

Supported Browser: 

  • Google Chrome 1.0
  • Internet Explorer 5.5
  • Firefox 1.0
  • Opera 3.5
  • Safari 1.0
  • Next


-Advertisement-


DeveloperTutorial
[email protected] © 2022-2023 Developers Tutorial All rights reserved.

Follow Us

Facebook Twitter LinkedIn Printerest Reddit

Announcement

Its a big achivement for us, We make a partnership with TutorialWithExample.com for the better content of our users.

Still Need Help ?

Let us now about your issue and a Professional will reach you out.