POSITIONING WITH CSS

css3In the normal flow, the elements are positioned starting from the top left and following one another down to the right, until they occupy all the available space. Elements spread out across the edges. One of the systems used to vary this scheme is positioning, through which, by acting on the position property, the position of an element can be varied with respect to its original position in the document and also with respect to the other elements that surround it. Another method to vary the default schema is the floating, with which you can create the columns. Floating is allowed by the float property. There are five types of positioning that can be used, through the position property to control the arrangement of the different elements on a page.

TYPES OF POSITIONING

  • Static;
  • Relative;
  • Absolute;
  • Fixed;
  • Sticky

You can also use the floating technique, the float property that we will see later. The elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.

POSITION STATIC

HTML elements are placed static by default. These elements are not affected by the top, bottom, left, and right properties. An element with a static position is not positioned in any special way; it is always positioned according to the normal flow of the page. Static positioning as mentioned is the default that each element gets. It simply means “put the element in its normal position in the document flow”.

Copy to Clipboard

POSITION RELATIVE

A relative position element is positioned relative to its normal position. Setting the top, bottom, left, and right properties will cause it to be moved from its normal position. The other contents will not fit into the space left blank by the element. Relative positioning is the first type of position we will look at. This is very similar to static positioning, except that once the positioned element has taken its place in the normal flow of the page, you can change its final position, including overlapping other elements on the page.

Copy to Clipboard

POSITION ABSOLUTE

An element with absolute position is positioned relative to the closest positioned ancestor (rather than positioned relative to the viewport, such as fixed). However, if an absolutely positioned element has no positioned ancestors, it uses the body of the document and moves along with the page scroll. An absolutely positioned element no longer exists in the normal document flow. Instead, it sits on its own level separate from everything else. This is very useful: it means we can create isolated UI features that don’t interfere with the layout of other elements on the page. For example, pop-ups, control menus, rollover panels, UI features that can be dragged and dropped anywhere on the page, and so on. Top, Bottom left and right behave differently with absolute positioning. Rather than positioning the element based on its relative position within the normal flow of documents, they specify the distance the element should have from each side of the containing element.

Note: Absolutely positioned elements are removed from the normal flow and can overlap elements.

Copy to Clipboard

POSITION FIXED

An element with fixed position is positioned relative to the viewport, which means that it always remains in the same position even if you scroll the page. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a blank space on the page where it would normally have been placed. It works in exactly the same way as absolute positioning, with one key difference: while absolute positioning fixes an element in position relative to its closest positioned ancestor (the initial containing block if there isn’t one), fixed positioning usually fixes an element in position relative to the visible part of the window. (An exception to this occurs if one of the element’s ancestors is a fixed containing block because its transform property has a value other than none.) This means that you can create useful fixed UI elements, such as persistent navigation menus that are always visible no matter how far the page scrolls.

Copy to Clipboard

POSITION STICKY

A sticky position item is positioned according to the user’s sliding position. A permanent element alternates between relative and fixed positions, depending on the sliding position. It is positioned relative until a certain offset position is satisfied in the window; hence, it “sticks” into position (like position: fixed).

Note: Internet Explorer does not support permanent placement. Safari requires a -webkit- prefix. You must also specify at least one of the top, right, bottom, or left values for permanent positioning to work.

Copy to Clipboard

THE DISPLAY PROPERTY

The display property is the most important CSS property for controlling the layout. This property specifies if/how an element is displayed. Each HTML element has a default display value depending on the element type which can be block or inline. A block level element always starts on a new line and occupies the entire available width (extends left and right as far as possible). An inline element does not start on a new line and only occupies the necessary width.

DISPLAY NONE

Display none is commonly used with JavaScript to hide and show elements without deleting and recreating them. The <script> element uses display: none; as default. As mentioned, each element has a default display value. However, this value can be overwritten. Changing an inline element to a block level element, or vice versa. This can be useful for giving the page a specific look and continuing to follow web standards.

Note: Setting the display property of an element only changes the display mode of the element, NOT the element type. So, in an inline element with display: block; it is not allowed to have other blocking elements inside it. You can hide an element by setting the display property to none. The element will be hidden and the page will be displayed as if the element was not present:

THE VISIBILITY PROPERTY

visibility: hidden; it also hides an element. However, the element will still take up the same space as before. The element will be hidden, but it will still affect the layout:

THE Z-INDEX PROPERTY

The z-index property specifies the stack display order of an element. When elements are placed, they can overlap other elements. The z-index property specifies the stack order of an element (which element should be placed in front of or behind the others). An element can have a positive or negative stack order.

Note: z-index only works on elements positioned absolutely, relative, fixed, sticky and flex items (which we will see later).

PROPERTY DISPLAY CODE EXAMPLE

Copy to Clipboard

THE Z-INDEX PROPERTY CODE EXAMPLE

Copy to Clipboard

OVERFLOW

The overflow property controls what happens to content that is too large to fit into an area. Specifies whether to crop content or add scroll bars when the content of an element is too large to fit into the specified area.

The overflow property has the following values:

  • visible – Default. The overflow is not clipped. The content is displayed outside the element frame
  • hidden: the overflow is clipped and the rest of the content will be invisible
  • scroll – The overflow is clipped and a scroll bar is added to see the rest of the content
  • auto – Similar to scrolling, but adds scroll bars only when needed

Note: The overflow property works only for block-level elements with a specified height.

The overflow-x and overflow-y properties specify whether to change the content overflow only horizontally or vertically (or both). The overflow-wrap property specifies whether or not the browser can break lines with long words if they overflow from its container.

Copy to Clipboard

FLOAT AND CLEAR

The CSS float property specifies how an element should float.
The CSS clear property specifies which elements can float next to the cleared element and on which side.

The float property is used to position and format the content, e.g. let an image float to the left of the text in a container. The float property can have one of the following values:

  • left – The element floats to the left of its container
  • right – The element floats to the right of its container
  • none – The element does not float. This is the default
  • inherit – The element inherits the float value of its parent

When we use the float property and want the next element to be below (not to the right or left), we will need to use the clear property. The clear property specifies what should happen with the element next to a floating element.

The clear property can have one of the following values:

  • none – Element is not pushed under left or right float elements. This is the default
  • left – The element is pushed under the left float elements
  • right – The element is pushed under the right float elements
  • both – The element is pushed under the left and right float elements
  • inherit – The element inherits the clear value from its parent

If a floating element is higher than the containing element, it will “overflow” out of it. We can then add the clearfix property to solve this problem.

FLOAT EXAMPLE CODE

Copy to Clipboard

CLEAR EXAMPLE CODE

Copy to Clipboard

EXAMPLE CODE

Copy to Clipboard
Copy to Clipboard
Copy to Clipboard

LINKS TO PREVIOUS POST

PREVIOUS POST LINKS

LINK TO THE CODE ON GITHUB

GITHUB