What is CSS?
CSS Introduction: CSS stands for Cascading Style Sheets. CSS describes how HTML elements are to be displayed on screen, paper, or in other media. CSS saves a lot of work. It can control the layout of multiple web pages all at once .External stylesheets are stored in CSS files.
History of CSS?
- 1994: First Proposed by Hakon Wium Lie on 10th October
- 1996: CSS was published on 17th November with influencer Bert Bos
- Later he became co-author of CSS
- 1996: CSS became official with CSS was published in December
- 1997: Created CSS level 2 on 4th November
- 1998: Published on 12th May
HTML vs CSS
It’s not really “HTML vs CSS”. In fact HTML and CSS are the best of friends! CSS is a supplementary extension to HTML. Here’s what each does:
- HTML
- The purpose of HTML is to provide document structure and meaning. This is particularly true with the introduction of HTML5. You use HTML to specify what elements go on the page (eg, headings, paragraphs, tables, images, etc). Each of these elements (as well as their attributes and attribute values) have a certain meaning.
- CSS
- You use CSS to specify how those HTML elements look. But not just how they look, how they are presented. After all, you can use CSS to provide styles for speech output (for example, for users using a screen reader). So, you can write an HTML document without being concerned with its presentation, then use CSS to specify how it will be presented within any given context. Not only this, but you can change the CSS without having to change the HTML. In other words, you can “plug” a style sheet into an HTML document and the HTML document will immediately take on the styles of that style sheet.
CSS Comment
/* h1 { color: red; text-align: center; } */
CSS How-To
There are 3 ways to write CSS in our HTML file.
-
- Inline CSS
- Internal CSS
- External CSS
Implementation of all the three types of CSS:
<!DOCTYPE html> <html> <head> <title>HTML</title> <link rel="stylesheet" type="text/css" href="first.css"> <style> h1 { color:green; } </style> </head> <body> <h1>This heading will be green</h1> <p style="color:Red">This paragraph will be red</p> <p id="center">This paragraph will be pink and center-aligned</p> </body> </html>
CSS Selectors
The selector is used to target elements and apply CSS
- Three simple selectors
- Element Selector
- Id Selector
- Class Selector
Implementation of all the three selectors in CSS:
<!DOCTYPE html> <html> <head> <title>HTML</title> <link rel="stylesheet" type="text/css" href="first.css"> <style> #center1 { text-align: center; color:pink; } .center2 { text-align: center; color:red; } h1 { text-align:center; color:green; } </style> </head> <body> <h1>This heading will be green and center-aligned </h1> <p class = "center2">This paragraph will be red and center-aligned </p> <p id ="center1">This paragraph will be pink and center-aligned</p> </body> </html>
Why Use CSS?
CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.
body { background-color: lightblue; } h1 { color: white; text-align: center; } p { font-family: verdana; font-size: 20px; }
How to Solve CSS Big Problem?
HTML was NEVER intended to contain tags for formatting a web page!
HTML was created to describe the content of a web page, like:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes 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.
CSS removed the style formatting from the HTML page!
How to Save a Lot of Work with CSS!
The style definitions are normally saved in external .css files.
With an external stylesheet file, you can change the look of an entire website by changing just one file!
CSS Background Color Example
body { background-color: green; }
CSS Background Images Example
body { background-image: url("barn.jpg"); }
CSS Background Image – The Repeat Property
body { background-image: url("barn.jpg"); background-repeat: repeat-y; }
background-repeat property to “repeat-x”
body { background-image: url("barn.jpg"); background-repeat: repeat-x; }
background-repeat property to set an image to not repeat
body { background-image: url("barn.jpg"); background-repeat: no-repeat; }
Colors in CSS Example
Colors in CSS are used to colorize elements in our web-pages. There are many ways to assign colors to elements. You can use the actual names for the colors, their RGB values or the Hexadecimal values. In CSS3 the hsl (hue-saturation-lightness) has been added to the specification.
Color Names
Currently there are 140 color names supported in HTML, which can be assigned in CSS rules by just typing their name. For example:
Syntax
p { color: green; }