Вы находитесь на странице: 1из 18

CSS INTRODUCTION

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

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.

CSS Syntax:

The selector points to the HTML element you want to style.

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:

Simple selectors (select elements based on name, id, class)

Simple selectors (select elements based on name, id, class)


Combinator selectors (select elements based on a specific relationship between them)

Attribute selectors (select elements based on an attribute or attribute.

The CSS element Selector:


The element selector selects HTML elements based on the element name.

Example:

Here, all <p> elements on the page will be center-aligned, with a red text
color:

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

The CSS id Selector:


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

The id of an element is unique within a page, so the id selector is used to


select one unique element!

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;
}

The CSS class Selector:


The class selector selects HTML elements with a specific class attribute.

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;
}

All CSS Simple Selectors:

Selector Example Example description

.class .intro Selects all elements with class="intro"

#id #firstname Selects the element with id="firstname"

* * Selects all elements

element p Selects all <p> elements


element,element,.. div, p Selects all <div> elements and all <p> elements

CSS Colors:
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA,
HSLA values.

CSS Background Color:


You can set the background color for HTML elements:

Hello World
Example:
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>

CSS Text Color:


You can set the color of text:

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>

CSS Border Color:


You can set the color of borders:

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.

CSS background properties:

 background-color
 background-image
 background-repeat
 background-attachment
 background-position

CSS background-color:

The background-color property specifies the background color of an element.

Example:

The background color of a page is set like this:

body {
background-color: lightblue;
}

CSS background-image:
The background-image property specifies an image to use as the background of
an element.

By default, the image is repeated so it covers the entire element.

Example:

The background image for a page can be set like this:

body {
background-image: url("paper.gif");
}

CSS background-position:
The background-position property is used to specify the position of the
background image.

Example

Position the background image in the top-right corner:

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.

CSS Border Style:


The border-style property specifies what kind of border to display.

The following values are allowed:

 dotted - Defines a dotted border


 dashed - Defines a dashed border
 solid - Defines a solid border
 double - Defines a double border
 groove - Defines a 3D grooved border. The effect depends on the
border-color value
 ridge - Defines a 3D ridged border. The effect depends on the border-
color value
 inset - Defines a 3D inset border. The effect depends on the border-
color value
 outset - Defines a 3D outset border. The effect depends on the border-
color value
 none - Defines no border
 hidden - Defines a hidden border

Example
Demonstration of the different border styles:

p.dotted {border-style: dotted;}


p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}

CSS Border Width:


The border-width property specifies the width of the four borders.

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;
}

CSS Border Color:


The border-color property is used to set the color of the four borders.

The color can be set by:

 name - specify a color name, like "red"


 Hex - specify a hex value, like "#ff0000"
 RGB - specify a RGB value, like "rgb(255,0,0)"
 transparent

The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).

If border-color is not set, it inherits the color of the element.

Example:
p.one {
border-style: solid;
border-color: red;
}

CSS Margins:
All CSS Margin Properties:

Property Description

margin A shorthand property for setting the margin


properties in one declaration
margin-bottom Sets the bottom margin of an element

margin-left Sets the left margin of an element

margin-right Sets the right margin of an element

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 - Individual Sides


CSS has properties for specifying the margin for each side of an element:

 margin-top
 margin-right
 margin-bottom
 margin-left

All the margin properties can have the following values:

 auto - the browser calculates the margin


 length - specifies a margin in px, pt, cm, etc.
 % - specifies a margin in % of the width of the containing element
 inherit - specifies that the margin should be inherited from the parent
element

Tip: Negative values are allowed.


Example
Set different margins for all four sides of a <p> element:

p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}

The inherit Value:


This example lets the left margin of the <p class="ex1"> element be
inherited from the parent element (<div>):

Example:

Use of the inherit value:

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!

Look at the following example:

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).

Padding - Individual Sides:

CSS has properties for specifying the padding for each side of an element:

 padding-top
 padding-right
 padding-bottom
 padding-left

All the padding properties can have the following values:

 length - specifies a padding in px, pt, cm, etc.


 % - specifies a padding in % of the width of the containing element
 inherit - specifies that the padding should be inherited from the parent
element

Example
Set different padding for all four sides of a <div> element:

div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}

Padding and Element Width:


The CSS width property specifies the width of the element's content area. The content area
is the portion inside the padding, border, and margin of an element (the box model).
So, if an element has a specified width, the padding added to that element will be added to
the total width of the element. This is often an undesirable result.

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;
}

CSS Box Model:


The CSS Box Model:
All HTML elements can be considered as boxes. In CSS, the term "box
model" is used when talking about design and layout.

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;
}

Width and Height of an Element:


In order to set the width and height of an element correctly in all browsers,
you need to know how the box model works.

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.

Q #6) What are the uses of an embedded style sheet?


Answer: Embedded style sheet gives us the privilege to define styles in one place in an
HTML document.
We can generate multiple classes using an embedded style sheet to use on multiple tag
types of a web page and also there is no extra downloading required for importing the
information.

Example:
<!DOCTYPE html>

<html>
<head>

<style type="text/css">

p{

font-family: georgia, serif;

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

and also there is no extra downloading required importing the information.


</p>

</body>

</html>

Q #7) How to use CSS selector?

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.

Q #8) Explain the concept of Tweening.


Answer: Tweening is the process in which we create intermediate frames between two
images to get the appearance of the first image which develops into the second image.
It is mainly used for creating animation.

Q #9) Define CSS image scripts.


Answer: CSS image scripts are a group of images that are placed into one image.
It reduces the load time and request number to the server while projecting multiple
images into a single web page.

Q #10) Explain the term Responsive web design.


Answer: It is a method in which we design and develop a web page according to the
user activities and conditions which are based on various components like the size of
the screen, portability of the web page on the different devices, etc.
Hence it is done by using different flexible layouts and grids.

Q #11) What are CSS counters?


Answer: CSS counters are variables that can be incremented by rules of CSS that
inspector track how many times the variable has been used.
Q #12) What is CSS specificity?
Answer: CSS specificity is a score or rank that decides which style declaration has to be
used to an element.
(*) this universal selector has low specificity while ID selectors have high specificity.

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>

Q #14) What is the difference between padding and margin?


Answer: In CSS, the margin is the property by which we can create space around
elements. We can even create space to the exterior defined borders.
In CSS we have margin property as follows:
 margin-top
 margin-right
 margin-bottom
 Margin-left
Margin property has some defined values as shown below.
 Auto – using this property browser calculates the margin.
 Length – It sets the margin values in px,pt,cm etc.
 % – It sets the width % of the element.
 Inherit – By this property we can inherit the margin property from the parent
element.
In CSS, padding is the property by which we can generate space around an element’s
content as well as inside any known border.

CSS padding also has properties like,


1. Padding-top
2. Padding-right
3. Padding-bottom
4. Padding-left
Negative values are not allowed in padding.

div {

padding-top: 60px;
padding-right: 40px;

padding-bottom: 50px;

padding-left: 70px;

Q #15) What is the use of the Box Model in CSS?


Answer: In CSS, the box model is a box that binds all the HTML elements and it
includes features like margins, border, padding, and the actual content.
By using a box model we will get the authority to add the borders all around the
elements and we can also define the space between the elements.

Вам также может понравиться