CSS Cheat Sheet - A Basic Guide to CSS - GeeksforGeeks
CSS Cheat Sheet - A Basic Guide to CSS - GeeksforGeeks
What is CSS?
Table of Content:
External CSS: External CSS contains a separate CSS file with a .css
extension which contains only style property with the help of tag
attributes.
selector{
Open In App
property1: value1;
property2: value2;
}
Include external CSS file: The external CSS file is linked to the HTML
document using a link tag.
Internal CSS or Embedded: CSS is embedded within the HTML file using a
style HTML tag.
<style type="text/css">
div { color: #444;}
</style>
Inline CSS: It contains CSS properties in the body section specified within
HTML tags.
.clearfix::after {
content: "";
clear: both;
display: block;
}
Selectors: Used to find or select the HTML elements you want to style.
These are categorized as follows:
Basic
Description Syntax
Selectors
Complex selectors
selector1 selector2/ selector
consisting of more than one
Combinators 1+selector2 / selector 1>
selectors having some
selector 2 {property: value;}
relationship between them.
CSS
<!DOCTYPE html>
<html>
<head>
<title>* Selectors</title>
<!-- CSS Selectors are in used -->
<style>
/* universal selector */
* {
background-color: hsl(325, 63%, 82%);
text-align: center;Open In App
}
/* type selector */
span {
background-color: skyblue;
}
/* id selector */
#div1 {
color: green;
text-align: center;
font-size: 20px;
font-weight: bold;
}
/* class selector */
.div2 {
color: orange;
text-align: left;
font-size: 10px;
font-weight: bold;
}
/* attribute selector */
div[style] {
text-align: center;
color: purple;
font-size: 20px;
font-weight: bold;
margin-bottom: -20px;
}
/* combinator selector */
div>p {
color: #009900;
font-size: 32px;
font-weight: bold;
margin: 0px;
text-align: center;
}
/* class selector */
.box {
background-color: yellow;
width: 300px;
height: 100px;
margin: auto;
font-size: 30px;
text-align: center;
}
/* pseudo selector */
.box:hover { Open In App
background-color: orange;
}
</style>
</head>
<body>
<p>
*(Universal) Selector here gives a pink background
</p>
<br>
<span>This span is styled using type selector.
<br><br>
<div id="div1">
This div is styled using id selector
</div>
<br>
<div class="div2 ">
This div is styled using class selector
</div>
<br>
<div style="color:green">
This div is styled using attribute selector
</div>
<br>
<div style="text-align:center;">
This div is styled using combinators
<p>child selector</p>
</div>
<br>
<p>pseudo selector:</p>
<div class="box">
My color changes if you hover over me!
</div>
</body>
</html>
Font Properties: CSS font properties are used to set the font’s content of
the HTML element as per requirement.
Open In App
font-family:
Font- Specifies the font family to be used
family-name |generic-
family for the element’s text content.
family |initial |inherit;
font-variant:
Font- Converts all lowercase letters into
normal| small caps |
variant uppercase letters.
initial;
font-weight:
Font- Specifies thickness or weight of the
normal| bold |number
weight font
|initial |inherit |unset;
font-size:
Specifies the size of the text in HTML
Font-size small |medium |large
document.
|initial |inherit;
CSS
<!DOCTYPE html>
<html>
<head>
<title>Font properties</title>
<style>
.style1 {
font-family: "Times New Roman", "sans-serif";
font-weight: bold;
font-size: 30px;
color: #090;
text-align: center;
font-style: normal;
font-variant: normal;
}
.style2 {
font-family: "sans-serif";
font-weight: 5px; Open In App
font-size: 15px;
color: blueviolet;
text-align: left;
font-style: italic;
font-variant: normal;
}
.style3 {
font-family: "arial";
font-weight: 10px;
font-size: 20px;
color: black;
text-align: right;
font-style: oblique;
font-variant: small-caps;
}
</style>
</head>
<body>
<p>Normal text aligned center sized 10 px</p>
<div class="style1">Geeks for Geeks</div>
<p>Italic text aligned left sized 15px</p>
<div class="style2">Geeks for geeks</div>
<p>Oblique text aligned right sized 20px, in small caps</p>
<div class="style3">Geeks for geeks</div>
</body>
</html>
text-align:
Text- Defines the horizontal
left|right|center|
alignment alignment of the text.
justify|initial|inherit;
Open In App
Text- Add or remove text- text-decoration:
decoration decorations. decoration-type;
letter-spacing:
Specifies spacing between
Letter spacing normal|length|
the characters of the text.
initial|inherit;
line-height:
Specifies the space between
Line height normal|number|length|
the lines of the text.
percentage|initial|inherit;
text-shadow:
h-shadow v-shadow
Text-shadow Adds shadow to the text.
blur-radius
color|none|initial|inherit
word-spacing:
Specifies space between
Word spacing normal|length|
words of lines.
initial|inherit;
CSS
<!DOCTYPE html>
<html>
<head>
<title>Text formatting properties</title>
</head>
<body>
<div style=" color: red">
Open In App
Color property used here
</div>
</br>
<div style=" text-align: center">
Text align property used here
</div>
</br>
<div style=" text-decoration: underline">
Text decoration property used here
</div>
</br>
<div style="text-transform: lowercase">
Text transform property used here
</div>
</br>
<div style="text-indent: 80px">
Text indent property used here
</div>
</br>
<div style=" letter-spacing: 4px">
Text line spacing property used here
</div>
</br>
<div style="line-height: 40px">
Text line height property used here
</div>
</br>
<div style="text-shadow: 3px 1px blue;">
Text shadow property used here
</div>
</br>
<div style="word-spacing: 15px;">
Text word spacing property used here
</div>
</body>
</html>
Open In App
Specifies the
Background- background-color:
background color of an
color color_name;
element.
Specifies the
Background- background-position:
positioning of the
position value;
image in a certain way.
CSS
<!DOCTYPE html>
<html>
Open In App
<head>
<title>Background Properties</title>
<style>
.a {
background-image:
url(
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/2019041712430
}
.b {
background-image:
url(
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/2019041712430
background-repeat: no-repeat;
}
.c {
background-image:
url(
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/2019041712430
background-repeat: no-repeat;
background-position: center;
}
.d {
background-image:
url(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.pn
background-repeat: no-repeat;
background-origin: initial;
}
.e {
background-image:
url(
"https://media.geeksforgeeks.org/wp-content/uploads/geeks-25.png");
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
</head>
<body>
<div style="background-color: blue">Background color property</di
</br>
<div class="a" style="height: 200px; width: 100%">
<h3> Background Image property</h3>
</div>
<br><br>
<div class="b" style="height: 200px; width: 100%">
Open In App
<h3> Background repeat property: no-repeat</h3>
</div>
<br><br>
<div class="c" style="height: 200px; width: 100%">
<h3> Background position property</h3>
</div>
<br><br>
<div class="d" style="height: 200px; width: 100%">
<h3>Background origin property: The background-origin
is a property used to set the origin of the
image in the background.
</h3>
</div>
<br><br>
<div class="e" style="height: 400px;
width: 100%;
text-align:center;">
<h3> Background-attachment property</h3>
<p>The property background-attachment property in CSS is used
to specify the kind of attachment of the background image
It can be set to scroll or remain fixed.
</p>
</div>
<br>
</body>
</html>
Box Properties: The CSS box model is essentially a box that wraps around
every HTML element consisting of the border, padding, margin, and
content. The CSS properties used to attain the box model are:
margin:
Margin Used to set the margin
value;
Open In App
Sets the element’s border width and set the style, border:
Border
and color of an element’s border. value;
width:
Width Used to set an element’s width.
value;
height:
Height Used to set an element’s height
value;
CSS
<!DOCTYPE html>
<head>
<title>CSS Box Model</title>
<style>
.main {
font-size: 20px;
font-weight: bold;
Text-align: left;
}
.gfg {
margin-left: 60px;
border: 50px solid #009900;
width: 300px;
height: 200px;
text-align: center;
padding: 50px;
}
.gfg1 {
font-size: 42px;
font-weight: bold;
color: #009900;
margin-top: 60px;
background-color: #c5c5db;
}
.gfg2 {
font-size: 18px;
font-weight: bold;
Open In App
background-color: #c5c5db;
}
</style>
</head>
<body>
<div class="main">CSS Box-Model Property</div>
<div class="gfg">
<div class="gfg1">GeeksforGeeks</div>
<div class="gfg2">
A computer science portal for geeks
</div>
</div>
</body>
</html>
text-shadow:
Text h-shadow v-shadow blur-
Adds shadow to text.
shadow radius color| none |initial |
inherit;
box-shadow:
Box Gives shadow-like effect to the h-offset v-offset blur spread
shadow box or frames of an element. color |none |inset |initial |
inherit;
CSS
<!DOCTYPE html>
<html>
<head>
<title>CSS box-shadow Property</title>
Open In App
<style>
.gfg1 {
border: 1px solid;
padding: 10px;
/* box-shadow: h-offset v-offset blur */
box-shadow: 5px 10px 10px;
}
background-image:
Linear Creates smooth color
linear-gradient(direction, color-
Gradient transitions.
stop1, color-stop2, …);
CSS
Open In App
<!DOCTYPE html>
<html>
<head>
<title>CSS Gradients</title>
<style>
#main1 {
height: 200px;
background-color: white;
background-image: linear-gradient(white, green);
}
#main2 {
height: 350px;
width: 700px;
background-color: white;
background-image: radial-gradient(#090,
#fff, #2a4f32);
}
.gfg {
text-align: center;
font-size: 40px;
font-weight: bold;
padding-top: 80px;
}
.geeks {
font-size: 17px;
text-align: center;
}
</style>
</head>
<body>
<!-- Linear gradient -->
<div id="main1">
<div class="gfg">GeeksforGeeks</div>
<div class="geeks">
Linear Gradient
</div>
</div>
<br><br>
<!-- Radial Gradient -->
<div id="main2">
<div class="gfg">GeeksforGeeks</div>
<div class="geeks">
Radial Gradient
</div> Open In App
</body>
</html>
Border Properties: The CSS border properties allow you to specify how
the border of the box representing an element should look.
border-width:
Border Sets the width of the border of the length |thin
Width element. |medium |thick
|initial |inherit
CSS
<!DOCTYPE html>
<html>
<head>
<title> Border Properties</title>
<style>
#gfg1 {
border: 2px solid blue;
width: 60%;
}
#gfg2 {
border: thick dashed green;
width: 60%;
}
</style> Open In App
</head>
<body>
<div id="gfg1">
Demonstration of solid thick border of color blue
</div><br><br>
<div id="gfg2">
Demonstration of dotted 2px width border of color green
</div>
</body>
</html>
float:
Float Defines flow of content none|left|right|
initial| inherit;
Open In App
Set an element as visibility:
Visibility
visible or not. visible|hidden|collapse|initial|inherit;
cursor:
Specifies the type or auto|default|pointer|crosshair|help
Cursor
shape of cursor | e-resize | all-scroll |progress |initial
|inherit;
CSS
<!DOCTYPE html>
<html>
<head>
<title>Classification properties</title>
<style>
#geeks1 {
height: 50px;
width: 100px;
background: teal;
display: block;
}
#geeks2 {
height: 50px;
width: 100px;
background: cyan;
display: block;
}
#geeks3 {
height: 50px;
width: 100px;
background: green;
display: block;
}
.pos {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
.clr {
width: 100px; Open In App
height: 100px;
background-color: green;
color: white;
font-weight: bold;
font-style: itallic;
font-size: 25px;
text-align: center;
float: left;
padding: 15px;
}
p.GFG {
clear: left;
}
h1,
h2 {
color: green;
text-align: center;
}
.wait {
cursor: wait;
}
</style>
</head>
<body>
<p>display Property: block </p>
<div>
<div id="geeks1">Block 1 </div>
<div id="geeks2">Block 2</div>
<div id="geeks3">Block 3</div>
</div>
<br>
<p>Float Property:left</p>
<div style="font-size:20px; color:#006400; float:right;">
Content floats right
</div>
<br>
<p>Position Property:relative</p>
<div class="pos">
This div element has position: relative;
</div><br>
<p>Clear property: left</p>
<div class="clr">
<pre>GFG</pre>
</div>
<p> Open In App
GeeksforGeeks:
A computer science portal for geeks
</p>
<p class="GFG">GeeksforGeeks</p>
<br>
<p>Visibility property: visible/ hidden</p>
CSS Functions: CSS has a range of inbuilt functions. These are used as a
value for various CSS properties. Some of the CSS functions can be
nested as well. It ranges from simple color functions to mathematical,
shape, color, transform, gradient, and animations functions. Some of the
key functions are:
Open In App
Takes a string URL as a parameter and
url( <string> <url-
url() is used to load images, fonts and
modifier>* )
content
CSS
<!DOCTYPE html>
<html>
<head>
<title>CSS functions</title>
<style>
a:before {
content: attr(href) " =>";
}
a {
text-decoration: none;
}
body {
text-align: center;
}
.geeks {
position: absolute;
left: 50px;
width: calc(100% - 20%);
height: calc(100px - 20px);
background-color: green;
text-align: center;
}
.url {
background-image: url(
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234
text-align: center;
}
.gfg1 {
background-color: var(--main-bg-color);
padding: 10px;
}
:root {
Open In App
--main-bg-color: Green;
}
</style>
</head>
<body>
<p>attribute function</p>
<a href="https://www.geeksforgeeks.org">GeeksforGeeks</a><br><br>
<p>Calc function</p>
<div class="geeks">
<h3>The calc() Function</h3>
</div><br><br>
<p>URL function</p>
<div class="url" style="height:200px; width:100%">
<h3>CSS url() function</h3>
</div><br>
<p> var function</p>
<div class="gfg1">demonstration of var function</div><br>
</body>
</html>
Media Queries: The CSS Media Query is used to make the web page more
responsive according to the different screens or media types. Media
queries include a block of CSS only if a certain expression is true.
Syntax:
Open In App
Screen It is used for computer screens, smartphones etc.
Speech It is used for screen readers that read the screen aloud.
Similar Reads
15 min read
13 min read
5 min read
6 min read
Advertise with us
Company Languages
About Us Python
Legal Java
Privacy Policy C++
In Media PHP
Contact Us GoLang
Advertise with us SQL
GFG Corporate Solution R Language
Placement Training Program Android Tutorial
GeeksforGeeks Community Tutorials Archive
Open In App