CSS Document
CSS Document
Sheets)
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
CSS:
====
* In Html we can upload the content in web page but using CSS we can make styles or
looks to that Html file.
* Three ways to apply styles to the file.
<body>
<style>
p{
color: red;
background: green;
}
h1{
color: yellow;
}
</style>
</body>
Selectors:
==========
* These selectors are used in internal and external style sheets.
* These are referred to which elements in file should be styled.
1. Simple Selector:
==================
* If we want to apply to tags, id's, lists this simple selector will be used.
i. Tag Based:
=============
tagname(p or h1){
property: value;
property: value;
.
.
.
property: value;
ii. id Based:
=============
* If we want to apply styles to only some paragraphs
we use this.
To those particular paragraphs use id attribute and give value to them and use
those values while applying styles to that particular paragraphs only.
Syntax:
=======
#id{
property: value;
property: value;
.
.
.
property: value;
}
Ex:
===
<head>
#second{
color: white;
backgriundcolor: pink;
}
</head>
<body>
<p>this is paragraph</p>
<p>this is paragraph</p>
<p id="second">this is paragraph</p>
<p id="second">this is paragraph</p>
<p>this is paragraph</p>
<p>this is paragraph</p>
</body>
iii. Class Based:
===============
* If we want to apply some different tags we use this tag.
* If we want to apply styles to only some h tags and some p tags we use this class
based.
Syntax:
=======
.class{
property: value;
property: value;
.
.
.
property: value;
}
Ex:
===
<head>
<h2> This is heading</h2>
<h2 class="special"> This is heading</h2>
<h2 class="special"> This is heading</h2>
<h2> This is heading</h2>
.special{
color: white;
backgroundcolor: pink;
}
</head>
<body>
<p>this is paragraph</p>
<p>this is paragraph</p>
<p class="special">this is paragraph</p>
<p class="special">this is paragraph</p>
<p>this is paragraph</p>
<p>this is paragraph</p>
</body>
2. Combination Selector:
========================
* If we want apply styles to combination of tag & id or class & tag or list & tag
we use this selector.
i. Child Selector(>):
=====================
* If we want to apply styles to the direct children of that tag we use this.
<body>
<p>this is paragraph</p> --> Direct Child tag
<p>this is paragraph</p> --> Direct Child tag
<div>
<p>this is paragraph</p> --> This is direct child tag to div and Indirect
Child Tag to body.
<p>this is paragraph</p> --> This is direct child tag to div and Indirect
Child Tag to body.
</div>
</body>
Ex:
===
<head>
<style>
div > p{
colour: white;
}
</style>
</head>
3. Pseudo-class Selectors:
==========================
* In the tags we have many states if we want to apply styles to that state means we
use this selector.
Syntax:
=======
selector:pseudo-classname
{
property= "value";
}
4. Pseudo-element Selectors:
============================
* In the Html file if we want to apply style to only the part of the content or
style to a particular letter in a word or a particular word in a paragraph we use
this selector.
Syntax:
=======
selector::pseudo-element{
property: value;
}
Ex:
===
* Common used pseudo elements are:
i. ::first-letter(:first-letter) --> If we want to apply style to first letter in
paragraph like making it big size and bold.
Ex:
===
<head>
<style>
h2::first-letter{
text-transform:uppercase
colour: white;
}
</style>
</head>
<body>
<p>This is paragraph</p>
<h2>This is heading2</h2>
<h2>This is heading2</h2>
<p>This is paragraph</p>
<h3>This is heading2</h3>
</body>
iii. ::before(:before) -->If we already define the content and now we want to add
content before to that content we use this before.
Ex:
===
<head>
<style>
h2::before{
content:"before element is used
colour: blue;
}
</style>
</head>
<body>
<p>This is paragraph</p>
<h2>This is heading2</h2>
<h2>This is heading2</h2>
<p>This is paragraph</p>
<h3>This is heading2</h3>
</body>
iv. ::after(:after) -->If we already define the content and now we want to add
content after to that content we use this after.
Ex:
===
<head>
<style>
h2::after{
content:"after element is used
colour: blue;
}
</style>
</head>
<body>
<p>This is paragraph</p>
<h2>This is heading2</h2>
<h2>This is heading2</h2>
<p>This is paragraph</p>
<h3>This is heading2</h3>
</body>
5. Attribute Selectors:
=======================
* If we want to apply only if that is the case only means we should apply styles.
* Attribute selectors are useful when you want to style elements based on specific
attributes they possess or when you want to target elements that have certain
characteristics in common across your HTML document.
i. [attribute] --> the style will be applied only if the attribute is matched does
not depend on value.
Syntax:
=======
selector[attribute name]
{
property: value;
}
Ex:
===
<head>
<style>
p[title]{
background:balck
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="para">This is paragraph</p>
</body>
* The style will be applied to both p tags.
ii. [attribute="value"] --> the style will be applied only if the attribute and
value is matched.
Syntax:
=======
selector[attribute=value]
{
property: value;
}
Ex:
===
<head>
<style>
p[title ="para"]{
background:balck
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="para">This is paragraph</p>
</body>
* The style will be applied to only "para" p tag.
iii. [attribute~="value"] --> the style will be applied the attribute and value
is matched but the value don't need to be exact.
Syntax:
=======
selector[attribute~=value]
{
property: value;
}
Ex:
===
<head>
<style>
p[title~="para"]{
background:balck
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="para">This is paragraph</p>
</body>
* The style will be applied to both "para" p tags because para is their in both p
tags.
iv. [attribute|="value"] --> the style will be applied the attribute and value is
matched or after that value their can be hyphen(-).
Syntax:
=======
selector[attribute|=value]
{
property: value;
}
Ex:
===
<head>
<style>
p[title|="para"]{
background:balck
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="para">This is paragraph</p>
<p title="para-">This is paragraph</p>
</body>
* The style will be applied to last two "para" p tags because the last one is
having hyphen(-).
Ex:
===
<head>
<style>
p[title^="para"]{
background: black;
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="para">This is paragraph</p>
<p title="para-">This is paragraph</p>
</body>
* The style will be applied to all "para" p tags because the all the three p tags
are started with para.
vi. [attribute$="value"] --> the style will be applied the attribute should
matched and the value's last should me matched.
Syntax:
=======
selector[attribute$=value]
{
property: value;
}
Ex:
===
<head>
<style>
p[title$="para"]{
background: black;
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="demo">This is paragraph</p>
<p title="graphpara">This is paragraph</p>
</body>
* The style will be applied to the last because only the last one is ending with
para.
vii. [attribute*="value"] --> the style will be applied the attribute should
matched and the value can be any where in the word.
Syntax:
=======
selector[attribute*=value]
{
property: value;
}
Ex:
===
<head>
<style>
p[title*="para"]{
background: black;
colour: white;
}
</style>
</head>
<body>
<p title="paragraph">This is paragraph</p>
<p title="demo">This is paragraph</p>
<p title="graphpara">This is paragraph</p>
</body>
* The style will be applied to the first and last para p tags.