Cascading Style Sheets
Add Some Style
Dr. Daniel Kelly
A helpful way to understand CSS is to imagine a box surrounding every HTML element
<body>
<h1>Far Far Away</h1>
<p>Far far away, <em>behind the word mountains</em>, far from the
countries Vokalia and Consonantia, there live the blind texts.
Separated they live in Bookmarksgrove right at the coast of the
Semantics, a large language ocean. A small river named Duden flows
by their place and supplies it with the necessary regelialia.</p>
<p>It is a paradisematic country, in which roasted parts of sentences
fly into your mouth. Even the all-powerful <strong>Pointing</strong>
has no control about the blind texts it is an almost unorthographic life.
One day however a small line of blind text by the name of Lorem Ipsum
decided to leave for the far World of Grammar.</p>
<p>The Big Oxmox advised her not to do so, because there were thousands of
bad Commas, wild Question Marks and devious Semikoli, but the
<strong>Little Blind Text</strong> didn’t listen. She packed her seven versalia,
put her initial into the belt and made herself on the way.</p>
</body>
: Block Level Element
: Inline Element
CSS allows us to create rules that control the way each individual box (and contents) is presented
<body>
<h1>Far Far Away</h1>
<p>Far far away, <em>behind the word mountains</em>, far from the
countries Vokalia and Consonantia, there live the blind texts.
Separated they live in Bookmarksgrove right at the coast of the
Semantics, a large language ocean. A small river named Duden flows
by their place and supplies it with the necessary regelialia.</p>
<p>It is a paradisematic country, in which roasted parts of sentences
fly into your mouth. Even the all-powerful <strong>Pointing</strong>
has no control about the blind texts it is an almost unorthographic life.
One day however a small line of blind text by the name of Lorem Ipsum
decided to leave for the far World of Grammar.</p>
<p>The Big Oxmox advised her not to do so, because there were thousands of
bad Commas, wild Question Marks and devious Semikoli, but the
<strong>Little Blind Text</strong> didn’t listen. She packed her seven versalia,
put her initial into the belt and made herself on the way.</p>
</body>
selector
{
property: value;
}
selector
property
: selects existing HTML
: applies style to selected HTML
h1
{
color: pink;
}
Changes color text for all h1's to pink
selector
{
property: value1;
anotherproperty: value2;
}
Change style of multiple aspects of element at once
p
{
color: red;
background-color: blue;
}
Change text color to red and background color to blue for all <p> elements
/*Make all h1's in the html purple and 50px font*/
h1{
color: purple;
font-size: 50px;
}
/*Make all img's in the html have 2px red border*/
img{
border-color: red;
border-size: 2px;
}
<!DOCTYPE html>
<html>
<head>
<title>CSS Demo</title>
<link rel="stylesheet" type="text/css" href="myfirststyle.css">
</head>
<body>
<h1>Heading here</h1>
<h4>Sub-heading here</h4>
<p>Body text here.</p>
</body>
</html>
myhtmlfile.html:
myfirststyle.css:
h1{
color: red;
}
h4{
color: green;
}
Result on Browser:
h1
{
color: red;
}
h2
{
color: green;
}
h1
{
color: red;
}
h1
{
color: #000000;
}
h2
{
color: #FF1493;
}
#000000
#000000
h1
{
color: rgb(0, 255, 0);
}
h2
{
color: rgb(10, 99, 150);
}
h1
{
color: rgba(0, 255, 0, 1);
}
h2
{
color: rgba(10, 99, 150,.3);
}
background : sets the color of background
color : sets the color of text
body{
background: #b5cece;
}
div{
background: #6ac9c9;
}
p{
color: #1b3333;
}
<body>
<div>
<p>Introduction section goes here</p>
<p>Main body section goes here</p>
<p>Conclusion section goes here</p>
</div>
</body>
Warm and Cool
Warm and Cool
Bright Accent Colors
Bright Accent Colors
Minimalist mixed with Striking color
Minimalist mixed with Striking color
body{
background-image: url(addressOfImageFile.jpg);
}
body{
background-image: url(computer-wallpaper-5.jpg);
}
div{
background: #6ac9c9;
}
p{
color: #1b3333;
}
<body>
<div>
<p>Introduction section goes here</p>
<p>Main body section goes here</p>
<p>Conclusion section goes here</p>
</div>
</body>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="mystlye.css">
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="bg.css">
</head>
<body>
<h1>Heading</h1>
<h4>Sub heading</h4>
<p>Main text here</p>
</body>
</html>
body{
background-image: url(https://i.imgur.com/SPZdXnP.png);
}
h1{
color: #119DA4;
}
h4{
color: #80DED9;
}
p{
color: #AEECEF;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="bg.css">
</head>
<body>
<h1>Heading</h1>
<h4>Sub heading</h4>
<p>Main text here</p>
</body>
</html>
body{
background: url(https://i.imgur.com/SPZdXnP.png);
background-repeat: no-repeat;
}
h1{
color: #119DA4;
}
h4{
color: #80DED9;
}
p{
color: #AEECEF;
}
background-repeat: no-repeat;
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="bg.css">
</head>
<body>
<h1>Heading</h1>
<h4>Sub heading</h4>
<p>Main text here</p>
</body>
</html>
body{
background: url(https://i.imgur.com/SPZdXnP.png);
background-repeat: no-repeat;
background-size: cover;
}
h1{
color: #119DA4;
}
h4{
color: #80DED9;
}
p{
color: #AEECEF;
}
background-size: cover;
Right Click anywhere on Webpage->Inspect
Open up previous example in chrome and right click, then click Inspect
border-color: blue;
border-width: 5px;
border-style: solid;
border: 5px solid blue;
border-width: 1px 4px 12px 4px;
Top Right Bottom Left
border-style: solid;
border-style: dotted;
border-style: double;
border-style: double solid none solid;
Top Right Bottom Left
selector{
property: value;
anotherproperty: value;
}
<div>
<p>Section 1 main text goes here</p>
<p>Section 1 conclusion text goes here</p>
</div>
<div>
<p>Section 2 main text goes here</p>
<p>Section 2 conclusion text goes here</p>
</div>
div{
background: green;
}
p{
color: orange;
}
<h1 id="my_id">Heading Text</h1>
#my_id
{
color: red;
}
1:
2:
<div>
<p>Section 1 main text goes here</p>
<p>Section 1 conclusion text goes here</p>
</div>
<div>
<p id="importantPar">Section 2 main text goes here</p>
<p>Section 2 conclusion text goes here</p>
</div>
div{
background: green;
}
#importantPar {
color: orange;
}
Each ID can only appear once per HTML page
selector{
property: value;
anotherproperty: value;
}
<h1 class="my_class">Heading Text</h1>
.my_class
{
color: red;
}
1:
2:
.
<div>
<p class="maintext">Section 1 main text goes here</p>
<p>Section 1 conclusion text goes here</p>
</div>
<div>
<p class="maintext">Section 2 main text goes here</p>
<p>Section 2 conclusion text goes here</p>
</div>
div{
background: green;
}
.maintext {
background: pink
}
Same class can be assigned to multiple elements on same HTML page
selector{
property: value;
anotherproperty: value;
}
<h1>Ulster University</h1>
<a href="https://www.ulster.ac.uk/">Go to Ulster University Website</a>
<div>
<h4>Schools</h4>
<ul>
<li>Computing</li>
<li>Nursing</li>
<li>Business</li>
</ul>
</div>
<div>
<h3>Computing Courses</h3>
<ul>
<li>Information Technology</li>
<li>Computer Science</li>
<li>Computer Engineering</li>
</ul>
</div>
<div>
<h3>Important Sites</h3>
<ul>
<li><a href="https://www.ulster.ac.uk/">Ulster University Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/computing-engineering-and-the-built-environment/schools/computing-engineering-intelligent-systems">Computer Science Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/ulster-university-business-school">Business Home</a></li>
</ul>
</div>
Selects EVERYTHING on the page
<h1>Ulster University</h1>
<a href="https://www.ulster.ac.uk/">Go to Ulster University Website</a>
<div>
<h4>Schools</h4>
<ul>
<li>Computing</li>
<li>Nursing</li>
<li>Business</li>
</ul>
</div>
<div>
<h3>Computing Courses</h3>
<ul>
<li>Information Technology</li>
<li>Computer Science</li>
<li>Computer Engineering</li>
</ul>
</div>
<div>
<h3>Important Sites</h3>
<ul>
<li><a href="https://www.ulster.ac.uk/">Ulster University Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/computing-engineering-and-the-built-environment/schools/computing-engineering-intelligent-systems">Computer Science Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/ulster-university-business-school">Business Home</a></li>
</ul>
</div>
*
{
border: 1px solid pink;
}
Uses two or more selectors and chains them together
<h1>Ulster University</h1>
<a href="https://www.ulster.ac.uk/">Go to Ulster University Website</a>
<div>
<h4>Schools</h4>
<ul>
<li>Computing</li>
<li>Nursing</li>
<li>Business</li>
</ul>
</div>
<div>
<h3>Computing Courses</h3>
<ul>
<li>Information Technology</li>
<li>Computer Science</li>
<li>Computer Engineering</li>
</ul>
</div>
<div>
<h3>Important Sites</h3>
<ul>
<li><a href="https://www.ulster.ac.uk/">Ulster University Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/computing-engineering-and-the-built-environment/schools/computing-engineering-intelligent-systems">Computer Science Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/ulster-university-business-school">Business Home</a></li>
</ul>
</div>
li a
{
color: red;
}
Example: Select all of the <a> tags that are in a <li>
Uses two or more selectors and chains them together
ul li a
{
color: red;
background-color: yellow;
}
Select all of the <a> tags that are in a <li> that are in a <ul>
More Examples:
<ul>
<li><a href="https://www.google.com/">Google</a></li>
<li><a href="http://www.ulster.ac.uk">Ulster</a></li>
<li>Plain Text</li>
</ul>
<ol>
<li><a href="https://facebook.com">Facebook</a></li>
<li><a href="https://twitter.com">Twitter</a></li>
</ol>
Uses two or more selectors and chains them together
li.myclass
{
background-color: pink;
}
Select all of the element with a class="myclass"that are in a <li>
More Examples:
<h4 class="myclass">Heading</h4>
<ul>
<li><a href="https://www.google.com/">Google</a></li>
<li class="myclass"><a href="http://www.ulster.ac.uk">Ulster</a></li>
<li class="myclass">Plain Text</li>
</ul>
<ol>
<li><a href="https://facebook.com">Facebook</a></li>
<li class="myclass"><a href="https://twitter.com">Twitter</a></li>
</ol>
Lets us select elements that come after another element
<h1>Ulster University</h1>
<a href="https://www.ulster.ac.uk/">Go to Ulster University Website</a>
<div>
<h4>Schools</h4>
<ul>
<li>Computing</li>
<li>Nursing</li>
<li>Business</li>
</ul>
</div>
<div>
<h3>Computing Courses</h3>
<ul>
<li>Information Technology</li>
<li>Computer Science</li>
<li>Computer Engineering</li>
</ul>
</div>
<div>
<h3>Important Sites</h3>
<ul>
<li><a href="https://www.ulster.ac.uk/">Ulster University Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/computing-engineering-and-the-built-environment/schools/computing-engineering-intelligent-systems">Computer Science Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/ulster-university-business-school">Business Home</a></li>
</ul>
</div>
h3 + ul
{
border: 1px solid blue;
}
Example: Select all of the <ul> tags that come directly after a <h4>
Lets us select elements based of any attribute
<h1>Ulster University</h1>
<a href="https://www.ulster.ac.uk/">Go to Ulster University Website</a>
<div>
<h4>Schools</h4>
<ul>
<li>Computing</li>
<li>Nursing</li>
<li>Business</li>
</ul>
</div>
<div>
<h3>Computing Courses</h3>
<ul>
<li>Information Technology</li>
<li>Computer Science</li>
<li>Computer Engineering</li>
</ul>
</div>
<div>
<h3>Important Sites</h3>
<ul>
<li><a href="https://www.ulster.ac.uk/">Ulster University Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/computing-engineering-and-the-built-environment/schools/computing-engineering-intelligent-systems">Computer Science Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/ulster-university-business-school">Business Home</a></li>
</ul>
</div>
a[href="https://www.ulster.ac.uk/"]
{
background: gold;
color: white;
}
Example: Select <a> tags with a specific href attribute. Make all links to Ulster University Home white on gold background
Lets us select elements based of any attribute
input[type="text"]
{
background: grey;
}
Make all textboxs have a grey background
input[type="email"]
{
color: blue;
}
Make all text in email inputs have blue text
Takes a number n and selects the n'th occurrence of a specific element
<h1>Ulster University</h1>
<a href="https://www.ulster.ac.uk/">Go to Ulster University Website</a>
<div>
<h4>Schools</h4>
<ul>
<li>Computing</li>
<li>Nursing</li>
<li>Business</li>
</ul>
</div>
<div>
<h3>Computing Courses</h3>
<ul>
<li>Information Technology</li>
<li>Computer Science</li>
<li>Computer Engineering</li>
</ul>
</div>
<div>
<h3>Important Sites</h3>
<ul>
<li><a href="https://www.ulster.ac.uk/">Ulster University Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/computing-engineering-and-the-built-environment/schools/computing-engineering-intelligent-systems">Computer Science Home</a></li>
<li><a href="https://www.ulster.ac.uk/faculties/ulster-university-business-school">Business Home</a></li>
</ul>
</div>
li:nth-of-type(3)
{
background: purple;
}
Example: Select the 3'rd <il> tag and give it a purple background
<div>
<h1>Test Heading</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<ul>
</div>
ul
{
color: red;
}
<ul>
<div>
<h1>Test Heading</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<ul>
</div>
div
{
color: red;
}
<div>
<div>
<h1>Test Heading</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<ul>
</div>
div
{
color: red;
}
ul
{
color: blue;
}
<div>
<h1>Test Heading</h1>
<ul>
<li class="important">Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li class="important">Item 4</li>
<ul>
</div>
div
{
color: red;
}
ul
{
color: blue;
}
.important
{
color: green;
}
<div>
<h1>Test Heading</h1>
<ul>
<li id="highlight" class="important">Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li class="important">Item 4</li>
<ul>
</div>
div
{
color: red;
}
ul
{
color: blue;
}
.important
{
color: green;
}
#highlight
{
color: yellow
}
.myclass1{
color: orange;
}
input[type="text"]{
color: orange;
}
div{
color:red
}
div ul{
color:red
}
div ul li{
color:red
}
#myID1{
color: orange;
}
#myimportantID{
color: orange;
}
#myheaderID{
color: orange;
}
Type Selectors
Class & Attribute
Selectors
ID Selectors
More Specific
Linking HTML and CSS
<h1 style="color:blue;">This is a Blue Heading</h1>
Inline:
used to apply a unique style to a single HTML element
Linking HTML and CSS
<!DOCTYPE html>
<html>
<head>
<style>
body
{
background-color: powderblue;
}
h1
{
color: blue;
}
p
{
color: red;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Internal:
used to define a style for a single HTML page
Linking HTML and CSS
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
External:
external style sheet is used to define the style for many HTML pages
body
{
background-color: powderblue;
}
h1
{
color: blue;
}
p
{
color: red;
}
styles.css
Hint 1: Color Codes
Hint 2: You will need to use rgba() at some point to match above exactly
<!DOCTYPE html>
<html>
<head>
<title>Rainbow Colors</title>
<link rel="stylesheet" type="text/css" href="rainbowstyle.css">
</head>
<body>
<h1>Colors of the Rainbow</h1>
<h3 id="violet">Violet</h3>
<h3 id="indigo">Indigo</h3>
<h3 id="blue">Blue</h3>
<h3 id="green">Green</h3>
<h3 id="yellow">Yellow</h3>
<h3 id="orange">Orange</h3>
<h3 id="red">Red</h3>
</body>
</html>
Edit a style.css to produce the following result: