Introduction


In this tutorials, you learn what Cascading Style Sheets (CSS) is and how to use it for various layout and style purposes. We recommend that you work through the chapter from beginning to end, because some exercises build on previous exercises.


What is CSS?


CSS has its own language, separate from HTML, but you wouldn’t use CSS without the HTML page. In other words, although HTML can stand on its own and present a page to a browser, CSS can’t. You wouldn’t write a CSS page. Rather, you write HTML and then use CSS to help style that page to get it to look like you want it to.


CSS stands for Cascading Style Sheets


CSS defines how HTML elements are to be displayed


CSS saves a lot of work


External Style Sheets are stored in CSS files


Some HTML elements have an impact on presentation. When the browser encounters a element, it will change the way the content is displayed.


The code element is one of them. When the browser encounters the code element, it displays the enclosed content using a fixed-width font.


You should use HTML elements to define the structure and use Cascading Style Sheets (CSS) to control the presentation.


CSS stands for Cascading Style Sheets. It defines how to display HTML elements.



syntax


A CSS rule set consists of a selector and a declaration block:


	      	<!DOCTYPE HTML> 
			<html> 
			    <head> 
			        <style> 
			        a {
			            background-color:grey; 
			            color:white 
			        } 
			        </style> 
			    </head> 
			    <body> 
			        <a href="http://studentsempire.com">Visit the website</a> 
			    </body> 
			</html>
	      

The code above set the background color and foreground color.


Styles are normally saved in external .css files.


comments


Comments are used to explain your code, and may help you when you edit the source code at a later date. Comments are ignored by browsers.


CSS comment starts with /* and ends with */. Comments can also span multiple lines:


	      	h1 {
			color: blue;
			/* This is a single-line comment */
			text-align: center;
			}
	      


Selectors


CSS selectors allow you to select and manipulate HTML elements.


CSS selectors are used to "find" (or select) HTML elements based on their id, class, type, attribute, and more.


The element Selector


The element selector selects elements based on the element name.


You can select all <h1> elements on a page like this: (all <h1> elements will be center-aligned, with a red text color)


	      	h1 {
			color: blue;
			/* This is a single-line comment */
			text-align: center;
			}
	      

The id Selector


The id selector uses the id attribute of an HTML element to select a specific element.


An id should be unique within a page, so the id selector is used if you want to select a single, unique element.


To select an element with a specific id, write a hash character, followed by the id of the element.


	      	#div {
			text-align: center;
			color: red;
			}
	      

Do NOT start an ID name with a number!


The class Selector


The class selector selects elements with a specific class attribute.


To select elements with a specific class, write a period character, followed by the name of the class:


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


You can also specify that only specific HTML elements should be affected by a class.


In the example below, all <h1> elements with class="center" will be center-aligned:


	      	h1.center {
			text-align: center;
			color: red;
			}
	      

Do NOT start a class name with a number!


Grouping Selectors


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

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

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

you can group the selectors, to minimize the code.


To group selectors, separate each selector with a comma.


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