tag like this:
My first line.<br/>My second line.
// Output:
// My first line.
// My second line.
This is another self-closing tag, where the br
tag contains no attributes and no closing tag superceding it.
div
tagThe div
tag defines a division or a section in an HTML document and is used as a container for other HTML elements. These elements can include content areas, sidebars, page headers, page footers, and more.
You can have as many div
elements as you want within a single web page. I've always practiced keeping HTML code to a minimum, so the page code doesn't outweigh the page content. Plus, I'm a minimalist. The less code, the better.
Here's an example of two sections separated by div
containers:
<div class="container">
My first container.
</div>
<div class="container">
My second container.
</div>
Visibly, nothing is separating the content of these two div
containers other than some vertical spacing. You could style these two elements any way you want to distinguish the two different sections. You could align them by sitting them side-by-side instead of top-bottom by adding display: inline-block;
styling to the elements. You could set the position styling to absolute
and place one container in the top-left corner of the screen, and the other in the top-right corner.
The options are endless as div
elements allow you to style and display in almost any way you can think possible.
span
tagUnlike div
tags, span
tags are inline containers used to mark up a part of a text within a document. This could include highlighting text, changing text color, etc., and can look something like this:
This is my first line.
<span style="color: red;">This line is red.</span>
This line is back to the normal color.
Tables allow page authors to arrange data into rows and columns. In its simplest form, your table structure could look like this:
<table>
<caption>My Table</caption>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td>$300</td>
</tr>
</tfoot>
</table>
Each table is created using, you guessed it, the table
tag! Within the table
tag, you can add additional elements that help organize your data as you need it:
tr
, to display column title names.td
, after your table header row, to display table cell data.Almost every website serves up images in some way, whether they be photographs, icons, or even vector images in browsers that support them.
Some of the major file formats supported with the img
tag include .jpg, .jpeg, .png, and .gif. And .svg format for vector images.
The following example shows how you can load images internally from different paths, or externally with a URL:
// load image from current directory:
<img src="my_image.jpg" />
// load image from project's root directory:
<img src="/my_image.jpg" />
// load image from local machine's file location:
<img src="file:\\\C:\images\my_image.jpg" />
// load image from URL:
<img src="https://orangeable.com/assets/img/logo.png" />
Note that theimg
tag is also self-closing and should contain a forward slash,/
at the end of the tag to follow best practices.
script
tagFinally, the script
tag, which can be used in a few ways, including adding JavaScript code internally in the HTML document, or externally in a JavaScript .js file.
To include JavaScript internally, you can do the following:
<script>
window.onload = function() {
alert("Hello There!");
};
</script>
This code will display a native alert box with the text "Hello There!" in the body.
Generally, you would include script code like this for third-party services like Google Analytics, Google AdSense, etc.
The second method, which I use for my own JavaScript code, includes a JavaScript .js script file into your HTML document:
<script src="/js/main.js"></script>
Again, it's a much cleaner method, and it keeps your HTML document content separate from your JavaScript code.
Oddly, this looks like it could be a self-closing tag, but you have to include the closing tag after the opening tag, despite there being no contents in between.
Note: The reason why I've included thescript
tag last in this article is because I always include JavaScript code or includes at the bottom of the HTML document, right before the closingtag. I've done this so that the page contents can load and render output to the screen before the script files are loaded. This is also a recommendation from Google to get a higher score when using Google PageSpeed Insights, a tool created by Google to help test page load speeds against several factors like render-blocking scripts, in this case.
These HTML basics and best practices are some of the most commonly used elements and will help you get started creating your very first web site.
You can learn HTML further by checking out additional HTML posts on Orangeable.
Comments
There are no comments yet. Start the conversation!