Deep dive into CSS position & Some practical usage
We all love good-looking websites, no one likes a bad User interface, and guess what position is property you can master to create some good websites.

Search for a command to run...
We all love good-looking websites, no one likes a bad User interface, and guess what position is property you can master to create some good websites.

No comments yet. Be the first to comment.
In this article, we will learn how we can convert the uploaded image to NodeJS Express Server to WebP image. Before we dive into code first let's understand Why it's a good idea to convert the image to WebP, and what are the Advantages of it? Let's s...

Hello there, Today I am presenting you some of the Javascript Interview questions, which are important, so let's start it. Scope The Scope is a code block in which values are accessible, let's understand it with an example. function myFunction() { ...

Hello there, in this article I'm going to share JavaScript Array Methods. Not only I will share the methods and usage, but you will find it very useful in practical usage as well. So let's explore them one by one. One last thing to say before we sta...

Nowadays TailwindCSS + CSS combo is ๐ฅ, and I can say now, that it's production ready ๐. Read further to Get started with Tailwind CSS, Let's go. TailwindCSS is a different UI library, imagine it as a stylesheet with a bunch of pre-written CSS class...

Today you will learn about Flexbox. Flexbox nowadays is used so much that you can't ignore it. I love the Flex system in CSS. they give so much control to position elements on the screen. so in this entire reading, we will see such a use case. Let's...

On this page
Let's start learning by practical use case, are you excited? ๐
We use Instagram, Facebook, LinkedIn, or some social platform. Then you must know there is a header at the top that stays at the top, regardless of what we do, whether we scroll or not, it just stays on top.

.header {
position: fixed:
top: 0;
left: 0;
right: 0;
}
by setting position property in CSS, you also can use top, left, right, and bottom properties to give the position.
in this example, I assigned 0 to the top, left & right because I want the header to stick to the top.

You might see this kind of chat box in the bottom-right corner of some websites. well implementing this kind of style is very easy.
.chat-popup {
position: fixed;
bottom: 20px;
right: 20px;
}
see here I added 20px from bottom and right, so it gets some space from corners, and add some nice spacing.
now you can visualize twitter's menu too, Twitter has a sticky menu on the left side, and right side, on the left side there is navigation, on the right side, some trending hashtags, and on the center, there is a feed.
now, let's move to the next example.
this one is very simple, we can position the element with top, left, right, and bottom properties and the element will position itself relative to its position.
usually, this is used with position: absolute; I'll show you in the next one the combined usage of both.

See the image, there is a download button on top of the image, these types of UIs are very common and very useful. here is the code for doing exactly the same.
<div class="image-box">
<img src="https://images.unsplash.com/photo-1657112016041-080a8f4b7a0e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=434&q=80" alt="image" />
<button>Download</button>
</div>
.image-box {
position: relative;
}
.image-box img {
width: 250px;
height: 250px;
object-fit: cover;
border-radius: 18px;
}
.image-box button {
position: absolute;
top: 10px;
right: 10px;
}
explanation: First we made .image-box position: relative; then the important code is for the button, we've to use position: absolute; so the button's position will be an absolute position to its parent, so in our example the parent of a button is .image-box, then we provided top and right position to align it to the top-right corner.
Whether you write it or not, this is the default assignment, by DOM renderers.
This is very useful, consider it as a combination of the position: static; and position: fixed;
when you use this, the content will be statically positioned, but when you scroll and the given position (top, left, right, bottom) matches to current position then it sticks to that position.
HTML
<div class="header">
<button>Full resolution</button>
<button>4k</button>
<button>Full HD</button>
</div>
CSS
.header {
position: sticky;
top: 0;
left: 0;
right: 0;
}
it was pretty long ๐ฅฒ and took me around 2-4 hours to prepare demos and write this blog, if you enjoyed it, Give it like ๐๐ป and share it with your friends.
Happy coding! ๐จ๐ปโ๐ป Bye.