Course: CSCI 1210
More HTML
id
andclass
are important attributes, mainly for styling.id
β typically for unique elements; elements are limited a singleid
class
β typically for general elements.
- Multiple
id
andclass
are space-delimited, such asrounded bold
for rounded & bold. id
andclass
attributes can be accessed through CSS or JS:
/* id="foo" */
#foo {
background: red;
}
const foo = document.getElementById("foo"); // id="foo"
const bar = document.getElementsByClassName("bar"); // class="bar"
Layout
- The layout of a website is essentially the websiteβs structure.
- A website should be intuitive; information should be accessible.
- Each page has a header, navigation bar, content, and footer.
- Within the content, there are different headings, paragraphs, etc.
Layouts make or break websites.
Introduction to CSS
- Through CSS, you can change how websites look
- Including size, color, margins, padding, etc.
- CSS is not uniformly implemented in browsers, mainly Internet Explorer
- Internet Explorer is dead anyways, thereβs no reason to tailor your website for IE
- For example, to make all
h1
elements blue, and set their font family to βHelveticaβ:- For
class
attributes, use.{class}
- For
id
attributes, use#{id}
- For
h1 {
color: blue;
font-family: "Helvetica";
}
Inline Style
- You can use the
style
attribute on elements to add CSS to one specific element - Not recommended for more complex CSS β not syntax highlighted, and annoying to edit
<h1 style="color: red;">Hello</h1>
Embedded Style
- Itβs generally recommended to use External Style Sheets
- You can use CSS within a
style
tag, but it leads to your page being messy
<style>
body {
background: black;
}
</style>
External Style
- External style sheets are particularly helpful for making pages with the same style.
- If youβre using embedded CSS, and you change the color of something, you need to change it for all sites.
- To use an external stylesheet within your page, use a
link
element:
<link rel="stylesheet" href="...">
Style Priority
- Inline Style
- Embedded Style
- External Style
- Browser Default
- Additionally, the selector that is further down the CSS has priority
h1 {
color: blue;
font-size: 2em;
}
h1 {
color: red; /* this overrides the h1 color from blue */
}
Comments
- You can add comments in CSS with the
/* comment */
syntax - Make sure you include this comment for all of your CSS:
/*
Name: Your Name
Course: CSCI 1210
Asssignment: Lab x
Date: mm.dd.yyyy
Description: Learning CSS
*/
Units of Measurements
Absolute Measurements
cm
β centimetersmm
β millimetersin
β inches (96px)px
* β pixels-
- px are relative to the viewing device; for high-resolution screens, 1px implies multiple device pixels
Relative Lengths
em
β relative to font-sizerem
β relative to font-size of the root elementch
β relative to width of β0βvw
β relative to width of the viewportvh
β relative to height of the viewport%
β relative to parent element
Colors
hex
β hex representation- IE:
#RRGGBB
,#RGB
- IE:
rgb
β red, green, blue (0β255)- IE:
rgb(255, 0, 0)
- IE:
rgba
β rgb + alpha/transparency- IE:
rgba(255, 0, 0, 0.5)
- IE:
hsl
β hue, saturation, lightness- IE:
hsl(360, 100%, 50%)
- IE:
hsla
β hsl + alpha- IE:
hsla(360, 100%, 50%, 0.5)
- IE:
currentcolor
β inherits from parent