How To Style the HTML element with CSS
This tutorial will introduce you to styling the HTML Content Division element—or element—using CSS. The element can be used to structure the layout of a page and break up a webpage into separate components for individual styling. In this tutorial, you will create and style elements, as well as learn how to add and style other elements inside a container. These skills will prepare you to use elements as layout tools later on in the series when you begin recreating the demonstration website.
The
tags to an HTML document. On its own, the
Prerequisites
To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project.
Exploring the Element in Practice
Let’s try a hands-on exercise to study how the element works. Erase everything in your styles.css file (if you added content from previous tutorials). Next, add the following CSS rule for the tag selector:
div background-color: green; height: 100px; width: 100px; >
Save the styles.css file. Next, return to your index.html file, erase everything that’s there (except for the first line of code: ) and add the following code snippet:
Notice that the element has opening and closing tags but does not require any content. Save the index.html file and reload it in your browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser).
Your webpage should display a green box 100 pixels wide and 100 pixels tall as specified by the CSS rule:
. div tag creates separate boxes or containers for all elements inside this tag like text, images, paragraphs.
Properties | Div Tag | Span Tag |
---|---|---|
Elements Types | Block-Level | Inline |
Space/Width | Contain Whole Width Available | Takes only required Width |
Examples | Headings, Paragraph, form | Attribute, image |
Uses | Web-layout | container for some text |
Attributes | Not required,with common css, class | Not required,with common css, class |
The span tag does not create a line break similar to a div tag, but rather allows the user to separate things from other elements around them on a page within the same line. avoiding of line break, results only that selected text to change, keeping all the other elements around them same. Below example will display the difference between span and div tag while div tag contains whole width and span tag contain only required width and rest parts are free for another element.