HTML and CSS NOtes
HTML and CSS NOtes
HTML (HyperText Markup Language) is the standard language for creating web
pages and web applications.
It is a markup language used to structure content on the web. HTML elements are the
building blocks of HTML pages and are represented by tags.
Structure of HTML
An HTML document has a basic structure, which includes the following components:
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Your Title Here</title>
</head>
<body>
<h1>Your Heading Here</h1>
<p>Your paragraph here.</p>
</body>
</html>
HTML provides six levels of headings, from <h1> to <h6>, with <h1> being the
largest and <h6> being the smallest.
<h1>This is a Heading</h1>
<h2>This is a Subheading</h2>
html
Copy code
<p>This is a paragraph.</p>
Comments in HTML
Comments can be added to HTML documents using <!-- comment text -->. These
comments are not displayed in the browser and are used for documentation purposes.
Lorem Ipsum is a placeholder text used in web design and publishing to fill a space
on a page and give an impression of how the final text will look.
It is often used in the design phase of projects when actual text is not yet available.
Lists in HTML
html
Copy code
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
html
Copy code
<ol>
<li>First item</li>
<li>Second item</li>
</ol>
Images in HTML
Images can be added using the <img> tag, with the src attribute to specify the image
source and the alt attribute for alternative text.
html
Copy code
<img src="image.jpg" alt="Description of the image">
Links are created using the <a> (anchor) tag, with the href attribute to define the
URL.
You can create links to other web pages, email addresses, or even to specific parts of
the same page.
HTML forms are used to collect user input. The <form> tag wraps all form elements.
Common input types include:
o Text fields: <input type="text">
o Password fields: <input type="password">
o Checkboxes: <input type="checkbox">
o Radio buttons: <input type="radio">
o Submit button: <input type="submit">
<video controls>
<source src="movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
Semantic HTML
Semantic HTML refers to using HTML markup that conveys meaning about the
content. Semantic elements clearly describe their meaning to both the browser and the
developer.
o Examples include:
<header>: Defines a header for a document or section.
<footer>: Defines a footer for a document or section.
<article>: Represents a self-contained composition.
<section>: Represents a thematic grouping of content.
<article>
<header>
<h1>Article Title</h1>
</header>
<p>This is the content of the article.</p>
<footer>Published on date</footer>
</article>
1. What Is CSS
Definition
Key Features
Definition: Styles are applied directly to an HTML element using the style attribute.
Example:
html
Copy code
<h1 style="color: blue; font-size: 20px;">Hello World</h1>
2. Internal CSS
Definition: Styles are defined within a <style> tag inside the <head> section of an
HTML document.
Example:
html
Copy code
<head>
<style>
h1 {
color: blue;
font-size: 20px;
}
</style>
</head>
Pros: Useful for styling a single document, maintains separation of content and styles.
Cons: Does not apply across multiple pages.
3. External CSS
Definition: Styles are written in a separate .css file linked to the HTML document
using a <link> tag.
Example:
html
Copy code
<link rel="stylesheet" href="styles.css">
Pros: Easy to maintain, applies styles across multiple pages, better for organization.
Cons: Requires an additional HTTP request.
1. Named Colors: Basic color names recognized by browsers (e.g., red, blue).
2. Hexadecimal: Color values represented in hex format (e.g., #ff0000 for red).
3. RGB: Uses the RGB color model (e.g., rgb(255, 0, 0)).
4. RGBA: RGB with an alpha channel for opacity (e.g., rgba(255, 0, 0, 0.5)).
5. HSL: Hue, Saturation, Lightness (e.g., hsl(0, 100%, 50%)).
6. HSLA: HSL with alpha for opacity (e.g., hsla(0, 100%, 50%, 0.5)).
Color Functions
Color Theory
Relative Units
Choosing Units
Web Fonts
Use services like Google Fonts or Adobe Fonts to include a variety of fonts.
@font-face Rule: Load custom fonts.
css
Copy code
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
Font Stacks
Define fallback fonts to ensure text remains readable if the preferred font isn’t
available.
css
Copy code
font-family: 'CustomFont', 'Arial', sans-serif;
Box Sizing
Default Box Model: Width and height apply only to the content area.
box-sizing: border-box;: Width and height include padding and border.
css
Copy code
* {
box-sizing: border-box;
}
Visual Representation
Shorthand Property
css
Copy code
background: blue url('image.jpg') no-repeat center/cover;
css
Copy code
background: linear-gradient(to right, red, yellow);
css
Copy code
background: radial-gradient(circle, red, yellow);
css
Copy code
background: repeating-linear-gradient(45deg, red, yellow 10%, blue
20%);
Gradient Functions
css
Copy code
background: linear-gradient(45deg, red, yellow);
Example Usage
css
Copy code
img {
filter: blur(5px) brightness(80%);
}
1. Attribute Selectors:
o Select elements based on attributes.
css
Copy code
/* Select elements with a specific attribute */
input[type="text"] {
border: 1px solid blue;
}
2. Child Combinator: Selects elements that are direct children of a specified element.
css
Copy code
div > p {
color: green;
}
3. General Sibling Combinator: Selects all siblings after the specified element.
css
Copy code
h1 ~ p {
color: red;
}
css
Copy code
li:nth-child(2) {
color: blue; /* Selects the second list item */
}
1. Static: Default positioning; elements are positioned in the order they appear in the
document flow.
2. Relative: Positioned relative to its normal position.
css
Copy code
position: relative;
top: 10px; /* Moves the element down 10px from its original position
*/
css
Copy code
position: absolute;
top: 20px; /* Moves the element 20px from the top of its positioned
ancestor */
css
Copy code
position: fixed;
top: 0; /* Stays at the top of the viewport */
5. Sticky: Hybrid between relative and fixed; behaves like relative until a certain scroll
position, then behaves like fixed.
css
Copy code
position: sticky;
top: 0; /* Sticks to the top of the viewport when scrolling */
1. Container Properties:
o display: flex;: Activates flexbox on the container.
o flex-direction: Defines the direction of flex items (row, column).
o justify-content: Aligns items horizontally (flex-start, center, space-
between).
o align-items: Aligns items vertically (flex-start, center, stretch).
2. Item Properties:
o flex-grow: Defines how much a flex item will grow relative to others.
o flex-shrink: Defines how much a flex item will shrink relative to others.
o flex-basis: Defines the default size of a flex item.
Example Usage
css
Copy code
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.item {
flex: 1; /* Each item takes up equal space */
}
1. Container Properties:
o display: grid;: Activates grid layout.
o grid-template-columns: Defines the number and size of columns.
o grid-template-rows: Defines the number and size of rows.
o grid-gap: Sets the space between grid items.
2. Item Properties:
o grid-column: Specifies the starting and ending column.
o grid-row: Specifies the starting and ending row.
o grid-area: Assigns a name to a grid item.
Example Usage
css
Copy code
.container {
display: grid;
grid-template-columns: 1fr 2fr; /* Two columns */
grid-gap: 10px; /* Space between items */
}
.item {
grid-column: 1 / 3; /* Spans two columns */
}
Example Usage
css
Copy code
.box {
transition: background-color 0.5s ease-in-out;
}
.box:hover {
background-color: blue; /* Changes color on hover */
}
Example Usage
css
Copy code
@keyframes slide {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
.box {
animation: slide 2s infinite; /* Slides the box */
}
Animation Properties
Media Queries allow you to apply styles based on the viewport's characteristics, such
as width, height, orientation, and resolution.
Example Usage
css
Copy code
/* Styles for devices wider than 600px */
@media (min-width: 600px) {
body {
background-color: lightblue;
}
}
Best Practices
Use mobile-first design by starting with styles for small screens and using min-width
for larger devices.
Combine multiple media queries for specific breakpoints.
css
Copy code
img {
max-width: 100%;
height: auto;
}
Frameworks
CSS Preprocessors enhance CSS with variables, nesting, mixins, and functions,
making it more maintainable and scalable.
Features
1. Variables: Store values (e.g., colors, fonts) to reuse throughout the stylesheet.
$primary-color: blue;
body {
color: $primary-color;
}
.nav {
ul {
list-style: none;
}
}
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
.container {
@include flex-center;
}
Choosing a Preprocessor
Choose based on your project needs, team familiarity, and community support.
CSS Variables allow you to define custom properties that can be reused throughout
the stylesheet.
Example Usage
css
Copy code
:root {
--main-color: blue;
}
.button {
background-color: var(--main-color);
}
Advantages