# CSS Position and Units

# What is Position Property

This property in CSS is used to to control how an HTML element is positioned on a webpage. It determines how an element is placed relative to its normal position , its parent element or the browser viewport.

There are 5 Position properties

1. Static
2. Relative
3. Absolute
4. Fixed
5. Sticky

Let discuss it one by one

1. `position:static`

The `static` value is the default positioning of every HTML element. Elements follow the normal document flow and `top` `bottom` `right` and `left` do not affect their position.

2. `position:relative`

The `relative` value positions an element relative to its original position in the normal document flow. You can move it using `top` `right` `bottom` and `left`.

**CSS File**

```css
.image2{
    position: relative;
    top: 100px;
}
```

The element moves 100px down. Its original space remains reserved. You can also use `bottom` `left` and `right` like `top`.

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/9113dc1a-590a-4447-be36-3362c51087a0.png align="center")

3. `position:absolute`

The `absolute` value positions an element relative to its nearest positioned ancestor(an ancestor whose position is not `static`) It is removed from the normal document flow.You can move it using `top` `right` `bottom` and `left`.

It do not reserve the original space.

**CSS File**

```css
div{
    border: 1px solid black;
    position:relative
}
.image2{
   position: absolute;
     top: 300px; 
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/59a44a5d-8010-4fac-bfa0-502f11cd67f2.png align="center")

4. `position:fixed`

The `fixed` value positions an element relative to the browser viewport. It stays in the same position even when the page is scrolled.You can move it using `top` `right` `bottom` and `left`. it do not reserve the original space. It initial output is same like `absolute` value.

**Example:** A floating button that stays at the bottom right corner of the screen.

**CSS File**

```css
div{
    border: 1px solid black;
    height:1800px;
  
}
.image2{
   position:fixed;
     top: 300px; 

}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/24b5b06e-d697-466c-b95e-1df5082bf273.png align="center")

It's real effect is show when we are scrolling the page.

5. `position:sticky`

The `sticky` value allows an element to behave like a normal element initially but it sticks to a specified position when the user scrolls to a particular point. It reserve the original space.You can move it using `top` `right` `bottom` and `left`.

**Example :** A navigation bar that sticks to the top of the page while scrolling.

**CSS File**

```css
div{
    border: 1px solid black;
    height:1800px;
  
}
.image2{
   position:sticky;
     top: 300px; 

}
.image4{
    position: fixed;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/39c5e48e-c8ac-4082-a03c-e2230ca17ba0.png align="center")

We have created two divs. The element with `position: sticky` will remain stuck only until the first div (its parent container) ends, whereas the element with `position: fixed` will remain fixed in the same position even when we scroll into the second div.

# CSS Size Unit

## What is Size Unit

CSS Size units are used to define the dimensions , spacing and positioning of HTML elements. They specify values such as `width` `height` `margin` `padding font-size` .

CSS units are mainly divided into two categories

1. Absolute Units
2. Relative Units

## 1\. Absolute Units

Absolute units represent fixed measurements that do not depend on the size of another element or its parent. e.g `pixels(px)`

**What is** `px`

Pixel (`px`) is an absolute CSS length unit used to define the size of HTML elements, such as width, height, margin, padding, and font size. It represents a specific CSS measurement that does not automatically change according to the size of its parent element or viewport.

## Difference between Physical pixels and logical pixels

**Physical Pixels**

Physical pixels are the actual tiny dots of light present on device's display screen. They represent the hardware resolution of the screen.

For example a display with resolution of 1920 x 1080 has

- 1920 physical pixels in width
- 1080 phusical pixels in height

**Logical Pixels**

Logical pixels are units used by software and browsers to define the size and positions of elements on the screen. They are also called CSS pixels in web development.

```plaintext
.box {
    width: 100px;
    height: 100px;
}
```

## Difference between PPI and DPI

**PPI(Pixels per inch)**

**PPI** stands for *pixels per inch.* It measures the pixels density of a digital display , indicating how many pixels are packed into one inch of screen.

- Higher PPI means more pixels are packed into each inch
- Higher PPI generally produce sharper and more detailed images.

**Example :** A display with 300 PPI has 300 Pixels per inch.

**DPI(Dots per inch)**

**DPI** stands for *Dots per inch.* It measures how many physical ink dots a printer can produce in one inch.

- Higher DPI means generally more detailed printing.
- It is commonly used to describe printer resolution.

**Example :** A Printer with 600 DPI can print up to 600 ink dots per inch.

**HTML File**

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="box">Welcome to Chaicode</div>
</body>
</html>
```

**CSS File**

```css
.box{
   border: 2px solid black;
   font-size: 50px;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/c9a4cbf0-2395-4551-ae1d-98904eefaf7a.png align="center")

# 2\. Relative units

Relative units define sizes based on another value, such as the parent element's size, root element size, or viewport dimensions. e.g `em` `rem %` `vw` `vh`

## What is `em`

The `em` is a relative CSS length unit used to define the size of elements based on the font size of their parent element. It is commonly used for setting font size, margin, padding, and other dimensions.

**HTML File**

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
    <div class="box">
        Welcome to chaicode
    </div>
</div>
</body>
</html>
```

**CSS File**

```css
.container{
    font-size: 10px;
}
.box{
 font-size: 10em;
}
```

parent element's size is multiply by chaild's element size so the result is 10 x 10=100px

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/753a9cf4-006a-446f-8eea-c07f20c007db.png align="center")

## **What is** `rem`

`rem` (Root Em) is a relative CSS length unit that is based on the font size of the root element (`html`). By default, most browsers set the root font size to **16px**.

**CSS File**

```css
#para{
    border: 2px solid black;
    font-size: 5rem;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/af056855-480e-4ddb-868e-5e5442d99274.png align="center")

## **What is** `%`

`%` is a relative CSS length unit that defines the size of an element based on the size of its parent element or another reference value, depending on the CSS property being used.

**HTML File**

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="parent">
    <div class="child">child</div>
</div>
</body>
</html>
```

**CSS File**

```css
.parent{
    border: 2px solid black;
    background-color: beige;
}
.child{
    border: 2px solid green;
    background-color: aqua;
    width: 50%;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/abec12aa-bd88-4a89-aefc-960e24c15189.png align="center")

## **What is** `vh` **and** `vw`

`vh` is a relative CSS unit that represents a percentage of the browser's viewport height. **1vh is equal to 1% of the viewport height.**

`vw` is a relative CSS unit that represents a percentage of the browser's viewport width. **1vw is equal to 1% of the viewport width.**

**CSS File**

```css
.box{
    border: 2px solid red;
    background-color: beige;
    height: 50vh;
    width: 50vw;
}
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/c4b9d2d4-ebd7-4941-8d01-b9a87ca6c398.png align="center")
