3 CSS
3 CSS
▪ Example:
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style
Simple elements
(select selectors based on name, id, class)
Combinator selectors
Simple selectors based
Pseudo-class selectors
(select elements based
(select elements based (select elements based
on a specific relationship state
on name, id, class) on a certain state)
between them)
Pseudo-elements
(select and style aselectors
part of an element) Attribute
(select elements
selectorsbased on an attribute or attribute value)
https://www.w3schools.com/css/css_selectors.asp
How to apply CSS in HTML documents?
Internal External
Inline CSS
CSS CSS
https://www.w3schools.com/css/css_howto.asp
Inline CSS
▪ Style sheet appears as the value of the style attribute to apply a
unique style for a single element
▪ General form:
style = "property_1: value_1; property_2: value_2;
… property_n: value_n“
▪ Example:
<ul style = "list-style-type: square" >
<li> Cessna Skyhawk </li>
<li> Beechcraft Bonanza </li>
<li> Piper Cherokee </li>
</ul>
Internal CSS
<!DOCTYPE html>
<html>
▪ Style sheet appears as a list of <head>
<style>
rules that are the content of a body {
<style> tag, inside the <head> background-color: linen;
}
of HTML
h1 {
▪ General form: color: maroon;
<style> margin-left: 40px;
rule list }
</style>
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
External CSS
▪ External style sheets are in separate files, potentially on any server on
the Internet
▪ Written as text files with the MIME type text/css
▪ A <link> tag is used to specify that the browser is to fetch and use an
external style sheet file
<head>
<link rel="stylesheet" href="http://www.wherever.org/termpaper.css">
</head>
https://www.w3schools.com/css/css_colors.asp
CSS Properties
Background https://www.w3schools.com/css/css_background.asp
Control Border https://www.w3schools.com/css/css_border.asp
many Margin https://www.w3schools.com/css/css_margin.asp
https://www.w3schools.com/html/html_layout.asp
https://www.w3schools.com/css/css_templates.asp
Flexbox vs
Grid
CSS Coding Standards: Case Study
▪ WordPress
❑ https://developer.wordpress.org/coding-standards/wordpress-coding-
standards/css/
▪ Google
❑ https://google.github.io/styleguide/htmlcssguide.html
Responsive Web Design Layout
<div class="header">
<h1>My Website</h1>
<p>A website created by me.</p>
</div>
Header
<div class="navbar">
Navigation Bar <a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#" class="right">Link</a>
</div>
<div class="row">
Side Content Main Content
<div class="side">...</div>
<div class="main">...</div>
</div>
<div class="footer">
<h2>Footer</h2>
Footer </div>
https://www.w3schools.com/howto/howto_make_a_website.asp
CSS box-sizing Property
▪ The box-sizing property allows us to include the padding and border
in an element's total width and height
▪ If you set box-sizing: border-box; on an element, padding and border
are included in the width and height
▪ Example: .div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
box-sizing: border-box;
https://www.w3schools.com/css/css3_box-sizing.asp
}
.div2 {
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
box-sizing: border-box;
}
CSS Media Queries
▪ The @media rule, introduced in CSS2, made it possible to define
different style rules for different media types
▪ Syntax
@media not|only mediatype and (expressions) {
CSS-Code;
}
▪ Example
@media screen and (min-width: 480px) { The media query will only apply if
body { the media type is screen and the
background-color: lightgreen; viewport is 480px wide or wider.
}
}
https://www.w3schools.com/css/css3_mediaqueries.asp
Make Responsive Layout
/* Responsive layout - when the screen is less than 700px wide, make the two columns
stack on top of each other instead of next to each other */
@media screen and (max-width: 700px) {
.row {
flex-direction: column;
}
}
/* Responsive layout - when the screen is less than 400px wide, make the navigation
links stack on top of each other instead of next to each other */
@media screen and (max-width: 400px) {
.navbar a {
float: none;
width: 100%;
}
} https://www.w3schools.com/css/css3_flexbox_responsive.asp
Questions
▪ What is the difference between HTML and CSS?
▪ What are the advantages and disadvantages of CSS?
▪ What is MIME type and give some examples of common MIME types.
▪ Which level of style sheet has highest precedence and explain how it affects the
document.
▪ Explain what kind of conflict may occur when there are two different values for
the same property on the same element. How does the browser solve it?
▪ What is CSS selector? What is the difference between id and class attribute
selectors? How to apply styles to the selector of these attributes?
▪ How to apply styles to all elements in a document?
▪ What are pseudo classes?
▪ Identify the available CSS properties in different categories with their function.
How to style <dt> and <dd> so they are on the
same line?
https://stackoverflow.com/questions/1713048/how-to-style-dt-and-dd-so-they-are-on-the-same-line
Solutions:
1) Using float
2) Without
float
3) Using
flexbox
*Note:
The flex property is a shorthand
property for the flex-grow, flex-
shrink, and flex-basis properties.
https://www.w3schools.com/css/css3_flexbox_items.asp
4) Using CSS
Grid
5) Using
Booststrap
To Speed up: CSS Frameworks
▪ Bootstrap
▪ Animate.css
▪ Foundation
▪ Ulkit
▪ Pure CSS
▪ etc