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.
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;}
Example :
In the following example all p elements will be center-aligned, with a blue text color :
p {
color: blue;
text-align: center;
}
Output :
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;
}
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;
}
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>
4. THE ID SELECTOR :
5. THE CLASS SELECTORS :
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>
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>
Version 4 comes with:-
Supported Browser:
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.
<h1 style="color: green;">Welcome to CSS tutorial</h1>
<!DOCTYPE html>
<html>
<head>
<title> internal CSS </title>
<style>
h1 {
color: green;
}
</style>
</head>
<body>
<h1>Welcome to CSS tutorial</h1>
</body>
</html>
/* this will be separate file */
<style>
h1 {
color: green;
}
</style>
Characteristics of CSS:
Advantages of CSS:
Disadvantages of 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>
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>
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>
@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:
Syntax :
@import url|string list-of-mediaqueries;
Property values:
Example 1: Consider the two CSS files as shown below.
@import url("i1css.css");
h1 {
color: #00ff00;
}
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>
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>
Supported Browser: