Unit 2 - CSS: Cascading Style Sheet: PSIT-Pranveer Singh Institute of Technology
Unit 2 - CSS: Cascading Style Sheet: PSIT-Pranveer Singh Institute of Technology
1. REQUIREMENT OF CSS
HTML itself is not able to give style to the website elements. CSS extends HTML capabilities and provides many
properties to give the required style to the website. Here are some of the key advantages of learning CSS:
a) Create Beautiful Websites: Behind every stunning website, there is a CSS Style Sheet. It handles the
look and feel of a website. The beautiful designs and the attractive features of CSS make it worthy of
your time. It is a must-have skill of a web designer.
b) Solve Big Problems: Before CSS, all attributes like color, element alignments, border, and size had to
be repeated on every web page to style any element. It was a very long process. For example: If you are
developing a large website where fonts and color information has to be added on every single page, it
will become a long and expensive process. CSS was created to solve this problem. It was a W3C
recommendation.
c) Presentation capabilities: CSS provides powerful control over the presentation of an HTML document.
It is related to the previous point as CSS gets combined with markup languages like HTML to enhance
their capabilities and to provide better control to the user of the website.
d) Increase your skills: HTML and CSS are the most basic languages which a web designer should learn
as it opens the door to various other technologies like JavaScript, Angular, PHP, etc. These technologies
become easier to understand, once a person gets exposed to CSS and HTML.
e) Better styling capabilities: It has a much wider variety of attributes as compared to HTML which has a
limited set of attributes with limited capabilities.
2. APPLICATIONS OF CSS
a) CSS saves time: It saves a lot of time as only one Style Sheet can be used on multiple pages, there is no
need to write same code in all the pages. A single file can be used in all those pages where same styling
of webpages is required. This also decreases the page loading time as file transfer size gets reduced
because of reusing of code.
b) Multiple Device Compatibility: CSS can optimize the same webpage to adapt different
viewports(screens). It makes a webpage compatible and readable in all the devices like laptops, tablets,
mobiles, etc. which have different screen sizes.
c) Improves HTML Functionality: CSS has a large number of attributes as compared to Html which
only provides a limited number of styling attributes.
d) Pages load faster: When we use the same Style Sheet in multiple webpages then, loading of the pages
gets faster because of reusing of the code.
e) Responsive layout: It can optimize a webpage to fit varies screen sizes very easily. Different devices
have different screen sizes and CSS features allows the programmer to make their website responsive
to these devices. The responsive nature of CSS makes a webpage compatible and readable in different
screen sizes with ease.
f) Global web standards: The web standards are now depreciating the use of HTML attributes and it is
being recommended to use CSS.
Explanation-
In the example above the <style> tag is used to add CSS code within the webpage. The selectors for a particular
tag are used to stylize it. For example, h1 { } and p { } selectors are used in the example. Let’s see the attribute
used inside them:
• Color: Used to specify the text color of the heading and the paragraph.
• Background-color: Used to define the background color of the heading.
• Padding: Specified the padding.
3. CSS SYNTAX
A CSS rule-set consists of a selector and a declaration block:
Selector: A Selector is any HTML element we want to style, like <p>, <div> etc.
Declaration Block: This block contains the different properties and their values which we want to change in the
selector element. Example: -
a) color:yellow;
b) font-face:arial;
Property: A Property is kind of a quality of any HTML element like color, border, font-size etc. which will
change the appearance and style of it.
Value: Every CSS property has a value which will give different results for a particular property. In the above
example, value yellow is assigned to color property.
4. INCORPORATING CSS
It is an important feature of CSS that it can be added by various ways in a webpage.
Use Description
Inline CSS Using the style attribute in the HTML start tag.
Embedded CSS Using the style Using the <style> tag within the code of the webpage.
External CSS Using the <link> tag to link an external CSS file in the webpage.
5. CSS SELECTORS
CSS selectors are used to select any element and then apply CSS on it. These selectors are very useful in writing
a clean and effective code.
CSS Selectors are used in internal and external CSS. Although inline CSS is also an option but it is never
recommended to use. There are various types of selector through which you can select an element. These selectors
can be combined together to select a more specific element also.
A CSS Selector can select an element by its id, class, type, attribute, tag, etc.
Syntax
tag-selector{
CSS Styling
}
Example-
p{
color:red;
}
Explanation: In the syntax example, we have used the <p> tag as a tag selector. Now all the paragraphs in the
webpage will have red text color. Remember that tags < > will be omitted when using tag selectors. Only tag
name will be used.
Syntax
Note: When using ID Selectors, the hash(#) symbol is used in front of ID name.
#ID_Name{
CSS Styling
}
Example-
#demo-id{
color:red;
}
Explanation: In the example above, the ID #demo-id is used as a selector. Now, wherever this ID is applied,
whether in a paragraph, heading, div, or anything, the text color will be red inside that element.
To define a class for an element, just use the class attribute. You can write anything as an class name but it should
start with an alphabet or an underscore(_).
Syntax
Note: When using Class Selector, the dot(.) symbol is used in front of class name.
.Class_Name{
Css Styling
}
Example-
.demo-class{
color:red;
}
Explanation: Any element which will use the demo-class as class name will display text color as red. It can be
applied to more than one elements.
Syntax
tag.class-name{
Css Styling
}
Example-
p.demo-class{
color:red;
}
Suppose, there is a div, a heading and a paragraph and in all of these we want the font color to be red. Instead of
declaring same color property 3 times for all 3 elements we can just declare it once using a comma(,).
It is not limited to tags only, multiple tags, IDs, or Classes can be given similar CSS by using the comma(,).
Syntax
p, h2, div{
color:red;
}
p, .demo-class, h2{
color:red;
}
Explanation: Notice in the above example, how the comma is used to create a grouping selector. We have also
combined the tags with a class. Now, all the paragraphs, h2 headings, div or elements with. demo-class will display
red color font.
Syntax
*{
Css Styling
}
Example-
*{
margin:0;
padding:0;
}
a. background-color
b. background-image
c. background-repeat
d. background-attachment
e. background-position
Background color
The background-color property specifies the background color of an element.
Background Image
The background-image property sets an image in the background of an element.
CSS Font property is way smarter than HTML Font Tag. HTML font Tag was used till HTML 4, but in HTML
5, it was deprecated. You can no longer use it, but it was an inferior option compared to the CSS Font property.
CSS Fonts has multiple options to give style to the fonts. You can alter the color, size, weight, and family of the
fonts. You can assign various font families at once as a replacement if one is not supported. Let's look at multiple
CSS Font properties and their uses:
Values Description
CSS Font family property let us assign multiple font families at once. It doesn't mean that all families will be
loaded and used together. Instead, the second one will be used if the browser does not support the first font, and
the third one will be used if the second font is not supported. It works in a particular order of priority.
To change the color of a particular section of the website like paragraph and headings, Class, ID, and tag selectors
will always help.
Font style property has three values. One is normal and the other two are slightly different versions of italic:
• font-style: normal;
• font-style: italic;
• font-style: oblique;
• font-variant: normal;
• font-variant: small-caps;
• font-weight: normal;
• font-weight: lighter;
• font-weight: bold;
• font-weight: 500;
Links can be styled with any CSS property (e.g. color, font-family, background, etc.). Links can be styled
differently depending on what state they are in.
Types Description
9. CSS TABLES
The CSS can also be applied to HTML tables. Below are some properties which can make an ordinary looking
table into an attractive one:
• border
• border-collapse
• padding
• width
• text-align
• color
• background color
According to the CSS box model, a browser engine represents every element in HTML as a box. This box is a
combination of the element's content, padding, margins and borders.
In the image above, the content is wrapped with padding. This padding area is then wrapped with borders and
then, outside the borders, there is margin. All these properties take space around the content, affecting the other
elements outside the box.
Changes in Content, padding and borders affects the size of the container, even when its size is fixed. The margins
on the other hand, clears the outside space for the container. All these properties together create a CSS Box.