Everything About CSS Positions And Its Helper Properties

Home » CSS » Everything About CSS Positions And Its Helper Properties

Have you ever wondered how display properties make an HTML element flow? Most of the layout models, like box-model, flex-model, and float-model use a Static Flow. meaning all the elements will take up space in the browser window as defined by its flow model. To make it understandable, All elements will flow on the page as they are defined in browser style. Elements like div will use full-width space, elements like strong, em, span, p will use inline flow.

Sometimes we have to put an element out of the flow, and sometimes move in the flow. We can do that using the CSS position property and some helper properties. We can simply set position values to static, relative, absolute, fixed or sticky to achieve this. All these property values behave differently in terms of flow. Let’s discuss how to use position properties like a pro in this article.

CSS syntax:

Selector{ position: <value>;}

Javascript syntax

document.getElementById('selector').style.position = "<value>";

<value>: can be either static, relative, absolute, sticky, or fixed

We can categorize these values into two groups. Values that put elements out of flow and values that do not put elements out of the flow. When you set one CSS position value and try to switch it between other values, you’d see other elements jumping around. That behavior is because some values move elements out of the flow.

When an element is moved in or out of the flow, there are some helper properties that we can use to place the element at its desired position. Those helper properties are :

  • top: valid positive width value
  • left: valid positive width value
  • right: valid positive width value
  • bottom: valid positive width value
  • z-index: valid integer value

Using Helper Properties:

When these properties are used with a relative, absolute, or fixed they move an element to the desired point on the screen from the top and left corner. This behaviour is also dependent on its closest related child, if not the body. I use a combination of two properties, like ( top and left ) or ( top and right ) or (bottom and left) or (bottom and right).

There is a reason I avoid using opposite properties, mainly because it can create confusion while using CSS absolute positioning. Results may differ on different screen sizes. The majority of the time, using the top will make the bottom ineffective, and using the left will make the property right ineffective.

The bottom and right properties will be ignored if height and property are set. Making the element snap to the top-left end. Try the below example to understand problems that might occur due to using opposite properties. Try checking any opposite properties and observer how the bounding box is not sitting inside the box.

When two or more elements are moved in or out of the flow, they might overlap with each other. The simple solution to arrange them in the correct depth is by using z-index. However z-index property has more complex usage when using multiple layers.

Now let’s look at the CSS position values and how they affect the flow of the layout.

Throughout the article, we’ll use the same HTML structure as defined:

<div class="room">
<div class="bed"></div>
<div class="bed"></div>
<div class="bed"></div>
<div class="bed"></div>
<div class="kid bed-pos"></div>
<div class="bed"></div>
<div class="bed"></div>
</div>
</div>

We’ll change the CSS position property of the .kid class and observe the various behaviours.

Using CSS Position Property

CSS Position: static

This is the default value of position when we don’t specify anything. This position will take up the needed space and keep the layout in flow.

Our technical experts can help fix any issue you are having with your website, regardless of its complexity.

FIND OUT MORE

For example, try to GRAB THE SLEEPWALKING CHILDObserve, helper properties change when you grab the element. But visually there is no change. This is because helpers don’t work on static positions for all browsers.

CSS Position: relative

Just in addition to static positions, relative CSS positions make helper properties work. In other words, top, left, right, bottom properties will start to affect the layout when a position is relative.

The element moves into a specified position because of helpers, which are out-flow behaviour, but by moving this relative position element, we are not adjusting or removing its original space occupied. Meaning, there will be a blank spot at the object’s static position. This is in-flow behaviour. So relative acts as in-flow as well as out-flow.

In the example below, try to move the kid element with the mouse, Viola.

You will be able to move the element from the original position, keeping the static space intact even after dragging the element. The static CSS position is shown as a dotted line in the demo

CSS Position: absolute

When a CSS position is set to absolute, it will take the element out-flow and the static space occupied by the positioned element will be collapsed. The positioned element will move relative to its closest relative parent, the document itself, otherwise.

In the example below, observe the bounding rectangle; the space of the walking child in the earlier example is occupied by the next element. This is the normal behaviour of the positioned elements.

This property allows the designer to manipulate elements as needed because of its out-flow behaviour. Helper properties will work the same as defined above.

position: fixed

Fixed positioned CSS elements share the same properties as absolute in terms of flow and helper properties. Meaning, It will move elements out of the flow, the element can be moved using helper properties. But these elements will stay in their position even after page scrolling.

Fixed is a relative killer because its coordinates don’t react to any relatively positioned element. The element will place itself about the entire document.

Until…

It has an ancestor with a transform, perspective, or filter property set to anything other than none. If you have any container with the above properties enabled, the element will position itself inside the container, losing its ability to stay fixed on scroll.

Try the above example, The kid element is fixed. It has a parent container with position:relative. Grab the kid, Observe, the element coordinate reacts to the document and not a relative parent.

position: sticky

In addition to the fixed-positioned element, think of sticky as a combination of both fixed and relative.

Only the top and left helper properties work with a sticky CSS position. When any helper property is set with sticky, the element will move up and down or side-by-side when you scroll. The element will stay fixed within its related parent.

In the example below, try to scroll the window both horizontally and vertically. Grab the child and move it around, and check the scrolling again. Observe, the element never goes out of its relatively positioned element. Even after specifying higher values than the container, it will stay inside the container.

There you have it. I have covered the most useful CSS positioning with examples.

Our technical experts can help fix any issue you are having with your website, regardless of its complexity.

FIND OUT MORE

Final Thoughts:

It’s no wonder most of the web uses statically positioned elements. Some enhancements are made over time by UI developers to various components of the site using positioned and fixed elements. We see positioned elements in various UI elements. To name a few multi-level navigations, sticky menus, overlaid elements, cards, lightboxes, chatboxes and many more. The position is too small a property, but once you know the fundamentals of each type, you’d be able to solve many complex problems and develop more complex layer-based designs.

Continue Reading

rhodium

Rhodium Developers

Framework: WordPress – Elementor
Requirement: New custom-made website with SEO optimization
shangri_la
development

Problem

It’s a start-up company and it did not have enough stuff to showcase its work. It faced the challenge of establishing a strong online presence to attract potential clients. They wanted a visually appealing website that would convey their professionalism and expertise.

Our Plan

Execution

1A premium website template was designed in Figma. Blueprints of the home page and other internal pages were created. The color palette has been chosen wisely to exude elegance without compromising usability – sleek beige typography against crisp black backgrounds creates an impactful visual contrast that enhances readability and attention. 

2By using strong visuals such as high-quality images and graphics showcasing their past projects, we aimed to convey their expertise and build trust with potential clients. The overall tone we maintained throughout the design process was professional yet approachable, reflecting the client’s brand image as a reliable and innovative construction company.

3 By incorporating intuitive navigation features and user-friendly interfaces, we ensured seamless browsing experiences for potential clients. By focusing on creating internal pages that offered valuable content and intuitive navigation, we were able to provide users with an immersive and informative experience. With intuitive menu options, easy-to-use contact forms, and visually appealing project showcases, visitors are certain to find exactly what they need with minimal effort.

4 One of the primary goals for this project was to showcase the exceptional quality of their work through an impressive gallery section. By leveraging the power of visually appealing images and intuitive navigation, we successfully transformed their website into a virtual gallery that entices visitors to explore more.

5 A major challenge encountered during the project was effectively showcasing Rodium’s unique approach to building construction. By incorporating captivating visuals and interactive elements, such as 2D models and virtual tours, the website successfully demonstrates the intricate processes involved in constructing high-quality buildings. Additionally, clear and concise descriptions were implemented to explain each step of the construction journey, making it easily understandable for potential clients who may not be familiar with technical jargon.

Final Testing

During the testing phase, every aspect of the website will be put under a microscope to ensure its seamless functionality and performance. From checking cross-browser compatibility to ensuring smooth navigation on different devices and screen sizes, no stone will be left unturned. The comprehensive testing process aims not only to identify any potential glitches or bugs but also to guarantee that the user experience is intuitive and flawless.

By utilizing the latest techniques in search engine optimization algorithms, this website is now primed to attract organic traffic and boost its online presence.