7.1 CSS Positioning & Display Properties
Lesson 3: CSS Positioning & Display Properties
CSS Display Property
Block Elements: Block elements (e.g.,
<div>,<p>,<h1>) take up the full width available and always start on a new line.div { display: block; }
Here are the block-level elements in HTML:
// Block Elements
<address> <article> <aside> <blockquote> <canvas> <dd> <div> <dl> <dt>
<fieldset> <figcaption> <figure> <footer> <form> <h1>-<h6> <header> <hr>
<li> <main> <nav> <noscript> <ol> <p> <pre> <section> <table> <tfoot> <ul> <video>Inline Elements: Inline elements (e.g.,
<span>,<a>) only take up as much width as their content and do not start on a new line.span { display: inline; }Inline-Block Elements: Inline-block elements are like inline elements but allow setting width and height, and they don’t force new lines.
.inline-block { display: inline-block; }
Here are the inline elements in HTML:
Controlling Visibility with
display: none;: Thedisplay: none;property hides an element and removes it from the document flow, making it as if the element never existed.
The display property specifies the display behavior (the type of rendering box) of an element.
CSS Positioning
CSS positioning allows you to precisely control the placement of elements on a web page. It's a powerful tool to create complex layouts and visual effects. There are five main positioning methods:
There are five different position values:

Static Positioning(default): Default position; elements are placed in the normal document flow without any special positioning. You can't move elements out of their normal flow using static positioning.
Relative Positioning: Elements are positioned relative to their normal position. This is useful for slight adjustments. You can use
top,bottom,left, andrightproperties to shift the element.
Absolute Positioning: Elements are removed from the document flow and positioned relative to the nearest positioned ancestor (relative, absolute, or fixed) or the viewport. You can use
top,bottom,left, andrightproperties to position the element.Fixed Positioning: Elements are positioned relative to the browser window(the viewport), and they remain in place even when the page is scrolled.
Sticky Positioning: A hybrid of relative and fixed positioning. Elements become fixed when the user scrolls past them. Note: You must specify at least one of
top,right,bottomorleftfor sticky positioning to work.
Example for Sticky Positioning
Z-index & Layering
The
z-indexproperty controls the stacking order of positioned elements. t determines which element appears on top of others when they overlap. Elements with a higherz-indexwill appear in front of those with a lower one.How it Works:
Positive Values: Higher values bring elements to the front.
Negative Values: Lower values send elements to the back.
Default Value: 0.
Layering Example: You can use
z-indexto stack elements on top of each other. Set the z-index for an image, so that it is displayed behind the text:
Note: Since Element 2 has the highest z-index, it will appear on top of the other two elements. Element 2 will be above <img> Element, and so on.
CSS Layout - Float & Clear
Float and Clear are two CSS properties used to control the layout of elements on a web page. They are particularly useful for creating layouts where elements wrap around one another.
Float Property: The
floatproperty is used to align elements to the left or right, allowing text or other inline elements to wrap around it. Thefloatproperty is used for positioning and formatting content e.g. let an image float left to the text in a container.The
floatproperty can have one of the following values:left- The element floats to the left of its containerright- The element floats to the right of its containernone- The element does not float (will be displayed just where it occurs in the text). This is defaultinherit- The element inherits the float value of its parent
Clear Property: The
clearproperty prevents elements from floating beside a specific element. It's often used to clear floats and return the flow of elements to the next line. In other words, when we use thefloatproperty, and we want the next element below (not on right or left), we will have to use theclearproperty.The
clearproperty can have one of the following values:none- The element is not pushed below left or right floated elements. This is defaultleft- The element is pushed below left floated elementsright- The element is pushed below right floated elementsboth- The element is pushed below both left and right floated elementsinherit- The element inherits the clear value from its parent
Example for CSS Clear
Basic Layout with Float: While floats were widely used in the past, modern CSS layout techniques like
FlexboxandGridoffer more flexible and powerful ways to create complex layouts.
Last updated