Adding internal CSS to your webpage
<!DOCTYPE html>
<html>
<head>
<title>You Are Here</title>
<style>
/* This is where you would put your CSS */
/* Inside the style tags */
p {
color: blue;
}
</style>
</head>
<body>
<p>This text should be blue.</p>
</body>
</html>Internal styles are not the best practice for the following reasons:
Maintenance: If you have multiple pages using the same styles, making changes to those styles becomes difficult as you will have to make the changes on every page.
Reusability: Internal styles cannot be reused on other pages or websites, making it difficult to maintain consistency across multiple pages and sites.
File size: Internal styles increase the size of the HTML file, making it slow to load and increasing the bandwidth required to load the page.
Separation of concerns: It is considered best practice to separate the HTML, CSS, and JavaScript code, making it easier to maintain and modify each piece of code individually.
Adding Inline Styles with the style attribute
<!DOCTYPE html>
<html>
<head>
<title>You Are Here</title>
</head>
<body>
<h1 style="color: blue;">Hello World!</h1>
<p style="font-size: 20px;">This is an example of inline styles in HTML.</p>
</body>
</html>Adding CSS StyleSheet file to your webpage
(optimal way)
<!DOCTYPE html>
<html>
<head>
<title>You Are Here</title>
<link rel="stylesheet" href="path/to/styles.css">
</head>
<body>
<p class="message">Press <code>alt+command+i</code> Mac /
<code>ctrl+shift+i</code> Windows to open your Dev Tools.
Make sure the CSS styles are applied correctly.</p>
</body>
</html>p{
color:red
}CSS file
HTML file
// STEP 1 Target
<button id="button">SAVE</button>
// STEP 2 Target and adjust Styles
#button {
background-color: blue;
color: white;
padding: 10px 20px;
border-radius: 5px;
}CSS Selectors
Save
button {
}
#myId {
}
.myClass {
}
[type="checkbox"] {
}Element Selector: Selects elements based on their HTML tag name, such as "p" for paragraphs, "h1" for headings, etc.
Element Selectors
p {
color: blue;
font-size: 16px;
}Pros of Element Selector:
Cons of Element Selector:
Best practices:
div, body, or html
<p class="message">This is a paragraph with a class selector.</p>CSS
.message {
background-color: blue;
color: white;
padding: 10px 20px;
border-radius: 5px;
}Text
<p id="message">This is a paragraph with a class selector.</p>CSS
#message {
background-color: blue;
color: white;
padding: 10px 20px;
border-radius: 5px;
}
:hover - used to select and style an element when a user hovers over it with a mouse.:active - used to select and style an element when it is being actively clicked.:focus - used to select and style an element that has focus, such as a form input field.:first-child - used to select and style the first child element within a parent element.:last-child - used to select and style the last child element within a parent element.:hover - used to select and style an element when a user hovers over it with a mouse.
<style>
h1:hover {
background-color: yellow;
}
</style>:active - used to select and style an element when it is being actively clicked.
<style>
a:active {
color: red;
font-weight: bold;
}
</style>
<a href="#">Click me!</a>:focus - used to select and style an element that has focus, such as a form input field.
<style>
input[type="text"]:focus {
background-color: lightblue;
outline: none;
}
</style>
<label for="name">Name:</label>
<input type="text" id="name">
:first-child - used to select and style the first child element within a parent element
<html>
<head>
<style>
li:first-child {
font-weight: bold;
}
</style>
</head>
<body>
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ul>
</body>
</html>
The specificity of a style rule is calculated based on the number of each type of selector in the rule.Inline styles have the highest specificity, as they are directly applied to an element.
ID selectors have a higher specificity than class selectors and pseudo-class selectors.
Class selectors and pseudo-class selectors have a higher specificity than element selectors. A class selector is denoted with a "." symbol.
Element selectors have the lowest specificity. They select elements based on the HTML tag name.
When two or more styles have the same specificity, the last style declared in the CSS wins.
<h3 style='color: red'>Hello</h3><h3 id="sub-header">Hello</h3> <h3 class="hello-header">Hello</h3> <p>This is an element selector</p><div class="container">
<p class="text">Hello World</p>
<p id="text">Goodbye World</p>
</div>
.text {
color: blue;
}
#text {
color: red;
}In this example, both the .text and #text selectors are targeting the same element, which is the second p tag. However, the #text selector has higher specificity than the .text selector. This means that the text color of the second p tag will be red, because the styles defined in the #text selector will override the styles defined in the .text selector.
In CSS, the !important rule is used to increase the specificity of a declaration and to override any conflicting styles from other stylesheets or inline styles. When a declaration is marked with !important, it takes precedence over any other conflicting styles, regardless of their specificity.
Each square takes up the full width of its line.
.container {
display:flex
}Setting up a container
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>.container {
display: flex;
align-items: center;
justify-content: center;
height: 300px;
background-color: lightgray;
}
.item {
background-color: steelblue;
color: white;
padding: 20px;
margin: 10px;
}Flexbox is a layout system in CSS that provides a flexible way to arrange elements within a container.
Flexbox allows us to put elements inside of a container, and have that container decide how to shrink/expand/wrap those elements to fill the space available. It also allows us to specify how the items are arranged and ordered.
1. The main axis is the primary axis along which items are laid out. It runs either horizontally (left to right)
2. The cross axis runs perpendicular to the main axis, and is used to align the items along this axis
order
flex
flex-grow
flex-shrink
align-self
justify-content: flex-start | flex-end | center | space-between | space-around
Specifies how items are placed in flex- container, defining the main axis and its direction.
flex-direction: column this will switch the main and cross axisallows us to align items along the cross axis (see terminology diagram above). This allows content to be positioned in many different ways using justify content and align items together
align-self: allows you to adjust the alignment of a single element.
It has all the same properties of align-items
Flex-grow allows an item to grow relative to the rest of the items in the same container, filling up any remaining space.
specifies whether items are forced into
a single line OR can be wrapped into multiple lines.
flex-wrap is the property that deals with the flex items when space in the container isn’t big enough to fit them all