Css
Css
Why css
To apply the styles to elements in html
● Font-color
● Background color
● font-sizes
Ways to specify styles
● Inline
● Embed
● External
Inline styles
Apply styles directly to the elements
<div style="background-color:green">
this is div1
</div>
Embed styles
We specify the styles with in the head tag.
<style>
p{
text-align: center;
color: red;
background-color:green;
}
</style>
External css
<head>
<title>External CSS</title>
<link type="text/css" rel="stylesheet" href="style.css">
</head>
selectors
selector
● Element
● Id
● Class
● Attribute
● Decedent
● Sibling
Element selector
If we need to apply style to an element then we use the element selector. In below
example we are applying the style to paragraph element
<style>
p {
text-align: center;
color: red;
background-color:green;
}
</style>
Id selector
If we want to apply style to an element with the specific id then we use the id selector.
<p id="para1">Paragraph2</p>
#para1 {
text-align: center;
color: red;
}
assignment
Define inline styles ,id ,element and see who will win and how using the inspect
Specificity is applicable only if
conflicting style
p{ <p>para1</p>
background-color: blue; <p id="abc" style="background-
color:yellow; color:blueviolet">para3</p>
}
#abc{
background-color: brown;
}
● Inline styles
● Id
● Class
● element
Attribute selectors
If we want to apply the styles to the html elements with a specific attribute or attribute
having specific text then we are going to use the attribute selectors.
[title]{
background-color: yellow
}
Attribute with specific value
[title^="you"]{
background-color: yellow;
}
It selects the elements which starts with the attribute value with you
Combine selectors
Combine selectors
div[title='center']{
background-color: blue;
}
assignment
Apply the styles only to the div elements with the title attribute
Descendant selectors
If we want to apply the styles for the children or grand children of html elements then we
use the descendant selector
a1 a2
a3
a11 a21
a12
a112
a111
div p{
background-color:brown;
}
Child selector
If we want to apply the styles to the immediate children we use the child selector
<p>this is para3</p>
<div class="good">this is a div with title
<p>this is para</p>
<span>this is span</span>
<span>this children div
<p>this is para inside the child div</p>
</span>
</div>
<p>this is para2</p>
<p>this is para4</p>
<div >this is the div1
<p>this is para inside second paragraph</p>
General siblings
div ~ p{
background-color:brown;
}
Adjancent sibling
<p>this is para3</p>
<div class="good">this is a div with title
<p>this is para</p>
</div>
<p>this is para2</p>
<p>this is para4</p>
<div >this is the div1
<p>this is para inside second paragraph</p>
</div>
div + p{
background-color:brown;
}
Pseudo Classes
Hover,visited, active
a{
color:purple;
font-size: 32px;
}
a:hover{
color:seagreen;
}
a:active{
color:brown;
}
<div>
<a href="https://www.google.com">google</a>
</div>
<div>
<a href="http://www.uitricks.in">ui tricks</a>
</div>
<div>
<a href="http://www.jatanix.com">jatanix</a>
</div>
display
● Block
● None
● Visibility:hidden
● inline
p:first-child{
background-color: green;
}
<div id="div1">
<p>this is para1</p>
<p>this is para2</p>
</div>
<div id="div2">
<p>this is para3</p>
<p>this is para4</p>
</div>
focus
If we want to apply the style when an input element is focussed we are going to use this
styles
input:focus{
background-color: green;
}
<div>
user name <input type="text" name="" id="">
</div>
<div>
phone <input type="number" name="" id="">
</div>
If we want to apply style only for the text box
input[type=text]:focus{
background-color: blue;
}
select option[value="1"]{ <select name="" id="">
background-color: yellow; <option value="1">india</option>
} <option value="2">china</option>
select option[value="2"]{ <option value="3">pakisthan</option>
background-color: blue; </select>
}
select option[value="3"]{
background-color: green;
}
nth-child
<div> p:nth-child(4){
<p>para1</p>
background-color: green;
<p>para2</p>
}
<p>para3</p>
<p>para4</p>
<p>para5</p>
<p>para6</p>
</div>
Odd and Even child
<div> p:nth-child(odd){
<p>para1</p> background-color: green;
<p>para2</p> }
<p>para3</p>
<p>para4</p> p:nth-child(even){
<p>para5</p> background-color: blue;
<p>para6</p> }
</div>
Assignment - define a table
with the data and style odd
and even rows and headers
nth child an+b ( n is loop from 0 to number of elements
<div> p:nth-child(2n+1){
<p>para1</p> background-color: green;
<p>para2</p> }
<p>para3</p>
<p>para4</p>
<p>para5</p>
<p>para6</p>
<p>para7</p>
<p>para8</p>
<p>para9</p>
Pseudo Elements
Pseudo elements
● p::first-line
● p::first-letter
● h1::before
● h1::after
::first-line
<div>
<p>Lorem ipsum dolor sit amet consectetur
adipisicing elit. Molestias asperiores rem tenetur
eum, necessitatibus perferendis libero vitae velit nisi
p::first-line{
accusantium recusandae temporibus error dolor color:blueviolet;
reiciendis fugiat fuga aliquam. Ullam, cumque! }
Lorem ipsum dolor sit, amet consectetur adipisicing
elit. Debitis, ratione! Doloribus eum dolores aperiam
neque, ducimus, id velit voluptatum tenetur officia
necessitatibus a. Necessitatibus dolorum doloribus
nemo, nesciunt sint porro.</p>
</div>
Simulate in the
Device/Tab Mode
::first-letter
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
If we want to highlight the first letter in paragraph
::before
h1::before
{
content: url(laughy.jpeg);
}
h1::after
{
content: url(laughy.jpeg);
}
If we want to add some content after element
::selection
::selection {
color: red;
background: yellow;
}
● Width
● Height
● Padding
● Margin
● border
Div with box styles
div{
width:300px;
height:100px;
border:2px solid red;
padding: 5px;
}
Visibility : hidden
Vs
Display : none
Visibility : hidden - > will not render the element but still reserve the space
Display:none -> will not render the element but will not reserve the space
box-sizing
We use box sizing if we want to control padding ,margin,border to be inclusive of width or
exclusive of width
● Border-box
● content-box
border-box
We use this if the width specified is for the content and padding,margin,border all inclusive
div{
height: 300px;
width: 300px;
border: 5px solid royalblue;
padding:5px;
margin: 5px;
box-sizing: border-box;
content-box
We use this if the width specified is only for the content and padding,margin,border or
exclusive
div{
height: 300px;
width: 300px;
border: 5px solid royalblue;
padding:5px;
margin: 5px;
box-sizing: content-box;
}
positions
Positions
● Static
● Relative
● Fixed
● absolute
static
This is the default position for all the elements. Mean they will come where are they are
supposed to come
relative
.abc{
position: relative;
top:20px;
left:100px;
}
relative
50p,50px
70px,70px
top:20px,left:20px
fixed
.abc{
position: fixed;
bottom:0px;
right: 0px;
}
Fixed - reference point window position
50p,50px
top:20px,left:20px
absolute
.abc{
position: absolute;
top:20px;
left:100px;
}
In CSS we have 3 axis
● X axis
● Y axis
● Z axis
img{
position: absolute;
top:10px;
right: 10px;
z-index: -10;
}
backgrounds
background-image
div {
height: 500px;
width: 100%;
background-image: url("paper.jpg");
color: red;
font-size: 5em;
}
background-repeat
● Underline
● linethrough
● overline
text-transform
● Uppercase
● Lowercase
● titlecase.
font-size
● How much size we want the font
● What is px? Px is measurement unit for html and css. The way we use
cm,feet,meters are used to measure the distance units. We use px in the css world.
● By default font-size is 16px
Sizes can be applied in 3 ways
● Px
● Em
● percentage.
#div1{ body{
text-align: left; font-size: 64px;
text-decoration: underline; }
text-transform: uppercase;
#div3{
} text-align: right;
#div2{ text-decoration: line-through;
text-align: center; text-transform: lowercase;
text-decoration: overline; font-size: 200%;
text-transform: capitalize; }
font-size: 2em;
}
best practice specify size
Em,percenrtage are best practice to specify font-size , height and width for the responsive
web design
User agent styles
By default all the browsers apply some styles. They are called as UAS
Media queries
applying different styles based on the media like print,screen,reader etc
Based on the width and height of the screen also we want to apply some styles. They also
called as media queries
div{
height: 200px;
background-color: yellow;
border: 1px solid red;
}