Getting Started with HTML5
A beginner's guide to HTML5 and why it's important for modern web development.
Read MoreHTML is more than just tags and attributes—it's the foundation of the web. Learning to craft efficient, clean, and advanced HTML code sets you apart as a pro developer.
Using semantic tags like <header>
,
<article>
, and <footer>
not only
improves the structure of your document but also enhances SEO and
accessibility.
Properly indent your code and avoid redundant tags. Keep your code DRY (Don't Repeat Yourself) and refactor regularly to maintain clarity and performance.
Use media queries to ensure your website looks stunning on all devices. Start by designing for mobile and then add styles for larger screens.
Simple animations like hover effects or background transitions can enhance user experience and engagement. Don't overdo it, though—keep it subtle and elegant.
Use ::before
and ::after
pseudo-elements to
add extra content, design elements, or visual effects without
cluttering your HTML.
Sign up for our advanced HTML newsletter to receive weekly tips and tricks on professional web development.
Subscribe Now"Learning HTML has never been easier thanks to these incredible tips. I've improved my web development workflow 10x!"
- Alex Johnson
"I used to think HTML was simple. Now, with these techniques, I understand how to build robust and professional websites."
- Lisa Simpson
<img src="image.jpg" alt="A responsive image" style="max-width: 100%; height: auto;">
<header> <nav> <section> <footer>
HTML (Hypertext Markup Language) is the standard markup language used to create web pages. It structures web content and connects different resources such as images, videos, and links.
HTML is the foundation of web development. Understanding it allows you to build websites from scratch, which is essential for any web developer or designer.
HTML5 introduced new elements that help structure content better,
such as <article>
, <aside>
,
<header>
, and <footer>
.
A beginner's guide to HTML5 and why it's important for modern web development.
Read MoreExplore advanced HTML coding practices for clean, professional code.
Read MoreLearn how to create and style complex forms using HTML and CSS.
Read MoreTry out your HTML code live in our sandbox environment. See instant results as you code!
Try it NowEnsure your HTML is error-free with our real-time validator tool. Get suggestions and fix issues instantly.
Validate HTMLTest your knowledge of HTML5 with our interactive quiz and see how well you know the latest features.
Take the QuizLearn, experiment, and create amazing websites with our expert-led tutorials and tools.
Get Started"This platform helped me master HTML like a pro! The tools and tutorials are top-notch."
"I went from basic knowledge to being confident in creating full websites. Highly recommend!"
"The HTML Validator tool saved me hours of debugging. Thanks to the team for such great features!"
Responsive website built using HTML5, CSS3, and JavaScript.
Interactive eCommerce platform designed with modern HTML techniques.
Mobile-first website optimized for performance and SEO.
We launched with a vision to teach HTML and web development to the masses.
Expanded our offerings to include advanced tutorials, tools, and a growing community.
We became a global platform, offering content in multiple languages and catering to developers worldwide.
We’d love to hear from you! Whether you have a question, feedback, or just want to say hello, feel free to reach out.
Every HTML page starts with the same basic structure. Here's what the skeleton of any HTML document looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- Content goes here -->
</body>
</html>
This structure includes the **DOCTYPE declaration** at the top, which tells the browser to render the page as an HTML5 document. Inside the `html` tag, we have two main sections: `head` and `body`.
HTML is made up of **tags**, which define different elements on a web page. For example:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<a href="https://example.com">This is a link</a>
The `h1` tag defines a heading, the `p` tag defines a paragraph, and the `a` tag defines a link. The **anchor (`a`)** tag has an `href` attribute that tells the browser where the link should go.
To add images to your website, you use the `` tag. Here's an example:
<img src="https://via.placeholder.com/300" alt="Placeholder Image">
The `src` attribute is the path to the image, and the `alt` attribute provides a description of the image (important for accessibility).
HTML allows you to create two types of lists: **ordered** and **unordered**. Here's how you do it:
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ul>
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ol>
Unordered lists use the `ul` tag, while ordered lists use the `ol` tag. Each item in the list is wrapped in an `li` (list item) tag.
Links are created using the `a` (anchor) tag. You can link to other pages, sections of the same page, or even external websites.
<a href="#tutorial">Jump to Tutorial Section</a>
<a href="https://google.com" target="_blank">Open Google in a New Tab</a>
The `target="_blank"` attribute opens the link in a new tab. This is useful when linking to external sites.
HTML forms allow users to submit data to a server. Here's an example of a basic form:
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
This form sends the data to a server using the POST method when the **Submit** button is clicked. The `label` tag is associated with the input field using the `for` attribute.
You can embed media like audio and video files in HTML using the following tags:
<audio controls>
<source src="audio-file.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<video width="320" height="240" controls>
<source src="video-file.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
The `controls` attribute adds playback controls (play, pause, etc.). The `
HTML5 introduced new elements that help structure content better. Here are some of the most important ones:
<header>...</header>
<nav>...</nav>
<section>...</section>
<article>...</article>
<footer>...</footer>
These tags help make your code more readable and improve SEO. Use them to structure your page logically.
Styling text in HTML is done using CSS. Here are some common ways to style text:
<p style="font-size: 20px;">This text is larger than usual.</p>
In this example, the `font-size` property sets the size of the text in pixels.
<p style="color: red;">This text is red.</p>
Use the `color` property to change the color of the text.
<p><b>This is bold text.</b></p>
<p><i>This is italic text.</i></p>
<p><u>This is underlined text.</u></p>
You can use and the less then then (b) and then greater the for bold, and the less then then (i) and then greater the for italics, and the less then then (u) and then greater then for underline.
Resizing elements like headers and images is essential for making your page responsive and accessible. Here’s how you do it:
<h1 style="font-size: 40px;">Large Header</h1>
<h2 style="font-size: 30px;">Medium Header</h2>
You can use the `font-size` property to resize your headers to any size you want.
<img src="image.jpg" style="width: 300px; height: auto;" alt="A resized image">
The `width` property controls how wide the image is. Using `height: auto;` keeps the image’s aspect ratio intact.
Links are an important part of HTML. Here’s how you can use them:
<a href="https://example.com">Visit Example.com</a>
Use the `a` tag to create a link. The `href` attribute defines the destination of the link.
<a href="https://example.com" target="_blank">Open in New Tab</a>
The `target="_blank"` attribute opens the link in a new browser tab.
Tables are useful for displaying structured data. Here’s how to create a simple table:
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
</tr>
<tr>
<td>Bob</td>
<td>30</td>
</tr>
</table>
The `table` tag creates a table, `tr` defines a row, `th` defines a header cell, and `td` defines a standard cell.
Forms allow users to submit data to a server. Here’s a basic form example:
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
The `action` attribute tells the form where to send the data, and the `method="POST"` sends the data in the background.
You can embed audio and video using the following tags:
<audio controls>
<source src="audio-file.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<video width="320" height="240" controls>
<source src="video-file.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Both the `audio` and `video` tags allow you to embed media directly into your HTML page. The `controls` attribute provides play/pause controls.
In CSS, semicolons and colons are critical components of the syntax. Here’s how they work:
Semicolons are used to **separate** different CSS declarations. They mark the end of a statement, just like periods in sentences.
h1 {
color: red; /* This changes the text color */
font-size: 32px; /* This changes the font size */
}
In this example, each CSS property (e.g., `color`, `font-size`) is followed by a semicolon to signify the end of the declaration. Every declaration block inside `{}` uses semicolons to separate different properties.
Colons are used to **assign a value** to a CSS property. They separate the property from its value. For example:
p {
text-align: center; /* Text is centered */
color: blue; /* Text color is blue */
}
The colon comes after the property (`text-align`) and before the value (`center`). It’s essential to use colons properly to ensure your CSS works.
HTML attributes provide additional information about an element. They are always included in the opening tag and usually consist of a name/value pair, like this:
<element attribute="value">...</element>
<a href="https://example.com" target="_blank">Visit Example.com</a>
<img src="image.jpg" alt="A beautiful scenery">
In this example, the `a` element uses the `href` attribute to link to another page, and the `img` element uses `src` and `alt` to display an image with descriptive text.
In HTML, **ID** and **class** are used to uniquely identify and style elements. Here’s how they work:
The `id` attribute gives an element a unique identifier that can be referenced by CSS and JavaScript. No two elements should have the same `id` on a page.
<div id="header">This is the header</div>
In CSS, you can style this element using `#header`:
#header {
background-color: #333;
color: white;
}
The `class` attribute allows you to apply the same styles to multiple elements. Unlike `id`, multiple elements can share the same class.
<p class="highlight">Important text.</p>
<p class="highlight">Another important text.</p>
In CSS, you can style all elements with the class `highlight` using a period (`.`) before the class name:
.highlight {
color: red;
font-weight: bold;
}
CSS provides several ways to position elements on a webpage. Here are the most common positioning methods:
By default, elements are positioned **statically**, which means they follow the normal flow of the document.
p {
position: static; /* This is the default */
}
An element positioned **relatively** is moved relative to its normal position in the document flow.
p {
position: relative;
top: 10px; /* Moves the element down by 10px */
left: 20px; /* Moves the element right by 20px */
}
An element with **absolute positioning** is positioned relative to its nearest positioned ancestor (not relative to the document flow).
div {
position: absolute;
top: 50px;
right: 20px;
}
Elements with **fixed positioning** are positioned relative to the browser window. They stay in the same place when you scroll.
div {
position: fixed;
bottom: 0;
right: 0;
}
**Flexbox** is a modern CSS layout model that allows you to easily position and align elements. Here’s a quick example:
div {
display: flex;
justify-content: center; /* Centers content horizontally */
align-items: center; /* Centers content vertically */
}
Forms are one of the most important elements in HTML. They allow users to submit data. Let's explore some advanced form elements:
Text input fields allow users to type in information. You should always use labels to describe them:
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</form>
In this example, the `for` attribute in `label` matches the `id` of the input element. The `required` attribute makes the field mandatory.
Dropdowns allow users to select one option from a list:
<label for="car">Choose a car:</label>
<select id="car" name="car">
<option value="volvo">Volvo</option>
<option value="bmw">BMW</option>
</select>
Radio buttons allow a user to select only one option from a group, while checkboxes allow multiple selections:
<!-- Radio Buttons -->
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>
<!-- Checkboxes -->
<label><input type="checkbox" name="subscribe" value="yes"> Subscribe to Newsletter</label>
Radio buttons and checkboxes work similarly, but radios enforce a single choice while checkboxes allow multiple.
Let users upload files using the `` tag:
<label for="file-upload">Upload a file:</label>
<input type="file" id="file-upload" name="file-upload">
Responsive design ensures your webpage looks great on any device, whether it’s a phone, tablet, or desktop. Media queries in CSS help achieve this. Here's how:
Media queries allow you to apply styles based on the size of the viewport (the browser window). For example:
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
This media query applies the background color only if the browser window is 600 pixels wide or less (i.e., mobile devices).
You can target various screen sizes for different devices:
@media (max-width: 768px) {
body {
background-color: lightgreen;
}
}
@media (min-width: 769px) and (max-width: 1024px) {
body {
background-color: lightyellow;
}
}
In this example, screens with widths of 768px or less (tablets) have one background color, and wider screens (desktops) have another.
CSS Grid is a powerful layout system that allows you to create complex, responsive layouts with ease. Here's how it works:
Start by defining a grid container. This element will hold your grid items:
div {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* Creates 3 equal columns */
grid-gap: 20px; /* Adds spacing between grid items */
}
Here, we use `grid-template-columns` to create three equal-width columns. The `1fr` unit means “one fraction of the available space.”
You can place items into specific rows and columns using `grid-column` and `grid-row`:
div.item {
grid-column: 1 / 3; /* Spans the first two columns */
grid-row: 1; /* Stays in the first row */
}
In this example, the item spans the first two columns but only occupies the first row.
By default, grid items are placed in the next available cell. You can control this with auto-placement:
div {
grid-auto-flow: dense; /* Fills in gaps in the grid automatically */
}
CSS transitions and animations allow you to create smooth, dynamic effects on your website. Let's break them down:
**Transitions** allow changes to CSS properties to occur smoothly over a given duration. For example, you can smoothly change a button's background color when hovered:
a {
background-color: #ffa500;
transition: background-color 0.3s ease; /* 0.3s transition */
}
a:hover {
background-color: #ff4500; /* Changes color on hover */
}
Here, the `transition` property makes the background color change smoothly over 0.3 seconds when the user hovers over the link.
**Animations** give you more control over how elements change over time by using keyframes. Here's an example of a bounce animation:
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
div {
animation: bounce 2s infinite;
}
In this example, the element will "bounce" up and down, looping forever (`infinite`). The `@keyframes` rule defines how the element behaves at each stage of the animation.
Pseudo-classes and pseudo-elements allow you to style parts of your page in ways that aren’t possible with standard CSS. Here’s a breakdown:
Pseudo-classes apply styles to an element based on its state. For example:
a:hover {
color: #ff4500; /* Change the link color when hovered */
}
input:focus {
border-color: #00ff00; /* Highlight the border when input is focused */
}
Common pseudo-classes include `:hover` (when an element is hovered), `:focus` (when an input is focused), and `:nth-child()` (for styling specific children in a list or table).
Pseudo-elements target parts of elements (such as the first letter of a paragraph or content before an element). Here are two examples:
These pseudo-elements insert content before or after an element:
p::before {
content: "👉 "; /* Adds an emoji before the paragraph */
}
p::after {
content: " 🚀"; /* Adds a rocket emoji after the paragraph */
}
These are great for adding extra visual elements to your design without needing additional HTML.
You can use `::first-letter` or `::first-line` to style specific parts of a block of text:
p::first-letter {
font-size: 2em;
font-weight: bold;
color: #ffa500;
}
This example makes the first letter of the paragraph larger and bolded.
CSS transformations allow you to change the size, position, and rotation of elements. Here are some examples:
You can rotate an element using the `transform` property:
div {
transform: rotate(45deg); /* Rotates the element by 45 degrees */
}
The value `45deg` indicates the angle in degrees. You can also rotate elements by negative values for a counter-clockwise rotation.
You can change the size of an element with the `scale()` function:
div {
transform: scale(1.5); /* Increases the size by 150% */
}
The `scale()` function increases or decreases the size of an element based on the given factor. `1.5` makes it 150% of its original size, while `0.5` would shrink it to 50% of its size.
You can skew (distort) elements along the X or Y axis:
div {
transform: skewX(20deg); /* Skews the element along the X-axis */
}
This will skew the element by 20 degrees along the horizontal axis. You can also use `skewY()` for vertical skewing.
You can apply multiple transformations at once:
div {
transform: rotate(30deg) scale(1.2);
}
Here, the element is rotated by 30 degrees and scaled up by 120%. Transformations are applied in the order they are written.
**Flexbox** is a layout model in CSS that allows you to easily position and align items within a container. It’s great for building responsive layouts without needing to use floats or complex positioning. Here’s how it works:
Start by defining a flex container. Flexbox will only apply to the direct children of this container:
div.container {
display: flex;
justify-content: space-between; /* Distributes items with space between them */
align-items: center; /* Aligns items vertically in the center */
}
The `display: flex;` makes the container a flexbox, and its children become flex items. The `justify-content` property distributes the items horizontally, and `align-items` controls their vertical alignment.
By default, flex items are laid out in a row. You can change this using the `flex-direction` property:
div.container {
flex-direction: column; /* Arranges items vertically */
}
Setting `flex-direction` to `column` will arrange the items vertically instead of horizontally.
If you have more items than can fit in a row, you can make them wrap onto the next line using `flex-wrap`:
div.container {
flex-wrap: wrap; /* Allows items to wrap onto the next line */
}
This allows flex items to wrap if they don’t fit in a single row.
HTML5 provides built-in validation features for forms, but you can also add custom validation to enhance user experience. Let’s dive into both:
HTML5 forms come with built-in validation attributes like `required`, `minlength`, `maxlength`, and `pattern`. Here’s how to use them:
<form action="/submit" method="POST">
<label for="name">Name (required)</label>
<input type="text" id="name" name="name" required minlength="3">
<label for="email">Email (valid email required)</label>
<input type="email" id="email" name="email" required>
<button type="submit">Submit</button>
</form>
The `required` attribute ensures the field must be filled in before the form can be submitted. `minlength` ensures the user enters at least 3 characters for the name. If the email field is not a valid email address, the form won’t submit.
In addition to the built-in validation, you can use JavaScript to create custom validation logic. Here’s an example:
<form id="custom-form">
<input type="text" id="username" placeholder="Username">
<span id="error-message" style="color: red; display: none;">Username is required!</span>
<button type="submit">Submit</button>
</form>
<script>
document.getElementById('custom-form').addEventListener('submit', function(event) {
const username = document.getElementById('username').value;
const errorMessage = document.getElementById('error-message');
if (username === '') {
errorMessage.style.display = 'block'; // Show error message
event.preventDefault(); // Prevent form from submitting
}
});
</script>
In this example, if the username field is empty, the form submission will be prevented, and an error message will be displayed.
Sometimes, you’ll want to change the default behavior of browsers to fit your design needs. Here are a few common examples:
By default, browsers show a blue outline around focused elements like buttons and links. You can remove or customize this outline with CSS:
a, button {
outline: none; /* Removes default focus outline */
}
a:focus, button:focus {
box-shadow: 0 0 5px #ffa500; /* Adds a custom shadow for accessibility */
}
It’s important not to remove the focus state entirely, as it helps users who navigate with a keyboard.
You can customize scrollbars for modern browsers using the `::-webkit-scrollbar` pseudo-element:
::-webkit-scrollbar {
width: 12px;
}
::-webkit-scrollbar-thumb {
background-color: #ffa500; /* Scrollbar color */
border-radius: 10px;
}
This pseudo-element allows you to change the appearance of scrollbars in webkit-based browsers (Chrome, Safari).
You can prevent users from selecting text on your webpage using the `user-select` property:
body {
user-select: none; /* Prevents text selection */
}
Use this property sparingly, as it can impact the user experience.
CSS Variables, also known as **custom properties**, allow you to store values and reuse them throughout your stylesheet. This makes your CSS more maintainable and flexible. Here's how to use them:
CSS variables are declared using the `--` prefix and are often defined in the `:root` selector to make them globally available:
:root {
--primary-color: #ff4500;
--secondary-color: #ffa500;
--font-size: 18px;
}
In this example, we define three variables: `--primary-color`, `--secondary-color`, and `--font-size`. These variables can now be used anywhere in your CSS.
You can use a variable by referencing it with the `var()` function:
body {
font-size: var(--font-size);
color: var(--primary-color);
}
h1 {
background-color: var(--secondary-color);
}
Variables make it easy to update styles globally. Changing the value of `--primary-color` will automatically update all instances where it's used.
You can override variables locally within specific selectors. For example:
button {
--primary-color: #0000ff; /* Changes the primary color locally */
color: var(--primary-color); /* Uses the overridden color */
}
In this example, the button's `--primary-color` is changed to blue without affecting the rest of the document.
The **z-index** property controls the stacking order of elements on the z-axis (i.e., whether elements appear in front or behind other elements). Here’s how to use it effectively:
Elements with a higher `z-index` appear on top of elements with a lower `z-index`:
div {
position: relative; /* Z-index only works with positioned elements */
z-index: 10; /* Higher z-index means it will appear on top */
}
span {
position: relative;
z-index: 5; /* This will appear behind the div */
}
By default, elements with higher z-index values are stacked on top of those with lower values.
**Stacking context** is created when an element is positioned (relative, absolute, or fixed) and has a z-index value other than `auto`. Elements inside a stacking context are stacked independently of the rest of the document:
div.parent {
position: relative;
z-index: 10;
}
div.child {
position: relative;
z-index: 5; /* This is within the parent stacking context */
}
In this case, the `.child` element’s z-index only affects elements inside its parent, not the entire page.
CSS Filters allow you to apply visual effects like **blurring, grayscale, contrast** adjustments, and more. They are commonly used for images, but can be applied to any element. Here’s how:
img {
filter: blur(5px) grayscale(50%) brightness(0.8);
}
In this example, the image will be blurred by 5px, turned 50% grayscale, and slightly darkened (brightness at 80%). Filters can be combined in a single line.
The **CSS Box Model** is crucial for understanding how elements are sized and spaced on the page. It consists of four parts: content, padding, border, and margin. Here’s a breakdown:
div {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
In this example, the content is 200px wide, with 20px of padding, a 5px border, and 10px of margin.
By default, the width and height of an element are calculated without including padding and borders. The `box-sizing` property changes this behavior:
div {
box-sizing: border-box; /* Includes padding and border in width/height */
}
With `box-sizing: border-box;`, the width includes padding and borders, which simplifies layout calculations.
**CSS Shapes** and **Clip Paths** allow you to create non-rectangular layouts and shapes. Here’s how:
The `clip-path` property defines which part of an element should be visible. You can create custom shapes using various functions:
div {
clip-path: circle(50%);
}
In this example, the element will be clipped into a circle with a radius of 50% of the element’s size.
You can define complex shapes with the `polygon()` function:
div {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
This creates a diamond shape, with points defined at each corner.
The `shape-outside` property allows text to wrap around custom shapes:
img {
float: left;
shape-outside: circle(50%);
}
In this example, text will wrap around an image clipped into a circle.
CSS Variables, also known as **custom properties**, allow you to store values and reuse them throughout your stylesheet. Here's how to use them:
CSS variables are declared using the `--` prefix and are often defined in the `:root` selector to make them globally available:
:root {
--primary-color: #ff4500;
--secondary-color: #ffa500;
--font-size: 18px;
}
In this example, we define three variables: `--primary-color`, `--secondary-color`, and `--font-size`. These variables can now be used anywhere in your CSS.
You can use a variable by referencing it with the `var()` function:
body {
font-size: var(--font-size);
color: var(--primary-color);
}
h1 {
background-color: var(--secondary-color);
}
Variables make it easy to update styles globally. Changing the value of `--primary-color` will automatically update all instances where it's used.
You can override variables locally within specific selectors. For example:
button {
--primary-color: #0000ff; /* Changes the primary color locally */
color: var(--primary-color); /* Uses the overridden color */
}
In this example, the button's `--primary-color` is changed to blue without affecting the rest of the document.
The **z-index** property controls the stacking order of elements on the z-axis (i.e., whether elements appear in front or behind other elements). Here’s how to use it effectively:
Elements with a higher `z-index` appear on top of elements with a lower `z-index`:
div {
position: relative; /* Z-index only works with positioned elements */
z-index: 10; /* Higher z-index means it will appear on top */
}
span {
position: relative;
z-index: 5; /* This will appear behind the div */
}
By default, elements with higher z-index values are stacked on top of those with lower values.
**Stacking context** is created when an element is positioned (relative, absolute, or fixed) and has a z-index value other than `auto`. Elements inside a stacking context are stacked independently of the rest of the document:
div.parent {
position: relative;
z-index: 10;
}
div.child {
position: relative;
z-index: 5; /* This is within the parent stacking context */
}
In this case, the `.child` element’s z-index only affects elements inside its parent, not the entire page.
CSS Filters allow you to apply visual effects like **blurring, grayscale, contrast** adjustments, and more. They are commonly used for images, but can be applied to any element. Here’s how:
img {
filter: blur(5px) grayscale(50%) brightness(0.8);
}
In this example, the image will be blurred by 5px, turned 50% grayscale, and slightly darkened (brightness at 80%). Filters can be combined in a single line.
The **CSS Box Model** is crucial for understanding how elements are sized and spaced on the page. It consists of four parts: content, padding, border, and margin. Here’s a breakdown:
div {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
In this example, the content is 200px wide, with 20px of padding, a 5px border, and 10px of margin.
By default, the width and height of an element are calculated without including padding and borders. The `box-sizing` property changes this behavior:
div {
box-sizing: border-box; /* Includes padding and border in width/height */
}
With `box-sizing: border-box;`, the width includes padding and borders, which simplifies layout calculations.
**CSS Shapes** and **Clip Paths** allow you to create non-rectangular layouts and shapes. Here’s how:
The `clip-path` property defines which part of an element should be visible. You can create custom shapes using various functions:
div {
clip-path: circle(50%);
}
In this example, the element will be clipped into a circle with a radius of 50% of the element’s size.
You can define complex shapes with the `polygon()` function:
div {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
This creates a diamond shape, with points defined at each corner.
The `shape-outside` property allows text to wrap around custom shapes:
img {
float: left;
shape-outside: circle(50%);
}
In this example, text will wrap around an image clipped into a circle.
This entire website is built with just one HTML file!
×This content is available only to $9.99 subscribers. To gain access to advanced HTML, CSS, and performance optimization tutorials, please subscribe.
As part of your premium subscription, you can download well-organized code snippets that you can use in your own projects. Below are some key snippets for advanced layouts and animations:
Edit and run your own HTML & CSS code directly in the browser! This playground allows you to experiment with the concepts you've learned and see real-time results.
Access cheat sheets, quick reference guides, and premium tutorials to speed up your workflow:
Making Comments in HTML and CSS
Comments are crucial in both HTML and CSS for making your code more readable and maintainable. Here’s how to add comments in both:
1. HTML Comments
HTML comments help you annotate your code without affecting how the browser renders the page. They’re invisible to users.
The comment is written inside `<!-- -->`. Anything between these symbols will be ignored by the browser.
2. CSS Comments
In CSS, comments allow you to explain the styles without affecting the layout. CSS comments are written like this:
In CSS, comments are enclosed between `/* */`. They can span multiple lines and are essential for leaving notes, especially in complex stylesheets.
Best Practices for Comments: