Skip to main content

CSS Positions : Everything you must know as a web developer

  

css positions






If you are a front end web developer and want to change the positions of the HTML elements to create a stunning layout, you must know CSS positions property. If you don't know or have doubts, be ready to discover everything you must know about the CSS positions.

In this article, I am going to discuss what are the different CSS positions properties you can use and how to apply them. There are five different position values - 
1. Static
    2. Relative
     3. Absolute
4. Fixed
 5. Sticky

You can use the top, bottom, left, and right properties to position the elements. But, you must declare the value of the position property before positioning. Let's discuss each property with examples.

Static:

Every HTML elements have a default position value to static. You can not change the position of an element having a static position using top, left, right, and bottom property. Its position is determined by the normal flow of the web page. I am not adding any examples as every HTML element is static.

Relative:

You can make an HTML element relative to its original position setting the position property to relative. Then, you can use the top, left, right, and bottom property for positioning the element. It will change only the position of the element to which the property is applied and other elements position will not change. You can understand it completely if you check the following link given below.


See the Pen relative by KAMAL DAS(@daskamal)on CodePen.



Here, in the example, you can find that I have used two divs and the yellow-colored div has the static position and red-colored div has relative position and the div with a static position does not change its position instead of the two divs overlap. Now, you must have the idea of using the top, right, bottom, and left properties. Have you noticed that I set the value of the bottom property for uplifting the red div? Actually, if you change the position of any element, you have to think of a relative position. If you want to change the position of the element to a top position than its original position, you will have to set the bottom value. It is the same for any other properties like left, right or top. If you want your element to be placed more right from before you have to set its left property. 

Absolute:

Now, let's discuss the absolute position. If you set the position property to absolute, the element will be out of the flow of the page and no space will be set for that element. If you add another div or text after an element having absolute position, you will find what I want to mean. Now, you should know the difference in setting position with top, left etc between relative and absolute position. You have learned about relative position and how you can set its position relative to its original position. But, in case of absolute position, the value of top, left, etc position changes the position of the element with respect to its parent div ( in case of no parent div, it is positioned with respect to the web page). You can better understand by example. So, check the code given below.

See the Pen absolute by KAMAL DAS (@daskamal)on CodePen.


Fixed:

If you are creating a website and want to have a fixed navigation bar that remains at the same place even on scrolling, you have to set the position property to fixed. You can take a look at the code to understand how it works.



See the Pen fixed by KAMAL DAS (@daskamal)on CodePen.



Sticky:

The sticky position of an element depends on the scroll position. You must specify the position using the top or left or right or bottom property for sticky position to work. Check the following example for understanding the sticky position property.

See the Pen ExKWBVp by KAMAL DAS (@daskamal)on CodePen.


Z-index: 

I want to discuss another property of CSS in this context. You might have noticed that if in the example of relative position that the two overlap and it was the second element on top of the first one. But, how can you change it and make the first one on the top and the last one at the bottom ? The z-index property does this job for you. You have to set higher the z-index value to the element which one you want on top. Find the following example for understanding how it works.


See the Pen z-index by KAMAL DAS (@daskamal)on CodePen.


To understand CSS positions property completely, you should make your hands dirty with some coding practice. You have already learned a lot of things and use this knowledge in your next project. If you have any doubts or find anything wrong, feel free to contact me. If you like this article, please share and comment so that other students can take help from this.

Happy coding 😄.

Comments

Popular posts from this blog

How to create CSS flip animation on hover ?

  Don't you want your website to seem attractive and user loving? I am sure you want to. You may have come up with an idea to make your website alive by using a flip animation on hover but don't know how to apply the CSS flip animation. Then, this article is for you. I am providing the code needed for creating this CSS flip animation on hover. First, check the code given below and I will discuss the most important properties needed for completing it. You need to know some CSS properties to create CSS flip animation. You can find those properties in the given code itself. The following properties you should learn about-  perspective CSS transitions CSS positions transform CSS gradients Read more  How to create CSS text glow animation? Now, I am going to discuss the steps that you need to follow for creating this cool flip animation. Let's first discuss the HTML code. Follow these steps- Create a div that will contain the card and add a class. (eg. card-flip). Inside that d...

Master CSS transitions in a few minutes !

    Ask yourself, don't you want your website to seem attractive and users to love it? I am sure you want to. Apply some cool CSS animation effect to make your website alive. I am here to teach you how to apply  CSS transitions  for creating some amazing animation effects. CSS Transitions: First, you need to know what  CSS transitions  is and what it can do for you before applying it.  CSS transitions  allows you to create smooth change or transition of a CSS property, over a given duration. Let's take an instance. You want an HTML element to be larger than previous on hover. If you just set its height and width to the value you want, you will find that on hover the element becomes larger on a sudden. Do you want this? Doesn't it look a bit odd? Now, think if it changes smoothly how it would be. It would look good, right. So, how can you do so? Yes, you are right.  CSS transitions  can help you to create this effect. So, be ready to lear...