CSS Basics
CSS Basics
Beginners
What is CSS?
CSS stands for Cascading Style Sheets. It is the language for describing the
presentation of Web pages, including colours, layout, and fonts, thus making our web
pages presentable to the users.
CSS is designed to make style sheets for the web. It is independent of HTML and can
be used with any XML-based markup language. Now let’s try to break the acronym:
CSS History
CSS Editors
Some of the popular editors that are best suited to wire CSS code are as following:
1. Atom
2. Visual Studio Code
3. Brackets
4. Espresso(For Mac OS User)
5. Notepad++(Great for HTML & CSS)
6. Komodo Edit (Simple)
7. Sublime Text (Best Editor)
CSS Syntax
Selector {
Property 1 : value;
Property 2 : value;
Property 3 : value;
}
For example:
h1
{
Color: red;
Text-align: center;
}
#unique
{
color: green;
}
CSS Comment
/*
h1
{
color: red;
text-align: center;
}
*/
CSS How-To
Inline CSS
● With the help of style tag, we can apply styles within the HTML file
● Redundancy is removed
● But the idea of separation of concerns still lost
● Uniquely applied on a single document
● Example:
< style>
h1{
color:red;
}
</style>
External CSS
● With the help of <link> tag in the head tag, we can apply styles
● Reference is added
● File saved with .css extension
● Redundancy is removed
● The idea of separation of concerns is maintained
● Uniquely applied to each document
● Example:
<head>
<link rel="stylesheet" type="text/css" href="name of the Css file">
</head>
h1{
color:red; //.css file
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
<link rel="stylesheet" type="text/css" href="first.css">
<style>
h1
{
color:green;
}
</style>
</head>
<body>
</body>
</html>
#center {
text-align: center;
color:pink;
}
Output:
CSS Selectors
Id > Class>Element
Element Selector
h1
{
Color: red;
We selected the heading tag and then changed the color property i.e text color to red.
Now whatever is written in this tag (content) will have the text color as red.
ID Selector
#unique
{
Color: red;
}
We selected the id and then changed the color property i.e text color to red. Now
whatever is written in this tag (content) will have the text color as red
Class Selector
group
{
Color: red;
}
<h1 class=”group”> Hi </p>
We selected the class and then changed the color property i.e text color to red. Now
whatever is written in this tag (content) will have the text color as red
<!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>
</body>
</html>
Output:
Now that we have seen all the three selectors now let’s see how style falls or cascades.
We will implement one program where we add style on the same element by using a
tag, id’s, and classes as selectors. The objective of this is to show how one style cuts
the other style that might also be referred to as Priority. We will see that id have the
highest priority over tags and classes.
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
<style>
p{
color:red;
}
.class {
color:green;
}
#id{
color:orange;
}
</style>
</head>
<body>
</body>
</html>
If you have observed how one style is fighting against another in order to style the
element. Here the race was won by id, what if all the selectors are classes or tags then
the one which is closer or applied at the end will win the race and what if a class and tag
selectors are used on the same element, in that case, the race will be won by the class
selector.
CSS Colors
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
<link rel="stylesheet" type="text/css" href="first.css">
<style>
#center{ color:#ff0099;}
h1{ color:rgba(255,0,0,0.5);}
</style>
</head>
<body>
<h1>This heading will be green</h1>
<p style="color:rgb(255,0,0)">This paragraph will be red</p>
<p id="center">This paragraph will be pink and
center-aligned</p>
</body>
</html>
CSS Background
● There are different ways by which CSS can have an effect on HTML elements
● Few of them are as follows:
● Color – used to set the color of the background
● Repeat – used to determine if the image has to repeat or not and if
it is repeating then how it should do that
● Image – used to set an image as the background
● Position – used to determine the position of the image
● Attachment – It basically helps in controlling the mechanism of
scrolling
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
<link rel="stylesheet" type="text/css" href="first.css">
<style>
html{
background: #ff9900;
}
p{
background:
url("https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRT8t-o6o
UJ-E9YRhimOvTU2TSH7vlBnRWBN554_rX30dZah466&usqp=CAU");
background-position:center;
background-repeat:no-repeat;
width: 100%;
height: 600px;
}
</style>
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non
provident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</p>
</body>
</html>
CSS Border
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
<link rel="stylesheet" type="text/css" href="first.css">
<style>
p{
border-style: solid;
border-color: blue;
border-width: 2px 5px;
border-radius: 10%;
}
</style>
</head>
<body>
</body>
</html>
CSS BoxModel
When you go in chrome and right-click, go on inspect. It will give all the elements that
are used in the HTML file. In the right-hand side, there will be a box model like:
The most inner rectangle represents our content, the padding is nothing but the space
between the content and the border then the margin is the area outside the border.
p{
border: 2px solid blue;
margin: 5px;
padding: 20px;
content: 50px;
}
</style>
</head>
<body>