<style>
element in the <head>
section.<link>
element to link to an external CSS file.Inline CSS:
<h1 style="color:blue;">A Blue Heading</h1>
<p style="color:red; font-size:20px;">A red paragraph.</p>
Internal CSS:
<head>
section of an HTML page, within <style>
element.<head>
<style>
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
</style>
</head>
External CSS:
.css
extension and should be linked to the HTML document using a <link>
tag.<head>
<link rel="stylesheet" href="style.css" />
</head>
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
CSS selectors are used to "find" (or select) the HTML elements you want to style.
CSS Element Selector:
<!DOCTYPE html>
<html>
<head>
<style>
p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
CSS ID Selector:
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
CSS Class Selector:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
</body>
</html>
CSS Universal Selector:
/* Universal Selector */
* {
text-align: center;
color: blue;
}
color
property specifies the text color of an element.<!DOCTYPE html>
<html>
<body>
<h3 style="color:Red;">Hello World</h3>
<p style="color:Blue;">Lorem ipsum dolor sit</p>
<p style="color:Green;">Ut wisi enim ad minim veniam</p>
</body>
</html>
CSS Background Color:
background-color
property specifies the background color of an element.body {
background-color: lightblue;
}
CSS Background Image:
background-image
property specifies an image to use as the background of an element. By default, the image is repeated so it covers the entire element.body {
background-image: url("bgimg.jpg");
}
CSS Background Repeat:
body {
background-image: url("bgimg.png");
background-repeat: no-repeat; /* Not Repeated */
background-repeat: repeat-x; /* Repeated Horizontally */
background-repeat: repeat-y; /* Repeated Vertically */
}
CSS Background Position:
background-position
property is used to specify the position of the background image.body {
background-image: url("bgimg.png");
background-repeat: no-repeat;
background-position: right top;
}
CSS Background Attachment:
background-attachment
property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page)body {
background-image: url("bgimg.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
opacity
property specifies the opacity/transparency of an element.0.0
- 1.0
. The lower value, the more transparent
CSS Border Style:
border-style
property specifies what kind of border to displaydotted
- Defines a dotted borderdashed
- Defines a dashed bordersolid
- Defines a solid borderdouble
- Defines a double bordergroove
- Defines a 3D grooved border. The effect depends on the border-color valueridge
- Defines a 3D ridged border. The effect depends on the border-color valueinset
- Defines a 3D inset border. The effect depends on the border-color valueoutset
- Defines a 3D outset border. The effect depends on the border-color valuenone
- Defines no borderhidden
- Defines a hidden border
.box {
border-style: solid;
}
CSS Border Color:
border-color
property is used to set the color of the four borders..box {
border-style: solid;
border-color: green;
}
CSS Border Width:
border-width
property specifies the width of the four borders..box {
border-style: solid;
border-color: green;
border-width: medium;
}
CSS Rounded Borders:
border-radius
property is used to add rounded borders to an element.box {
border-style: solid;
border-color: green;
border-width: medium;
border-radius: 5px;
}
margin
properties are used to create space around elements, outside of any defined borders.margin-top
margin-right
margin-bottom
margin-left
auto
- the browser calculates the marginlength
- specifies a margin in px, pt, cm, etc.%
- specifies a margin in % of the width of the containing elementinherit
- specifies that the margin should be inherited from the parent elementbody{
margin: 5px;
}
div{
margin-top: 5px;
margin-right: 10px;
margin-bottom:7px;
margin-left: 10px;
}
h1{
margin: 5px 10px 7px 10px;
}
.box{
margin 10px 20px;
}
padding
properties are used to generate space around an element's content, inside of any defined borders.padding-top
padding-right
padding-bottom
padding-left
length
- specifies a padding in px, pt, cm, etc.%
- specifies a padding in % of the width of the containing elementinherit
- specifies that the padding should be inherited from the parent elementbody{
padding: 5px;
}
div{
padding-top: 5px;
padding-right: 10px;
padding-bottom:7px;
padding-left: 10px;
}
h1{
padding: 5px 10px 7px 10px;
}
.box{
padding 10px 20px;
}
height
and width
properties are used to set the height and width of an element.max-width
property is used to set the maximum width of an element.auto
- This is default. The browser calculates the height and widthlength
- Defines the height/width in px, cm, etc.%
- Defines the height/width in percent of the containing blockinitial
- Sets the height/width to its default valueinherit
- The height/width will be inherited from its parent valuebody {
height: 200px;
max-width: 70%;
}
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Text Color & Background Color:
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: green;
}
Text Alignment & Text Direction:
text-align
property is used to set the horizontal alignment of a text. A text can be left
or right
aligned, centered
, or justified
.direction
property specifies the text direction/writing direction. It can be rtl
(right to left) or ltr
(left to right)h1 {
text-align: center;
direction: rtl;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
Text Decoration:
text-decoration-line
(required)text-decoration-color
(optional)text-decoration-style
(optional)text-decoration-thickness
(optional)Text Transformation:
text-transform
property is used to specify uppercase and lowercase letters in a text.uppercase
or lowercase
letters, or capitalize
the first letter of each word:p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Text Spacing:
text-indent
property is used to specify the indentation of the first line of a textletter-spacing
property is used to specify the space between the characters in a text.line-height
property is used to specify the space between lines.word-spacing
property is used to specify the space between the words in a text.white-space
property specifies how white-space inside an element is handled.p {
text-indent: 50px;
letter-spacing: 5px;
line-height: 0.7;
word-spacing: 10px;
white-space: nowrap;
}
Text Shadow:
text-shadow
property adds shadow to text.h1 {
text-shadow: 2px 2px;
}
font-family
property to specify the font of a text.font-style
property is mostly used to specify italic textfont-weight
property specifies the weight of a font:font-size
property sets the size of the text.
div {
font-family: "Lucida Console", "Courier New", monospace;
font-style: italic;
font-weight: lighter;
font-size: 14px;
}
list-style-type
property specifies the type of list item marker.ul {
list-style-type: circle;
}
display
property is used to specify how an element is shown on a web page.Block-level Elements:
<div>
<h1>
to <h6>
<p>
<form>
<header>
<footer>
<section>
Inline Elements:
<span>
<a>
<img>
Display Property Values:
Value | Description |
---|---|
inline | Displays an element as an inline element (like <span> ). Any height and width properties will have no effect. This is default. |
block | Displays an element as a block element (like <p> ). It starts on a new line, and takes up the whole width |
contents | Makes the container disappear, making the child elements children of the element the next level up in the DOM |
flex | Displays an element as a block-level flex container |
grid | Displays an element as a block-level grid container |
inline-block | Displays an element as an inline-level block container. The element itself is formatted as an inline element, but you can apply height and width values |
inline-flex | Displays an element as an inline-level flex container |
inline-grid | Displays an element as an inline-level grid container |
inline-table | The element is displayed as an inline-level table |
list-item | Let the element behave like a <li> element |
run-in | Displays an element as either block or inline, depending on context |
table | Let the element behave like a <table> element |
table-caption | Let the element behave like a <caption> element |
table-column-group | Let the element behave like a <colgroup> element |
table-header-group | Let the element behave like a <thead> element |
table-footer-group | Let the element behave like a <tfoot> element |
table-row-group | Let the element behave like a <tbody> element |
table-cell | Let the element behave like a <td> element |
table-column | Let the element behave like a <col> element |
table-row | Let the element behave like a <tr> element |
none | The element is completely removed |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
The position
property specifies the type of positioning method used for an element.
There are five different position values:
static
position: static;
is not positioned in any special way; it is always positioned according to the normal flow of the pagerelative
position: relative;
is positioned relative to its normal position.fixed
position: fixed;
is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled.absolute
position: absolute;
is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).sticky
position: sticky;
is positioned based on the user's scroll position.position:fixed
).Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.
z-index
property specifies the stack order of an element (which element should be placed in front of, or behind, the others).img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
h1 {
z-index: 1;
}
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.overflow
property has the following values:
visible
- Default. The overflow is not clipped. The content renders outside the element's boxhidden
- The overflow is clipped, and the rest of the content will be invisiblescroll
- The overflow is clipped, and a scrollbar is added to see the rest of the contentauto
- Similar to scroll, but it adds scrollbars only when necessarydiv {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid;
overflow: visible;
}
transition-timing-function
property specifies the speed curve of the transition effect.transition-timing-function
property can have the following values:
ease
- specifies a transition effect with a slow start, then fast, then end slowly (this is default)linear
- specifies a transition effect with the same speed from start to endease-in
- specifies a transition effect with a slow startease-out
- specifies a transition effect with a slow endease-in-out
- specifies a transition effect with a slow start and endcubic-bezier(n,n,n,n)
- lets you define your own values in a cubic-bezier functiontransition-delay
property specifies a delay (in seconds) for the transition effect.transition-duration
property specifies how many seconds or milliseconds a transition effect takes to completediv{
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
div:hover{
width: 300px;
}
CSS allows animation of HTML elements without using JavaScript
An animation lets an element gradually change from one style to another.
You can change as many CSS properties you want, as many times as you want.
To use CSS animation, you must first specify some keyframes for the animation.
Keyframes hold what styles the element will have at certain times.
When you specify CSS styles inside the @keyframes
rule, the animation will gradually change from the current style to the new style at certain times.
To get an animation to work, you must bind the animation to an element.
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
@keyframes example {
from {
background-color: red;
}
to {
background-color: yellow;
}
}
animation-direction
property specifies whether an animation should be played forwards, backwards or in alternate cycles.animation-direction
property can have the following values:
normal
- The animation is played as normal (forwards). This is defaultreverse
- The animation is played in reverse direction (backwards)alternate
- The animation is played forwards first, then backwardsalternate
-reverse - The animation is played backwards first, then forwardsdiv {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-direction: reverse;
}
@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;
}
}
animation-timing-function
property specifies the speed curve of the animation.animation-timing-function
property can have the following values:
ease
- Specifies an animation with a slow start, then fast, then end slowly (this is default)linear
- Specifies an animation with the same speed from start to endease-in
- Specifies an animation with a slow start ease-out - Specifies an animation with a slow endease-in-out
- Specifies an animation with a slow start and endcubic-bezier(n,n,n,n)
- Lets you define your own values in a cubic-bezier function@media
rule to include a block of CSS properties only if a certain condition is true.@media only screen and (max-width: 600px){
body {
background-color: lightblue;
}
}
Made By SOU Student for SOU Students