<em><font size=10 color=red> test </font></em>
selector1, ..., selectorN {
property1: value1, ..., valueN;
property2: value1, ..., valueN;
...
/* this is comment */
}
<h1 style="color:red; font-family: sans-sarif">Test Heading 1</h1>
<head>
<style type="text/css">
h1 {
color: red;
font-family: sans-serif;
}
</style>
</head>
.css
extension contains
h1, h2, h3, h4, h5, h6 {
color: #FF0000;
font-family: sans-serif;
}
<head>
<title>External CSS</title>
<link href="external_css.css" rel="stylesheet" type="text/css" />
</head>
<style type="text/css" media="screen">
<link rel="stylesheet" href="style.css" media="all">
<head>
<style type="text/css">
* {color: blue}
h1 {text-decoration: underline}
p {font-style: italic}
</style>
</head>
<body>
<h1> Testing Element Selector </h1>
<p> This is a paragraph </p>
</body>
id
Selector
<h2 id="blue_heading"> This is blue heading </h2>
<h2 id="red_heading"> This is red heading </h2>
#blue_heading {color:blue;}
#red_heading {color:red;}
class
Selector
<h3 class="blue_heading"> This is blue heading 1</h3>
<h3 class="red_heading"> This is red heading 1</h3>
<h3 class="blue_heading"> This is blue heading 2</h3>
<h3 class="red_heading"> This is red heading 2</h3>
.blue_heading {color:blue;}
.red_heading {color:red;}
<style type="text/css">
.all {color: blue;}
h2.all {color: red;}
h3.all {color: black;}
</style>
<h1 class="all"> Heading 1 </h1>
<h2 class="all"> Heading 2 </h2>
<h3 class="all"> Heading 3 </h3>
<h4 class="all"> Heading 4 </h4>
<h5 class="all"> Heading 5 </h5>
<style type="text/css">
.red {color:red;}
.bold {font-weight:bold;}
</style>
<body>
<p class="red"> This is a red paragraph </p>
<p class="bold"> This is a bold paragraph </p>
<p class="red bold"> This is a red-bold paragraph </p>
</body>
<div>
and
<span>
in CSS
<div class="green_color">
This is a green div. But, <span id="red_color"> this is red </span> using span.
</div>
.green_color {
color:green;
font-weight:bold;
}
#red_color {color:red;}
<p> A paragraph without "id" attribute </p>
<p myid="test1"> A paragraph with id=test1 </p>
<p myid="test2"> A paragraph with id=test2 </p>
<p myid="test3"> A paragraph with id=test3 </p>
p[myid] {color:red}
p[myid="test3"] {color:blue}
a:link {color:#FF0000}
a:visited {color:#00FF00}
p:hover {color:#FF00FF}
a:active {color:#FFFFFF}
/* Selects any <p> that is the first element
among its siblings */
p:first-child {
color: lime;
}
p::first-letter {font-size: 200%;}
p::first-line {color: red;}
/* The ::selection selector matches the portion of
an element that is selected by a user. */
::selection {font-size: 2em;}
{ content: url(something); }
to insert an object before & after and element
.google::after{
content: url("google_logo.gif");
}
<style type="text/css">
p code:first-child {color: blue;}
p::first-letter {font-size: 200%;}
p::first-line {color: red;}
.google::after{content:url("google_logo.gif");}
</style>
<p>
This is the first <code> code </code>,
this is second <code> code </code>. <br />
<span class="google">Google</span>
is a big company.
</p>
::selection {
color: #fff;
background: #FF5E99;
text-shadow: none;
}
<style type="text/css">
ol {color: red;}
ol > li {color: blue;}
</style>
<ol>
<li> Item 1 </li>
<ul> <li> Netsted 1</li> </ul>
<li> Item 2 </li>
<dl> <dt> def: <dt> <dd> Definition </dd>
</dl>
</ol>
<style type="text/css">
ol {color: red;}
ol li {color: blue;}
</style>
<ol>
<li> Item 1 </li>
<ul> <li> Netsted 1</li> </ul>
<li> Item 2 </li>
<dl> <dt> def: <dt> <dd> Definition </dd>
</dl>
</ol>
tag1 + tag2 {property: value;}
tag1 ~ tag2 {property: value;}
h2+p {color:red;}
h3~p {color:green;}
<h2> Heading 2 </h2>
<p> Next sibling of h2 </p>
<p> Another sibling of h2 </p>
<h3> Heading 3 </h3>
<p> Next sibling of h3 </p>
<p> Another sibling of h3 </p>
Assuming there is not any conflicting style and external styles are linked before internal styles in the head
test.css
p {
font-style: italic;
}
index.html
<head>
<link href="test.css"
rel="stylesheet"
type="text/css" />
<style type="text/css">
p {color: blue;}
</style>
</head>
<body>
<p> Test Text 1</p>
<p style="font-weight: bold;"> Test Text 2 </p>
</body>
p {font-style: italic;}
code {color: red;}
p {color: red}
p {color: blue}
h1
)::before
)..example
)[type="radio"]
):hover
)#example
).
<style type="text/css">
p {
color: red;
}
p.c {
color: blue;
}
p#i {
color: green;
}
</style>
<p class="c" id="i" style="color: black">This is test</p>
<p class="c" id="i">This is test</p>
<p class="c">This is test</p>
<p>This is test</p>
!important
<style>
h1{font-style: italic;
color: blue !important}
</style>
<h1> Heading 1 </h1>
<h1 style="font-style: normal;color: red">Heading 1</h1>
border-left
, border-right
,
border-bottom
, border-top
border
: All borderspadding-left
, padding-right
,
padding-top
, padding-bottom
padding
: All paddingmargin-left
, margin-right
,
margin-top
, margin-bottom
margin
: All marginsmargin-left: auto; margin-right: auto;
width
, height
, max-width
, …
.demo.one {
border-style:solid;
border-width:3px;
border-color:black;
margin-left:0.5cm;
padding-left:3cm;
margin-right:3cm;
padding-right:0.5cm;
}
This is the <span class="demo one">first box</span>, and this is the
<span class="demo one">second box</span>
This is the first box, and this is the
second boxThis is a normal paragraph.
This is the red paragraph.
body{
background-image: url("linux.jpg");
}
p {
background-color: white;
font-size: 3em;
}
p#red{
width: 50%;
border-style: solid;
border-width: 8px;
border-color: white;
background-color: #CF0000;
}
background-repeat
repeat (default)
no-repeat
repeat-x
repeat-y
background-attachment: fixed
background-position: center center | center top | center bottom |
left top | xpos ypos ...
.demo.three {
border-style: solid;
border-width: 3px;
border-color: black;
width: 10cm;
height: 5cm;
background-color: red;
}
.demo.four {
border-style: solid;
border-width: 3px;
border-color: black;
width: 10cm;
height: 5cm;
}
position
propertyProperty | Value | Description |
---|---|---|
position | static | default position = Normal |
relative | offset from its normal static position | |
absolute | a fixed position within its containing element | |
fixed | a fixed position within the browser window | |
sticky | this is basically a hybrid between relative and fixed position. | |
top, bottom, left, right | positions of box's corners |
top: +20px
top: -20px
bottom: -20px
bottom: +20px
The paragraph 1
The paragraph 2
The paragraph 3
The paragraph 4
p.demo.five {
border-style: solid;
border-width: 2px;
border-color: black;
width: 300px;
}
#m {
position: relative;
left: 50%;
top: 150px;
border-color: red;
}
<html>
top
,
left
,
right
,
bottom
properties
<a href="https://1995parham-teaching.github.io/ie-lecture/" target="_blank">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="/img/forkme_right_orange.png" alt="Fork me on GitHub">
</a>
The paragraph 1
The paragraph 2
The paragraph 3
The paragraph 4
top
,
left
, right
,
bottom
properties
.sticky-element {
position: sticky;
top: 0px;
background-color: orange;
}
z-index
specifies the stack order of an element
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
h1 {
color: red;
}
float: left | right | none
Top
float: right; width: 45%;
width: 45%;
normal
element must have
clear
attributeclear
attribute can be set to
left
, right
,
or both
This is header
This is a paragraph in the left column
Menu Item 1
Menu Item 2
This is footer.
This is header
This is a paragraph in the left column
Menu Item 1
Menu Item 2
This is footer.
This is header
This is a paragraph in the left column
Menu Item 1
Menu Item 2
This is footer.
display
propertyinline
: no break after or before, no
width/heightblock
: break after and before, fill a
line, width/height are modifiableinline-block
: inside is formatted as
block-level box, the element formatted as an inline-level boxflex
grid
.demo.inline {
display: inline;
}
.demo.inlineblock {
display: inline-block;
width: 180px;
height: 100px;
border-style: solid;
border-width: 3px;
}
<div class="demo inline">1</div>
<div class="demo inline">
<span>2</span>
<div class="demo inlineblock">3 4 5 6 7 8 9 0</div>
<div class="demo inlineblock">3 4 5 6 7 8 9 0</div>
<div class="demo inlineblock">3 4 5 6 7 8 9 0</div>
</div>
We can create a flex container using display: flex
Now we have flex container and flex items
.flex-container {
flex-direction: row | column | row-reverse | column-reverse;
// Default flex-direction: row
}
We can align flex items in the direction of main axis using justify-content property
We can align flex items in the direction of cross axis using align-items property
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.container {
display: flex;
flex-direction: row-reverse;
justify-content: space-around;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
}
Controls the wrapping of the container
Items will be wrapped if there is not enough space
.flex-container {
flex-wrap: wrap | no-wrap | wrap-reverse;
// no-wrap is the default
}
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
}
You can set a gap between rows and columns
row-gap: set gap between rows
column-gap: set gap between columns
gap: a shorthand for row-gap column-gap
.flex-container{
gap: 10px 20px;
// row-gap: 10px;
// column-gap: 20px;
}
.container {
gap: 20px;
}
Why don't we just use margin?
Gap is a useful property in responsive design
We will see the difference in the media query section
In a flex container, try to use flex-basis instead of width property. Because flex-basis specifies width or height depending on the value of flex-direction.
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.blue {
flex-basis: 300px;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.blue {
flex-basis: 300px;
}
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.blue {
flex-grow: 1;
}
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.blue {
flex-grow: 2;
}
.red {
flex-grow: 2;
}
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.blue {
order: 4;
}
Instead of using
.item{
flex-grow: 1;
flex-shrink: 1;
flex-basis: 300px;
}
You can use flex property
.item{
flex: 1 1 300px; // flex-grow flex-shrink flex-basis
}
Use align-self to override align-items for an individual item
.item {
align-self: auto | flex-start | flex-end | center | stretch
}
.container {
align-items: center;
}
.blue {
align-self: flex-end;
}
.red {
align-self: flex-start;
}
.orange {
align-self: stretch;
}
cm
mm
in
px
pt
Responsive Design is the practice of making sure your content looks good on all screen sizes.
Everything in the website including layouts, fonts and images should automatically adapt to the user's device.
Apply a block of CSS properties only if a certain condition is true
@media mediatype and (media-feature) {
// Styles
}
Defines the category of device
Describe a specific characteristic of the browser
Create more complex media queries using logical operators
@media screen and (media-feature) and (media-feature) {...}
@media screen and (media-feature), print and (media-feature) {...}
@media not screen and (media-feature)
Try to resize the screen
@media screen and (max-width: 800px){
.box{
width: 250px;
height: 250px;
background-color: #2c3e50;
border-radius: 10px;
border-color:#c0392b ;
}
}
.box {
flex-basis: 33%;
}
@media screen and (max-width: 600px){
.flex-container {
flex-wrap: wrap;
}
.flex-item {
flex-basis: 100%
}
}
You can have multiple media queries
@media screen and (max-width: 800px){
.box {
background-color: #2c3e50;
}
}
@media screen and (max-width: 600px){
.box {
background-color: #16a085;
}
}
.main {
gap: 2%;
}
.box {
flex-basis: 32%;
}
@media screen and (max-width: 600px){
.sidebar {
display: none;
}
.main {
flex-wrap: wrap; // or set it out of media query
}
.box {
flex-basis: 100%;
}
}
Try to change the orientation
@media screen and (orientation: landscape){
.box{
width: 250px;
height: 250px;
background-color: #2c3e50;
border-radius: 10px;
border-color:#c0392b ;
}
}
rgb()
, rgba()
, usl()
, …url()
, image()
, …var()
, calc()
, …scale()
, translate()
,
rotate()
, …repeat()
:root {
--blue: #1e90ff;
--white: #ffffff;
}
body { background-color: var(--blue); }
h2 { border-bottom: 2px solid var(--blue); }
.container {
color: var(--blue);
background-color: var(--white);
padding: 15px;
}
button {
background-color: var(--white);
color: var(--blue);
border: 1px solid var(--blue);
padding: 5px;
}
/* The animation code */
@keyframes example {
0% {background-color:red; left:0px; top:0px;}
25% {background-color:yellow; left:200px; top:0px;}
50% {background-color:blue; left:200px; top:200px;}
75% {background-color:green; left:0px; top:200px;}
100% {background-color:red; left:0px; top:0px;}
}
/* The element to apply the animation to */
#the-animation {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
visibility:hidden
It is not visible but gets up it’s original
space whereasdisplay:none
It is hidden and takes no space