- Positioning Blocks Defined by <div> Tag
- CSS Float property
- Negative Margins
- Relative+absolute positioning
- How to style <div> tag?
The <div> tag is an empty container that is used to define a division or a section. It does not affect the content or layout and is used to group HTML elements to be styled with CSS or manipulated with scripts.
Positioning Blocks Defined by <div> Tag
We recommend to use <div> tag only when no other semantic elements introduced in HTML5 (such as <nav> , <main> or <article>) are appropriate.
Since <div> is a block-level element, a line break is placed before and after it.
It is possible to place any HTML element within a <div> tag, including another <div>.
The <div> tag can NOT be inside <p> tag, because the paragraph will be broken at the point, where the <div> tag is entered.
To apply styles inside a paragraph use <span> tag, which is used with inline elements.
The <div> tag comes in pairs. The content is written between the opening (<div>) and closing (</div>) tags.
Example of the HTML <div> tag:
When we build layouts, we deal with multiple blocks defined by the <div> tag. The positioning of these blocks is at the heart of layout: placing elements in the correct relative positions across all screen sizes is one of the most important tasks.
Depending on the content and the goals of the page, we can use different techniques (or their combinations) to determine the place of each block. Let’s learn more about these techniques.
In modern websites float-based layouts are being replaced with Flexbox. The main idea behind Flexbox is that with it, you can control the alignment, direction, order, and size of the items inside the container.
Example of a Flexbox with HTML <div> tag:
CSS Float property
CSS float property, or “floats” allows elements to appear next to, or apart from, one another, which lets us create different types of layouts, including multi-column pages, sidebars, grids, etc.
Example of the HTML <div> tag with the CSS float property:
Negative margins can be applied to both static or floated elements.
Example of negative margins:
If you want to position div relative to particular element you can use a combination of position: relative and position: absolute.
Example of relative+absolute positioning of HTML <div> tag:
position: relative does not affect the positioning of elements in normal flow unless you add offsets.
|Was used to align the content inside a <div> tag.|
This attribute is not supported in HTML5. CSS text-align property is used instead.
The <div> tag also supports the Global attributes and the Event Attributes.
How to style <div> tag?
Common properties to alter the visual weight/emphasis/size of text in <div> tag:
- CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
- CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
- CSS font-size property sets the size of the font.
- CSS font-weight property defines whether the font should be bold or thick.
- CSS text-transform property controls text case and capitalization.
- CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.
Coloring text in <div> tag:
- CSS color property describes the color of the text content and text decorations.
- CSS background-color property sets the background color of an element.
Text layout styles for <div> tag:
- CSS text-indent property specifies the indentation of the first line in a text block.
- CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
- CSS white-space property specifies how white-space inside an element is handled.
- CSS word-break property specifies where the lines should be broken.
Other properties worth looking at for <div> tag:
- CSS text-shadow property adds shadow to text.
- CSS text-align-last property sets the alignment of the last line of the text.
- CSS line-height property specifies the height of a line.
- CSS letter-spacing property defines the spaces between letters/characters in a text.
- CSS word-spacing property sets the spacing between words.