Css
Css
What is CSS?
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 style sheets are stored in CSS files.
Advantages of CSS
1. CSS saves time − You can write CSS once and then reuse same sheet in multiple
HTML pages.
2. Pages load faster − If you are using CSS, you do not need to write HTML tag
attributes every time. Just write one CSS rule of a tag and apply it to all the
occurrences of that tag. So less code means faster download times.
3. Easy maintenance − To make a global change, simply change the style, and all
elements in all the web pages will be updated automatically.
4. Superior styles to HTML − CSS has a much wider array of attributes
than HTML, so you can give a far better look to your HTML page in
comparison to HTML attributes.
p {
text-align: center;
color: red;
}
2.The id Selector
The id selector uses the id attribute of an HTML element to select a specific
element.
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
The style rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
3. 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.
In the example below, all HTML elements with class="center" will be red
and center-aligned:
.center {
text-align: center;
color: red;
}
4. Grouping Selectors
If you have elements with the same style definitions,
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
In the example below we have grouped the selectors from the
code below:
h1 {
text-align: center;
color: red; h1, h2, p {
} text-align: center;
h2 { color: red;
text-align: center; }
color: red;
}
p {
text-align: center;
color: red;
}
How to add CSS
CSS is added to HTML pages to format the document according to
information in the style sheet. There are three ways to insert CSS in HTML
documents.
1. Inline CSS
2. Internal CSS
3. External CSS
1.Inline Styles
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The
style attribute can contain any CSS property.
The example below shows how to change the color and the left margin of a
<h1> element:
<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
2.External Style Sheet
Each page must include a reference to the external style sheet file inside the
<link> element.
The <link> element goes inside the <head> section:
An external style sheet can be written in any text editor. The file should not
contain any html tags. The style sheet file must be saved with a .css
extension.
CSS File:
Html File:
body {
background-color: lightgreen;
<head> }
<link rel="stylesheet" type="text/css" h1 {
href="mystyle.css"> color: navy;
</head> margin-left: 20px;
}
3.Internal Style Sheet
An internal style sheet may be used if one single page has a unique style.
Internal styles are defined within the <style> element,
inside the <head> section of an HTML page:
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
CSS Selector Reference
Selector Example Example description
element,element div, p Selects all <div> elements and all <p> elements
element>element div > p Selects all <p> elements where the parent is a <div>
element
element+element div + p Selects all <p> elements that are placed
immediately after <div> elements
element1~element2 p ~ ul Selects every <ul> element that are preceded by a <p> element
[attribute~=value] [title~=flower] Selects all elements with a title attribute containing the word
"flower"
[attribute|=value] [lang|=en] Selects all elements with a lang attribute value starting with
"en"
[attribute^=value] a[href^="https"] Selects every <a> element whose href attribute value begins
with "https"
[attribute$=value] a[href$=".pdf"] Selects every <a> element whose href attribute value ends
with ".pdf"
[attribute*=value] a[href*="w3school Selects every <a> element whose href attribute value contains
s"] the substring "w3schools"
CSS Properties
1.CSS Background
CSS background property is used to define the background effects on
element.
There are 5 CSS background properties that affects the HTML
elements:
a) background-color
b) background-image
c) background-repeat
d) background-attachment
e) background-position
1.Background Color
The background-color property specifies the background color of an element.
The background color of a page is set like this:
Body
{
background-color: lightblue;
}
body {
background-image: url(“flowers.gif");
}
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
Background Image - Set position and no-repeat
Background-attachment Property
A background-image that will not scroll with the page (fixed)
Background - Shorthand property
To shorten the code, it is also possible to specify all the background properties in
one single property. This is called a shorthand property.
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
CSS Properties
1. CSS Borders
The CSS border is a shorthand property used to set the border on an
element.
The CSS border properties are use to specify the style, color and size
of the border of an element. The CSS border properties are given
below
a) border-style
b) border-color
c) border-width
d) border-radius
CSS border-style
The Border style property is used to specify the border type which you
want to display on the web page.
There are some border style values which are used with border-style
property to define a border.
CSS border-width
The border-width property is used to set the border's width. It is set in
pixels. You can also use the one of the three pre-defined values, thin,
medium or thick to set the width of the border.
Note: The border-width property is not used alone. It is always used with other
border properties like "border-style" property to set the border first otherwise it
will not work.
p{
border-style: solid;
border-width: 5px;
}
CSS Border Radius
The border-radius property is used to add rounded borders to an
element
p {
border: 2px solid red;
border-radius: 5px;
}
CSS border-color
There are three methods to set the color of the border.
• Name: It specifies the color name. For example: "red".
• RGB: It specifies the RGB value of the color. For example: "rgb(255,0,0)".
• Hex: It specifies the hex value of the color. For example: "#ff0000".
There is also a border color named "transparent". If the border color is not set it is
inherited from the color property of the element.
Note: The border-color property is not used alone. It is always used with other
border properties like "border-style" property to set the border first otherwise it
will not work.
p { p {
border-style: solid; border-style: solid;
border-color: red; border-color: #98bf21;
} }
CSS Border Radius
The border-radius property is used to add rounded borders to an element
p {
border: 2px solid red;
border-radius: 5px;
}
CSS Margins
CSS Margin property is used to define the space around elements. It is
completely transparent and doesn't have any background color. It clears an area
around the element.
Top, bottom, left and right margin can be changed independently using separate
properties. You can also change all properties at once by using shorthand margin
property.
There are following CSS margin properties:
Property Description
Value Description
length It is used to specify a margin pt, px, cm, etc. its default value is 0px.
Value Description
p {
font-family: "Times New Roman", Times, serif;
}
Font Style
The font-style property is mostly used to specify italic text.
This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
CSS Font Size
CSS font size property is used to change the size of the font.
These are the possible values that can be used to set the font size:
h1 {
font-size: 40px;
} p.normal {
font-variant: normal;
h2 { }
font-size: 50%;
} p.small {
font-variant: small-caps;
p { }
font-size: 14px;
}
p.normal {
CSS font-weight
font-weight: normal;
}
p.thick {
font-weight: bold;
}
CSS Overflow
The overflow property specifies whether to clip the content or to add scrollbars
when the content of an element is too big to fit in the specified area.
visible - Default. The overflow is not clipped. The content renders outside the
element's box
hidden - The overflow is clipped, and the rest of the content will be invisible
scroll - The overflow is clipped, and a scrollbar is added to see the rest of the
content
auto - Similar to scroll, but it adds scrollbars only when necessary
CSS float Property
The float property is used for positioning and formatting content e.g. let an image float
left to the text in a container.
The animation could be bound to the selector by specifying at least these two
properties:
The name of the animation
The duration of the animation
CSS animation properties
Property Description
@keyframes It is used to specify the animation.
animation This is a shorthand property, used for setting all the properties,
except the animation-play-state and the animation-fill- mode
property.
animation-fill-mode it specifies the static style of the element. (when the animation is
not playing)
In the example above we have specified when the style will change by using the
keywords "from" and "to" (which represents 0% (start) and 100% (complete)).
CSS Transition
The CSS transitions are effects that are added to change the element
gradually from one style to another, without using flash or JavaScript.
Note. The transition effect is started when you move cursor over an
element. : If you don't specify the duration part, the transition will have no
effect because its default value is 0
Note: The transition property is not supported by IE9 and earlier version.
CSS - Pseudo Classes
CSS pseudo-classes are used to add special effects to some selectors. You do
not need to use JavaScript or any other script to use those effects.
2 :visited
Use this class to add special style to a visited link.
3 :hover
Use this class to add special style to an element when you mouse over it.
4 :active
Use this class to add special style to an active element.
5 :focus
Use this class to add special style to an element while the element has focus.
6 :first-child
Use this class to add special style to an element that is the first child of some other element.
7 :lang
Use this class to specify a language to use in a specified element.
Selector Example Example description
:active a:active Selects the active link
:checked input:checked Selects every checked <input> element
:disabled input:disabled Selects every disabled <input> element
:empty p:empty Selects every <p> element that has no children
:enabled input:enabled Selects every enabled <input> element
:first-child p:first-child Selects every <p> elements that is the first child of
its parent
:first-of-type p:first-of-type Selects every <p> element that is the first <p>
element of its parent
</head>
<body>
<a href = "">Click This Link</a>
</body>
</html>
CSS - Pseudo Elements
CSS pseudo-elements are used to add special effects to some selectors.
You do not need to use JavaScript or any other script to use those
effects.
2 :first-letter
Use this element to add special style to the first letter of the text in a selector.
3 :before
Use this element to insert some content before an element.
4 :after
Use this element to insert some content after an element.
CSS Tables
We can apply style on HTML tables for better look and feel. There are some CSS
properties that are widely used in designing table using CSS:
border
border-collapse-we can collapse all borders in one border only.
Padding-specify padding for table header and table data using the CSS
padding property.
Width
Height
text-align
color
background-color
• Text shadow
• Box Shadow
The CSS text-shadow property applies shadow to text.
In its simplest use, you only specify the horizontal shadow (2px) and the vertical
shadow (2px):
<style>
h1 {
text-shadow: 2px 2px #ffffff;
}
</style>
CSS box-shadow Property
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: yellow;
box-shadow: 10px 10px grey ;
}
</style>
CSS Buttons
Examples: You could have one set of style rules for computer screens, one for printers,
one for handheld devices, one for television-type devices, and so on.
Unfortunately these media types never got a lot of support by devices, other than the
print media type.
Media queries can be used to check many things, such as:
• width and height of the viewport
• width and height of the device
• orientation (is the tablet/phone in landscape or portrait mode)
• resolution
Using media queries are a popular technique for delivering a tailored style sheet to
desktops, laptops, tablets, and mobile phones (such as iPhone and Android phones).
Media Query Syntax
@media not|only mediatype and (expressions)
{
CSS-Code;
}
The result of the query is true if the specified media type matches the type of device
the document is being displayed on and all expressions in the media query are true.
When a media query is true, the corresponding style sheet or style rules are applied,
following the normal cascading rules.