HTML Css Basic
HTML Css Basic
By: Byte_Builders
HTML Basics
<button> Hello </button> Creates a button with the text "Hello" inside.
<p> paragraph of text </p> Creates a paragraph of text.
HTML Syntax
Syntax = rules for writing HTML code (like grammar in English).
Attributes
Attributes modify how an HTML element behaves.
CSS Basics
One way of writing CSS code is using the <style> HTML element.
<style>
button { background- Modifies all <button> s on the page.
color: red; color:
white; Change background color to red.
Change text color to white.
}
</style>
CSS Syntax
CSS Properties
Here are some common CSS properties we can use:
button {
background-color: Sets the background color. Common values:
red; ● Color name: red, white,
black
● rgb value: rgb( 0 , 150 , 255
);
● Hex value: #0096FF
color: white; Sets the text color. Takes the same values as
background-color (color name, rgb, hex).
height : 36px; Sets the height. Common values:
● Pixel value: 36px
● Percentage: 50%
width : 105px ; Sets the width. Takes the same values as
height.
border : none ; Removes the border.
border-radius : 2px ; Creates rounded corners.
cursor : pointer; Changes the mouse/cursor when hovering over the
element.
border-color : red ; Sets the border color.
border-style : solid Sets the border style. Common values:
; ● solid
● dotted
● dashed
border-width : 1px ; Sets the border width.
}
CSS Values
Each CSS property has a set of values that are allowed ( background-color allows color values,
cursor allows solid, dotted, dashed , etc.)
Color Values
1. A color name: red, white, black
● Each character in Hex is base 16, which means it can have a value of 0, 1, 2, ... 8, 9, A, B, C, D, E, F
(16 possible values).
● Using the first 2 characters, we can have 16 * 16 = 256 possible values from 0 - 255:
00 = 0
01 = 1
...
0F = 15 10
= 16
11 = (1 * 16) + 1 = 17
...
FF = (15 * 16) + 15 = 255
● This is the same range as RGB (0 to 255), so the first 2 characters in Hex are used to represent
red, the second 2 characters represent green, and the third 2 characters represent blue. Usually,
it's easier to use a Hex to RGB calculator to convert.
Measurement Values
1. Pixels: 50px, 100px
Pixels (px) are a common unit of measurement in the digital world. For
example: a 4K screen is 3840px by 2160px.
2. Percent: 50%, 100%
A relative measurement. For example, width : 50% ; means 50% of the width of the
page (or if the element is inside another element, 50% of the width of the container element).
<button class = "youtube-button"> Multiple elements can have the same class
SUBSCRIBE
</button>
<button class = "youtube-button">
JOIN
</button>
.youtube-button {
...
}
.subscribe-button {
...
}
CSS Pseudo-Classes
.subscribe-button:hover These styles only apply when hovering over an element
{ ... } with class= "subscribe-button"
Chrome DevTools
Lets us view (and modify) the HTML and CSS of a website directly in the browser.
To open the DevTools: right-click > Inspect.
CSS Box Model
● Determines how much space an element takes up.
● Determines how far away elements are from each other.
.join-button {
margin-right : 10px ; Add 10px of space on the outside of the element.
margin-left: 10px ;
margin-top : 10px;
margin-bottom: 10px ; Normal margin pushes things away from an element.
margin-right : -20px; Negative margin pulls things towards an element like this:
● <span> is the most generic text element (it doesn't have any default styles).
.shop-link { text-
decoration: underline ;
}
The HTML Structure
<!DOCTYPE html> Tells the browser to use a modern version of HTML.
< html>
<head> <head> contains everything that's not visible like the title and description
... (a.k.a. metadata) as well as links to fonts and CSS stylesheets.
</head>
<body> <body> contains everything that's visible like buttons, text, images, etc.
...
</body>
</html>
<link rel = "stylesheet" href= "styles.css" > Loads a CSS file to the page
</head>
Filepaths
href= "styles.css" Looks for a file called styles.css beside the HTML file.
href="fold1/styles.css" Looks for a folder called fold1 beside the HTML file,
then goes into the folder and looks for styles.css .
href= "fold1/fold2/styles.css" Go into fold1 , go into fold2 , look for
styles.css
Images
<img src = "image.png"> Loads an image image.png beside the HTML file.
<img src ="pics/image.png" > Loads image.png in the pics folder.
< img class = "image" src = "pics/image.png" >
.image {
width : 300px ; Resizes the image to a width of 300px . Height will also
resize to keep the image's dimensions.
height : 300px ; If both width and height are set, the image may stretch.
object-fit: cover ; Enlarges the image to cover the entire width * height
area without stretching or distorting.
object-fit : contain; Shrinks the image so that it's contained in the width *
height area.
object-position : left ; Determines where the image is positioned in the width *
object-position : right ; height area.
object-position : top ;
object-position: bottom;
}
Inputs
<input type = "text" > Creates a text box.
<input type = "text" placeholder = Add a placeholder (a label) to the text box.
"Search" >
<input type = "checkbox" > Creates a checkbox.
<input class= "search-bar" type= "text">
.search-bar {
font-size : 30px ; Changes the font-size when typing into the text box.
}
.search-bar::placeholder {
font-size : 30px ; Changes the font-size of the placeholder.
}
<div> Element
<div> is a container. We generally put other elements (including other <div> s) inside (nesting).
<div class = "container"> <div> s allow us to group elements together and create
<p> Name </p> more complex layouts.
<input type= "text">
</div>
<div class= "container" >
<p> Quantity </p>
<div>
<button>1 </button>
<button>2 </button>
</div>
<button> Submit </button>
</div>
.container { display:
inline-block ;
width : 200px ;
}
Nested Layouts Technique
There are 2 types of layouts:
CSS Grid
.grid {
display : grid ; Turns an element into a grid container.
grid-template-columns: 100px 100px; Sets how many columns are in the grid and
how wide the columns are.
column-gap : 20px ; Sets space between the columns.
row-gap : 40px ; Sets space between the rows.
}
.grid {
display :
grid ;
grid-template-columns: 100px 1fr; 1fr = the column will take up the remaining
amount of space in the grid container.
grid-template-columns : 1fr 1fr; The columns will take up an equal amount of the
remaining space (since they're both 1fr ).
grid-template-columns: 1fr 2fr; The number in front of fr = relatively how much
space the column gets. Here, the 2nd column gets
twice the amount of space as the 1st.
Flexbox
.flexbox {
display: flex; Turns an element into a flexbox container.
flex-direction: row ; Lays out elements horizontally inside the flexbox. Usually we
don't need to specify flex-direction : row; because
it is the default value.
justify-content : center ; Centers the elements in the flexbox horizontally.
justify-content : space-between; Spreads out the elements in the flexbox evenly
across the horizontal space.
.element-inside-flexbox {
width : 100px ; Sets the width of the flexbox element to 100px .
flex : 1 ; Take up the remaining amount of space. The value 1
determines relatively how much space.
flex-direction: column;
.flexbox {
display: flex ; flex- Lays out elements vertically inside the flexbox. Also,
direction: column; justify-content and align-items are reversed.
.element {
position : static ; This is the default value that every element starts with.
} position : static ; causes the element to display normally.
Position Fixed
.fixed { Positions the element in the browser window (sticks to the page while
position : scrolling).
fixed ;
top : 0 ; Places the element 0px from the top of the browser window.
bottom : 10px; 10px from the bottom of the browser window.
left : 50px ; 50px from the left of the browser window.
right : 100px ; 100px from the right of the browser window.
● If you set opposite directions ( top / bottom or left /
right ) , the element will stretch .
top: -5px; Using negative pixels places the element beyond the top edge.
Sets the element's width to 100px .
width : 100px;
height : 100px; Sets the element's height to 100px .
} ● When using width / height the element will not resize
with the page.
● When using top / bottom / left / right the
element will resize with the page.
Position Absolute
.absolute { Positions the element on the page (it will scroll with the page and
position: will not stick when scrolling).
absolute;
top : 0 ; Places the element 0px from the top of the page.
bottom : 10px ; 10px from the bottom of the page.
left : 50px ; 50px from the left of the page.
right : 100px; 100px from the right of the page.
Position Relative
.relative { The element will appear normally (as if it's position: static ).
position: We can then push it around with top / bottom / left /
relative; right .
top : 10px; Places the element 10px from the top of its original position
(pushes it down by 10px ) . Unlike margin , it won't push the
rest of the page down.
bottom : 10px ; Places the element 10px from the bottom of its original position
(pushes it up by 10px ).
left : 50px; Places the element 50px from the left of its original position.
right: 100px; Places the element 100px from the right of its original position.
.fixed { position : fixed ; This element will appear in front of the position :
absolute ; z-index : 2 ; element because it has a higher z-index .
}
.absolute {
position :
absolute ; z-index
: 1 ;
}
.static { This element will appear at the back since it's position : static . position
: static ;
}
Responsive Design
Responsive design = making the website look good on any screen size.
@media ( max-width: 750px ) { Only apply the CSS code below when screen width
.element { is between 0px - 750px. width : 350px;
}
}
@media ( min-width: 750.02px ) and ( max-width :
1000px) { .element {
width : 450px; Only apply this CSS code when screen width is
} between 750px - 1000px.
}
@media ( min-width : 1000.02px Only apply this CSS code when the screen width is
) { over 1000px.
.element {
width: 600px;
}
}
We generally use a gap of .02px between the ranges (like above) because the browser can support
fractional screen widths like 750.50px .
Advanced CSS Selectors
With Comma
.class1 , .class2 { ... } Target multiple classes at the same time.
.class1 , p { ... } Target a class and all <p> s at the same time.
With Space
.class1 img { ... } Target <img> s that are inside elements with
class = "class1"
.class1 img , Target <img> s that are inside elements with
.class2 .tooltip { ... }
class= "class1" AND .tooltip inside elements
with class = "class2" .
.class2:hover .tooltip { ... } Target .tooltip only when hovering over elements
with class= "class2" .
For a full list of selectors, check out CSS Selectors .
Inheritance
A text property set on the outer element will be passed down into inner elements:
<div style = "color: red;" >
<p> Paragraph </p> This paragraph will have red text.
</div>
For global text styles (styles we want on the entire page), we can set them on the <body>:
body { font-family: Roboto, Arial; All elements on the page by default
will use color : rgb( 20, 20 , 20 ); font-family : Roboto ,
Arial and color:
} rgb( 20 , 20 , 20 ). This can be overridden.
CSS Specificity
If multiple CSS selectors change the same property on the same element (see example below), CSS
Specificity determines which selector "wins" (which style gets applied).
body { color: <p class = "title"> Paragraph of text.
black ; } p { color </p>
: red ; } .title { </body>
color: green ; } In this example, the .title selector has the highest priority
(according to CSS Specificity) so the text will be green.
<body>
CSS Specificity Rules
Here's the full set of CSS Specificity Rules ( you don't need to memorise all of these).
Usually, you just need to know a few useful rules and search Google for more if needed:
1. Inline CSS has higher priority than .class selectors.
2. .class selectors have higher priority than element name selectors ( p ).
3. Element name selectors ( p ) have higher priority than inheritance (from body ).
4. If 2 selectors have the same priority, the one that is written later wins.
Semantic Elements
Elements that work the same way as <div> . However, they also give the HTML meaning when
screen readers, search engines, or other devices read the website.
Here's a list of Semantic Elements . They'll be covered in more detail in the accessibility course.
Comments
Let us write code that the browser ignores. Useful for documenting how the code works.
<!-- This is a comment --> Syntax for a comment in HTML: <!-- ... -->
<p class = "title" >
Paragraph of text. </p>
.tooltip {
pointer-events : none; Disables all interactions with the mouse (clicks, hovers, etc.)
white-space: nowrap; Prevents the text inside the element from wrapping to multiple
} lines.