Copy of CSS Basics
Copy of CSS Basics
● CSS selectors are used to "find" (or select) the HTML elements you want to style.
● There are several different types of selectors in CSS.
○ Element Selector
○ Id Selector:
○ Class Selector
○ Universal Selector
○ Group Selector
CSS Selector: Element Selector
● The id selector uses the id attribute of an HTML element to select a specific element.
● Id selector is used to select one unique element
● To select an element with a specific id, write a hash (#) character, followed by the id of
the element.
Eg: HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
CSS Selector: Class Selector
● The universal selector (*) selects all HTML elements on the page.
*{
text-align: center;
color: blue;
}
CSS Selector: Group Selector
● The grouping selector selects all the HTML elements with the same style definitions.
Eg: The h1, h2, and p elements have the same style definitions
h1, h2, p {
text-align: center;
color: red;
}
● To group selectors, separate each selector with a comma.
Attribute Selectors
● Used to style HTML elements that have specific attributes or attribute values.
● Eg:
1. selects all <a> elements with a target attribute:
a[target] {
background-color: yellow;
}
● It facilitates you to change the look of the entire web site by changing just one file.
● File extension must be .css
Eg: mystyle.css
● Each HTML page must include a reference to the external style sheet file inside the
<link> element, inside the head section.
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
● External style sheet can be written in any text editor
● The external .css file should not contain any HTML tags.
● If some properties have been defined for the same selector (element) in different style
sheets, the value from the last read style sheet will be used.
Cascading Order
● All the styles in a page will "cascade" into a new "virtual" style sheet by the following
rules, where number one has the highest priority:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
● So, an inline style has the highest priority, and will override external and internal styles
and browser defaults.
CSS Comments
p {
color: red;
}
CSS - Measurement Units
Unit Description Example
% Defines a measurement as a percentage relative to another value, p { width: 50%;}
typically an enclosing element.
Short Hex Code A shorter form of the six-digit notation. Each digit is replicated to arrive at an equivalent #RGB p{color:#6A7;}
six-digit value.
RGB % Specified using the rgb( ) property. This property takes three values, one each for red, rgb(rrr%,ggg p{color:rgb(50%,5
green, and blue. %,bbb%) 0%,50%);}
RGB Absolute RGB can be an integer between 0 and 255 or a percentage. rgb(rrr,ggg,bbb) p{color:rgb(0,0,25
5);}
● Used to style the dynamic states of an element such as hover, active and focus state.
● Helpful in the situations
○ Style an element when a user mouses over it
○ Style visited and unvisited links differently
○ Style an element when it gets focus
● A pseudo-class starts with a colon (:). Its syntax can be given with:
selector:pseudo-class { property: value; }
Eg: a:link {
color: blue;
}
a:visited {
text-decoration: none;
}
ul li:last-child { border-right: none; }
● Eg: https://www.w3schools.com/css/tryit.asp?filename=trycss_link
CSS Pseudo-elements
● Used to style the elements or parts of the elements without adding any IDs or classes to
them.
● Helpful in the situations
○ Style the first letter, or line, of an element
○ Insert content before, or after, the content of an element
● double-colon (::) syntax for pseudo-elements to distinguish between them and pseudo-
classes.
selector::pseudo-element { property: value; }
Eg: p::first-line {
color: #ff0000;
font-variant: small-caps;
}
● Try: https://www.w3schools.com/css/tryit.asp?filename=trycss_before
Media Queries
● The result of the query is true if the specified media type matches the type of device the
document is being displayed on and all expressions in the media query are true.
● When a media query is true, the corresponding style sheet or style rules are applied,
following the normal cascading rules.
● Unless you use the not or only operators, the media type is optional and the all type will
be implied.
● You can also have different stylesheets for different media:
<link rel="stylesheet" media="mediatype and|not|only (expressions)" href="print.css">
CSS3 Media Types
Value Description
all Used for all media type devices
speech Used for screen readers that "reads" the page out loud
● Eg: changes the background-color to light green if the viewport is 480 pixels wide or
wider
@media screen and (min-width: 480px) {
body {
background-color: lightgreen;
}
}
● Try: https://www.w3schools.com/css/tryit.asp?filename=trycss3_media_queries1
Responsive Web Design
● Web pages can be viewed using many different devices: desktops, tablets, and phones.
● Web page should look good, and be easy to use, regardless of the device.
● Web pages should not leave out information to fit smaller devices, but rather adapt its
content to fit any device:
● It is called responsive web design when you use CSS and HTML to resize, hide, shrink,
enlarge, or move the content to make it look good on any screen.
Desktop Tablet Phone
Responsive Web Design - Viewport
● The viewport is the user's visible area of a web page.
● The viewport varies with the device, and will be smaller on a mobile phone than on a computer
screen
● When we started surfing the internet using tablets and mobile phones, fixed size web pages
were too large to fit the viewport. To fix this, browsers on those devices scaled down the entire
web page to fit the screen.
● Include the following <meta> viewport element in all your web pages:
● <meta name="viewport" content="width=device-width, initial-scale=1.0">
● This gives the browser instructions on how to control the page's dimensions and scaling.
● The width=device-width part sets the width of the page to follow the screen-width of the device
(which will vary depending on the device).
● The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.
Responsive Web Design - Grid-View
● Many web pages are based on a grid-view, which means that the page is divided into
columns
● A responsive grid-view often has 12 columns, and has a total width of 100%, and will
shrink and expand as you resize the browser window.
● Eg: https://www.w3schools.com/css/tryit.asp?filename=tryresponsive_webpage
CSS Animations
● CSS saves time - You can write CSS once and then reuse same sheet in multiple HTML
pages.
● Pages load faster - If you are using CSS, you do not need to write HTML tag attributes
every time.
● Easy maintenance - To make a global change, simply change the style, and all
elements in all the web pages will be updated automatically.
● Superior styles to HTML -Give a far better look to your HTML page in comparison to
HTML attributes.
● Multiple Device Compatibility - Style sheets allow content to be optimized for more than
one type of device.
Thank you