Design Converter
Education
Last updated on Jun 6, 2024
Last updated on May 30, 2024
An HTML Cheat Sheet is an essential reference document that summarizes key HTML elements, attributes, and syntax, making it easier for web developers to create websites efficiently.
HTML, or Hypertext Markup Language, is the foundational language used to create and structure content on the web. It consists of a series of elements and tags that define the layout and format of a web page. Each HTML document is structured using a set of predefined tags such as <html>
, <head>
, and <body>
, which organize the content and metadata of the page.
A basic HTML document is structured with essential elements that define the content and metadata of the web page. Here is an overview of these fundamental components:
The <!DOCTYPE html>
declaration must be the very first thing in your HTML document. It tells the browser to render the page in standards mode, ensuring that the HTML and CSS behave consistently across different browsers.
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Sample Page</title> 6</head> 7<body> 8 <h1>Welcome to My Website</h1> 9 <p>This is a paragraph of text on my web page.</p> 10</body> 11</html>
The <html>
element wraps all the content of the web page. This element is also known as the root element. It typically includes the lang attribute to specify the language of the document:
1<html lang="en">
The <head>
element contains meta-information about the document, such as its title, character set, styles, scripts, and other meta tags:
1<head> 2 <meta charset="UTF-8"> 3 <title>Sample Page</title> 4</head>
The <body>
element includes all the content of the HTML document, such as text, images, links, and other media:
1<body> 2 <h1>Welcome to My Website</h1> 3 <p>This is a paragraph of text on my web page.</p> 4</body>
The html lang attribute specifies the language of the content in the HTML document. This helps search engines and browsers to render the text correctly and is important for accessibility:
1<html lang="en">
Meta tags provide metadata about the HTML document, which is used by browsers and search engines. The term 'meta tag' refers to additional material about the HTML. Here are some important meta tags:
The <meta charset="UTF-8">
tag specifies the character encoding for the HTML document, ensuring that it can display any character correctly:
1<meta charset="UTF-8">
Meta name tags provide metadata such as descriptions, keywords, and author information, which are useful for search engines:
1<meta name="description" content="A brief description of the page content"> 2<meta name="keywords" content="HTML, CSS, JavaScript"> 3<meta name="author" content="Your Name">
The meta http-equiv tag provides HTTP headers to the browser, such as page refreshes and character sets:
1<meta http-equiv="X-UA-Compatible" content="IE=edge"> 2<meta http-equiv="refresh" content="30">
The meta name="viewport" tag controls the layout on mobile browsers, ensuring the page is responsive:
1<meta name="viewport" content="width=device-width, initial-scale=1.0">
These tags and elements collectively ensure that your HTML document is well-structured, accessible, and optimized for both search engines and users.
Headings are used to create titles and subtitles within the content. HTML provides six levels of headings, <h1>
being the highest level and <h6>
the lowest:
1<h1>Main Heading</h1> 2<h2>Subheading</h2> 3<h3>Sub-subheading</h3> 4<h4>Sub-sub-subheading</h4> 5<h5>Sub-sub-sub-subheading</h5> 6<h6>Sub-sub-sub-sub-subheading</h6>
The <p>
tag is used to define paragraphs of text in an HTML document:
1<p>This is a paragraph of text.</p>
The <br>
tag is an empty element used to insert a line break within text:
1<p>This is a line of text.<br>This is another line of text.</p>
The <b>
tag makes text bold:
1<p>This is <b>bold</b> text.</p>
The <i>
tag makes text italic:
1<p>This is <i>italic</i> text.</p>
The <pre>
tag is used to display pre formatted text, preserving both spaces and line breaks:
1<pre> 2 This text 3 is preformatted. 4</pre>
The <code>
tag is used to define a piece of computer code:
1<p>Here is some <code>inline code</code>.</p>
The <blockquote>
tag is used to define a section that is quoted from another source:
1<blockquote> 2 This is a blockquote. 3</blockquote>
The <ul>
tag defines an unordered list, which is a bulleted list:
1<ul> 2 <li>Item 1</li> 3 <li>Item 2</li> 4 <li>Item 3</li> 5</ul>
The <ol>
tag defines an ordered list, which is a numbered list:
1<ol> 2 <li>First item</li> 3 <li>Second item</li> 4 <li>Third item</li> 5</ol>
The <dl>
tag is used to create a list of definitions:
1<dl> 2 <dt>HTML</dt> 3 <dd>Hypertext Markup Language</dd> 4 <dt>CSS</dt> 5 <dd>Cascading Style Sheets</dd> 6</dl>
The <a>
tag defines a hyperlink, which is used to link from one page to another:
1<a href="https://www.example.com">Visit Example.com</a>
Navigation links are typically created using the <a>
tag within a navigation bar:
1<nav> 2 <a href="home.html">Home</a> 3 <a href="about.html">About</a> 4 <a href="contact.html">Contact</a> 5</nav>
These basic HTML tags and elements are fundamental building blocks for creating structured, well-formatted web pages.
The <img>
tag is used to embed images in an HTML document. The src attribute specifies the path to the image file:
1<img src="image.jpg" alt="Description of the image">
In this example, the src attribute points to "image.jpg". This could be a relative path or an absolute URL.
The alt attribute provides alternative text for the image if it cannot be displayed. It is also crucial for accessibility, as it allows screen readers to describe the image to visually impaired users:
1<img src="image.jpg" alt="A beautiful sunrise over the mountains">
Providing descriptive alternate text enhances the usability and accessibility of web pages.
The <audio>
tag is used to embed sound content in an HTML document. The src attribute specifies the path to the audio file. The controls attribute adds audio controls like play, pause, and volume:
1<audio src="audio.mp3" controls> 2 Your browser does not support the audio element. 3</audio>
You can also include multiple sources using the <source>
element to provide different formats for better compatibility:
1<audio controls> 2 <source src="audio.mp3" type="audio/mpeg"> 3 <source src="audio.ogg" type="audio/ogg"> 4 Your browser does not support the audio element. 5</audio>
The <video>
tag is used to embed video content. Like the <audio>
tag, it includes the controls attribute for play, pause, and volume controls:
1<video src="video.mp4" controls> 2 Your browser does not support the video tag. 3</video>
Multiple sources can be provided for better browser support using the <source>
element:
1<video controls> 2 <source src="video.mp4" type="video/mp4"> 3 <source src="video.ogg" type="video/ogg"> 4 Your browser does not support the video tag. 5</video>
The <source>
element is used within <audio>
and <video>
tags to specify multiple media resources. Each <source>
element includes a src attribute that defines the file path and a type attribute that defines the media type:
1<video controls> 2 <source src="movie.mp4" type="video/mp4"> 3 <source src="movie.ogg" type="video/ogg"> 4 Your browser does not support the video tag. 5</video>
By including multiple sources, you ensure that your media files are compatible with different browsers and devices.
Tables in HTML are created using the <table>
element. Within this element, you define rows using <tr>
, header cells using <th>
, and data cells using <td>
. Here’s a breakdown of the basic structure:
The <tr>
tag defines a row in the table. Each table row can contain one or more <th>
or <td>
elements:
1<table> 2 <tr> 3 <th>Header 1</th> 4 <th>Header 2</th> 5 </tr> 6 <tr> 7 <td>Data 1</td> 8 <td>Data 2</td> 9 </tr> 10</table>
The <th>
tag is used to define a header cell in a table, which is typically displayed as bold and centered text. Header cells are important for providing context to the table data:
1<tr> 2 <th>Header 1</th> 3 <th>Header 2</th> 4</tr>
The <caption>
tag provides a title or description for the table. It is placed directly after the opening <table>
tag:
1<table> 2 <caption>Monthly Sales Data</caption> 3 <tr> 4 <th>Month</th> 5 <th>Sales</th> 6 </tr> 7 <tr> 8 <td>January</td> 9 <td>$10,000</td> 10 </tr> 11</table>
Table headers can span multiple columns or rows using the colspan and rowspan attributes, respectively:
1<table border="1"> 2 <tr> 3 <th colspan="2">Main Header</th> 4 </tr> 5 <tr> 6 <th>Subheader 1</th> 7 <th>Subheader 2</th> 8 </tr> 9 <tr> 10 <td>Data 1</td> 11 <td>Data 2</td> 12 </tr> 13</table>
In this example, the first row's header cell spans two columns.
The <td>
tag defines a standard data cell in the table. These cells can also use the colspan and rowspan attributes to span multiple columns or rows:
1<table border="1"> 2 <tr> 3 <th>Header 1</th> 4 <th>Header 2</th> 5 </tr> 6 <tr> 7 <td rowspan="2">Rowspan Data</td> 8 <td>Data 2</td> 9 </tr> 10 <tr> 11 <td>Data 3</td> 12 </tr> 13</table>
Here, the first data cell in the second row spans two rows, merging the cells below it.
Forms are essential for collecting user input on web pages. The <form>
element is used to create a form in HTML. It can contain various types of input elements and controls.
The action attribute of the <form>
tag specifies the URL where the form data should be sent when the form is submitted:
1<form action="submit_form.php" method="post"> 2 <!-- form elements go here --> 3</form>
HTML forms support a variety of input types to capture different types of data from users. Some common input types include:
1<input type="text" name="username" placeholder="Enter your username"> 2<input type="password" name="password" placeholder="Enter your password"> 3<input type="email" name="email" placeholder="Enter your email"> 4<input type="number" name="age" placeholder="Enter your age"> 5<input type="submit" value="Submit">
These input types ensure that the data collected is appropriate for the intended purpose.
An input field is created using the <input>
tag. Each input field can have various attributes such as type, name, placeholder, and value to define its behavior and appearance:
1<input type="text" name="firstname" placeholder="First Name"> 2<input type="text" name="lastname" placeholder="Last Name">
The name attribute of an input element specifies the name of the form control. When the form is submitted, the data entered into the input field is sent as a name-value pair:
1<input type="text" name="username" placeholder="Username"> 2<input type="password" name="password" placeholder="Password">
Buttons and labels are used to interact with form elements. The <button>
tag creates a clickable button, and the <label>
tag is used to define labels for form elements, improving accessibility:
1<form> 2 <label for="username">Username:</label> 3 <input type="text" id="username" name="username"> 4 <button type="submit">Submit</button> 5</form>
Using the for attribute in the <label>
tag links it to a specific form element, making it easier for users to click and access the input field.
Checkboxes and radio buttons allow users to select one or more options from a set. The type attribute is set to checkbox for checkboxes and radio for radio buttons:
1<form> 2 <label for="subscribe">Subscribe to newsletter:</label> 3 <input type="checkbox" id="subscribe" name="subscribe" value="yes"> 4 5 <p>Select your gender:</p> 6 <input type="radio" id="male" name="gender" value="male"> 7 <label for="male">Male</label> 8 <input type="radio" id="female" name="gender" value="female"> 9 <label for="female">Female</label> 10</form>
Checkboxes allow multiple selections, while radio buttons limit the selection to one option within a group.
Inline styles are used to apply a unique style to a single HTML element. To use inline styles, you add the style attribute directly to the HTML tag. This method is useful for quick, one-off changes but is not recommended for larger projects due to its lack of reusability and separation of concerns.
The style attribute is used to add CSS properties directly to an HTML element. Multiple CSS properties are separated by semicolons:
1<p style="color: blue; font-size: 20px;">This is a styled paragraph.</p>
The text-align property is used to set the horizontal alignment of text within an element. It can be set to left, right, center, or justify:
1<p style="text-align: center;">This text is centered.</p>
The background property is used to set the background color or image of an HTML element, such as the <body>
element. This can enhance the visual appeal of the web page:
1<body style="background-color: lightgray;"> 2 <h1>Welcome to My Website</h1> 3</body>
External CSS involves linking a separate CSS file to an HTML document. This method is preferred for maintaining larger websites as it allows for centralized and reusable style definitions.
The <link>
tag is used to link an external CSS file to an HTML document. The rel attribute specifies the relationship between the current document and the linked file, which is typically set to "stylesheet". The href attribute specifies the path to the CSS file:
1<head> 2 <link rel="stylesheet" href="styles.css"> 3</head>
This approach keeps the HTML clean and allows for consistent styling across multiple pages by editing a single CSS file.
Meta tags are crucial for search engine optimization (SEO) and ensuring compatibility with different browsers and devices. Here are some important meta tags:
The meta name="viewport" tag is essential for responsive web design. It controls the layout on mobile browsers and ensures that web pages render correctly on different devices. This tag sets the viewport's width to the device's width and the initial scale to 1.0:
1<meta name="viewport" content="width=device-width, initial-scale=1.0">
This tag helps create a better user experience by making the web page adaptable to various screen sizes.
The meta http-equiv="X-UA-Compatible" tag is used to specify the document mode for Internet Explorer, which can help in rendering the web page correctly in different versions of IE:
1<meta http-equiv="X-UA-Compatible" content="IE=edge">
Using this tag ensures that the latest rendering engine is used, which can help avoid compatibility issues with older versions of Internet Explorer.
HTML5 introduced several semantic elements that provide more meaningful structure to web pages. These elements help both developers and browsers understand the content's structure and hierarchy better.
These elements are used to define different parts of a web page:
<header>
: Represents introductory content, typically containing navigation links, logos, or headings.
<footer>
: Contains footer content, such as copyright information or links to privacy policies.
<main>
: Denotes the main content of the document, unique to the document, and must not contain content that is repeated across pages.
<section>
: Defines sections within the document, such as chapters, headers, footers, or any other sections of content.
Example:
1<header> 2 <h1>Website Title</h1> 3 <nav> 4 <a href="#home">Home</a> 5 <a href="#about">About</a> 6 <a href="#contact">Contact</a> 7 </nav> 8</header> 9 10<main> 11 <section> 12 <h2>Section Title</h2> 13 <p>This is a section of the main content.</p> 14 </section> 15</main> 16 17<footer> 18 <p>© 2024 Your Website</p> 19</footer>
These elements provide additional structure to web pages:
<article>
: Represents a self-contained composition in a document, page, or site, such as a blog post or news article.
<aside>
: Contains content that is tangentially related to the main content, such as sidebars or call-out boxes.
<nav>
: Defines a section of navigation links.
Example:
1<article> 2 <h2>Article Title</h2> 3 <p>This is the main content of the article.</p> 4</article> 5 6<aside> 7 <h3>Related Links</h3> 8 <ul> 9 <li><a href="#link1">Link 1</a></li> 10 <li><a href="#link2">Link 2</a></li> 11 </ul> 12</aside> 13 14<nav> 15 <ul> 16 <li><a href="#home">Home</a></li> 17 <li><a href="#about">About</a></li> 18 <li><a href="#services">Services</a></li> 19 </ul> 20</nav>
These semantic elements enhance the readability of the code and improve accessibility by providing meaningful landmarks for screen readers and other assistive technologies.
The <div>
and <span>
tags are fundamental container tags in HTML, used to group elements for styling and scripting purposes. They do not inherently convey any meaning but are extremely versatile for layout and styling.
<div>
: A block-level element used to group larger sections of content. It typically contains other block-level elements and can be styled using CSS.1<div class="container"> 2 <h2>Title</h2> 3 <p>This is a paragraph inside a div.</p> 4</div>
<span>
: An inline element used to group small portions of text or other inline elements. It's often used to apply styles or manipulate content with JavaScript.1<p>This is a <span class="highlight">highlighted</span> word in a paragraph.</p>
Multiple container tags can be nested or used in combination to create complex layouts and styles. For example, using a combination of <div>
and <span>
elements:
1<div class="content"> 2 <h2>Article Title</h2> 3 <p>This is an article. <span class="author">Author Name</span></p> 4 <div class="comments"> 5 <p>Comment 1</p> 6 <p>Comment 2</p> 7 </div> 8</div>
Empty elements, also known as self-closing tags, do not have closing tags and do not contain any content. Examples include <br>
for line breaks, <img>
for images, and <hr>
for horizontal rules. These elements are useful for adding structural elements without content.
1<p>This is a paragraph with a line break.<br>Here is the text after the line break.</p> 2<img src="image.jpg" alt="An example image"> 3<hr>
The id attribute is used to assign a unique identifier to an HTML element. This identifier must be unique within the document, making it useful for targeting elements with CSS and JavaScript:
1<p id="unique-paragraph">This paragraph has a unique id.</p>
You can target this element in CSS and JavaScript:
1#unique-paragraph { 2 color: blue; 3}
1document.getElementById("unique-paragraph").style.fontSize = "20px";
The class attribute is used to assign one or more class names to an HTML element. Unlike the id attribute, class names do not have to be unique and can be used on multiple elements. This makes classes ideal for applying the same styles to multiple elements:
1<p class="highlight">This is a highlighted paragraph.</p> 2<p class="highlight">This is another highlighted paragraph.</p>
You can target these elements in CSS:
1.highlight { 2 background-color: yellow; 3}
In JavaScript:
1let elements = document.getElementsByClassName("highlight"); 2for (let i = 0; i < elements.length; i++) { 3 elements[i].style.border = "1px solid red"; 4}
Using id and class attributes effectively helps organize and manipulate HTML elements, providing flexibility and control over web page design and functionality.
In this HTML cheat sheet, we covered the essential elements and attributes that form the foundation of web development, from basic document structure and text formatting to multimedia elements, tables, forms, and CSS styling. We also explored advanced HTML elements and best practices for creating accessible and responsive web pages. Continuous learning and staying updated with the latest HTML developments is crucial for improving your skills, leveraging new features, and following best practices to create high-quality, maintainable web content.
Using a cheat sheet can significantly speed up the web development process by providing quick access to commonly used HTML tags and their functionalities.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.