Академический Документы
Профессиональный Документы
Культура Документы
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
CSS is used to define styles for your web pages, including the design,
layout and variations in display for different devices and screen sizes.
CSS Syntax:
Example:
In this example all <p> elements will be center-aligned, with a red text
color:
p {
color: red;
text-align: center;
}
CSS Selectors:
CSS selectors are used to "find" (or select) the HTML elements you want to
style.
We can divide CSS selectors into three categories:
Example:
Here, all <p> elements on the page will be center-aligned, with a red text
color:
p {
text-align: center;
color: red;
}
To select an element with a specific id, write a hash (#) character, followed
by the id of the element.
Example:
The CSS rule below will be applied to the HTML element with id=”krishna":
#krishna {
text-align: center;
color: red;
}
To select elements with a specific class, write a period (.) character, followed
by the class name.
Example:
In this example all HTML elements with class="center" will be red and
center-aligned:
.center {
text-align: center;
color: red;
}
CSS Colors:
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA,
HSLA values.
Hello World
Example:
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Hello World
Example:
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
CSS Color Values:
In CSS, colors can also be specified using RGB values, HEX values,
HSL values, RGBA values, and HSLA values:
Example:
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.5);">...</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">...</h1>
CSS Backgrounds:
The CSS background properties are used to define the background effects for
elements.
background-color
background-image
background-repeat
background-attachment
background-position
CSS background-color:
Example:
body {
background-color: lightblue;
}
CSS background-image:
The background-image property specifies an image to use as the background of
an element.
Example:
body {
background-image: url("paper.gif");
}
CSS background-position:
The background-position property is used to specify the position of the
background image.
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
CSS Borders:
CSS Border Properties:
The CSS border properties allow you to specify the style, width, and color of
an element's border.
Example
Demonstration of the different border styles:
The width can be set as a specific size (in px, pt, cm, em, etc) or by using
one of the three pre-defined values: thin, medium, or thick.
The border-width property can have from one to four values (for the top
border, right border, bottom border, and the left border).
Example:
p.krishna {
border-style: solid;
border-width: 2px 10px 4px 20px;
}
The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).
Example:
p.one {
border-style: solid;
border-color: red;
}
CSS Margins:
All CSS Margin Properties:
Property Description
CSS Margins
The CSS margin properties are used to create space around elements, outside
of any defined borders.
With CSS, you have full control over the margins. There are properties for
setting the margin for each side of an element (top, right, bottom, and left).
margin-top
margin-right
margin-bottom
margin-left
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Example:
div {
border: 1px solid red;
margin-left: 100px;
}
p.ex1 {
margin-left: inherit;
}
Margin Collapse:
Top and bottom margins of elements are sometimes collapsed into a single
margin that is equal to the largest of the two margins.
This does not happen on left and right margins! Only top and bottom
margins!
Example
Demonstration of margin collapse:
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
CSS Padding:
The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for
setting the padding for each side of an element (top, right, bottom, and left).
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
Example
Here, the <div> element is given a width of 300px. However, the actual width of the <div>
element will be 350px (300px + 25px of left padding + 25px of right padding):
div {
width: 300px;
padding: 25px;
}
The CSS box model is essentially a box that wraps around every HTML
element. It consists of: margins, borders, padding, and the actual content.
The image below illustrates the box model: Explanation of the different
parts:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is
transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define
space between elements.
Example
Demonstration of the box model:
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Example
This <div> element will have a total width of 350px:
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
#inerview questions:?
Q #1) What is CSS?
Answer: CSS outline the style of an HTML webpage, it is a language by which we can
set the behavior of an HTML webpage. It describes how the HTML content will be shown
on screen.
CSS controls the layout of several HTML web pages. CSS is referred to as the
Cascading Style Sheet.
Q #2) Name all the modules which are used in the current version of CSS.
Answer: There are several modules in CSS as stated below:
Selectors
Box Model
Backgrounds and Borders
Text Effects
2D/3D Transformations
Animations
Multiple Column Layout
User Interface.
Q #3) Distinguish between CSS2 and CSS3.
Answer:
There are several differences between CSS2 and CSS3.
CSS3 is divided into two various sections which are called a module. Whereas in
CSS2 everything accedes into a single document with all the information in it.
CSS3 modules are supported almost on every browser and on the other hand
modules of CSS and CSS2 are not supported in every browser.
In CSS3 we will find that many graphics related characteristics have been
introduced like “Border-radius or box-shadow, flexbox.
In CSS3, a user can precise multiple background images on a webpage by using
properties like background-image, background-position, and background-repeat
styles.
Q #4) Cite different types of CSS.
Answer: There are three types of CSS as mentioned below.
External – These are written in separate files.
Internal – These are cited at the top of the web page code document.
Inline – These are written right next to the text.
Q #5) Why is the external style sheet useful?
Answer: External style sheet is very useful as we write all the styling codes in a single
file and it can be used anywhere by just referencing the link of that external style sheet
file.
So if we do any changes in that external file, then the changes can also be observed on
the webpage. So we can say that it is very useful and it makes your work easy while
working on larger files.
Example:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
p{
font-size: x-large;
color:#ff9900;
a:hover {
color: LimeGreen;
text-decoration: none;
</style>
</head>
<body>
<p>Embedded style sheet gives us the privilege to define styles at one place in a HTML
document.
We can generate multiple classes using embedded style sheet to use on multiple tag types a web
page
</body>
</html>
Answer: By using the CSS selector, we can choose the content which we want to style
so that we can say that it is a bridge between the style sheet and the HTML files.
The syntax for CSS selector is “select” HTML elements created on their id, class, type,
etc.
There are four categories in CSS which authorized the specificity level of the
selector.
Inline style
IDs
Classes, Attributes, and pseudo-classes.
Elements and pseudo-elements.
Q #13) How can we calculate specificity?
Answer: To calculate specificity we will start with 0, then we have to add 1000 for each
ID and we have to add 10 to the attributes, classes or pseudo-classes with each element
name or pseudo-element and later we have to add 1 to them.
Example:
h2
#content h2
<div id=”content”>
<h2 style=”color:#FF0000”>heading</h2>
</div>
div {
padding-top: 60px;
padding-right: 40px;
padding-bottom: 50px;
padding-left: 70px;