惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Latest news
Latest news
NISL@THU
NISL@THU
S
Security Affairs
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
AI
AI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
小众软件
小众软件
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
I
InfoQ
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
H
Heimdal Security Blog
S
SegmentFault 最新的问题

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Learn CSS Positioning
Ahmad Shadeed · 2020-02-02 · via Ahmad Shadeed

Basic Concepts

Default Position

The default position for an element is position: static, which doesn’t have any effect on the element. Consider the next figure where all the cats are sitting together:

A Relatively Positioned Element

An element with position: relative. The top and bottom properties specify the vertical offset from its normal position. The left and right properties specify the horizontal offset from its normal position. In the next figure, we have a naughty cat that walked to the bottom without its owner permission.

An Absolutely Positioned Element

An element with position: absolute. The top and bottom properties specify the vertical offset from its containing block. The left and right properties specify the horizontal offset from its containing block. In the next figure, we have a naughty cat that escaped to the top-left corner without its owner permission.

For absolute positioning, the containing block is the closest positioned parent (An element with a position other than static)

A Fixed Positioned Element

An element with position: fixed. The top and bottom properties specify the vertical offset from its containing block. The left and right properties specify the horizontal offset from its containing block. The containing block for fixed positioned elements is the viewport, except when an element’s parent has one of the following properites applied to it: transform, filter or perspective.

The naughty cat has escaped to the top-left corner of the viewport.

How top, right, bottom, and left works?

Those properties represents the direction of where the element should be positioned, in case it has position: relative. They also represent the boundaries of the containing block, if the element has position: absolute.

Introduction

Before we start, I would like to highlight the two elements we have in the canvas.

  • 1. The wooden wrapper with nail corners.
  • 2. The fabric piece we want to position.

The nail corners are the boundaries that we will use to position the fabric piece.

Relative Position

For positioning to work, we will need to define a parent that the fabric will relate to. To do so, we should add position: relative to the wooden wrapper.

By adding position: relative, it’s like letting the fabric know that its boundaries are the four nails only.

.wood-wrapper {
  position: relative;
}

Absolute Position

position: absolute will position the fabric relative to the wood wrapper, on each nail corner as we declared each one of left, top, right, and bottom.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}

Absolute Position (Eg 1)

position: absolute will position the fabric relative to the wooden wrapper, on each nail corner as we declared each one of left, top, right, and bottom.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}

Absolute Position (Eg 2)

We need to give the fabric a height of 50% while keeping it at the top of the wood. To do that, I removed the bottom property and replaced it with height: 50%.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 50%;
}

Absolute Position (Eg 3)

Now that we added a height, let’s add a width too. The right property is not needed now.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  top: 0;
  width: 50%;
  height: 50%;
}

Absolute Position (Eg 4)

The fabric needs to be positioned from the top to the bottom, with a width of 50%.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 50%;
}

Absolute Position (Eg 5)

An interesting trick for centering an element inside its containing block using all horizontal and vertical properties, in addition to margin: auto.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  width: 120px;
  height: 120px;
  margin: auto;
}

Positioning Outside Parent

position: absolute will position the fabric relative to the wooden wrapper, on each nail corner as we declared each one of left, top, right, and bottom.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: 120px;
}

Centering The Fabric

We want to center the fabric in the wood wrapper, with a width of 200px and a height of 100px.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 200px;
  height: 100px;
}

When we positioned the fabric 50% from the left and top, what’s being centered is its top-left corner, and not the actual fabric.

To solve that, we will need to trick it by using a negative margin with half of the width and half of the height, or we can use CSS transforms as an alternative to margins.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 200px;
  height: 100px;
  margin-left: -100px;
  margin-top: -50px;
}

The alternative way is to use CSS transforms instead of negative margins. By using transform: translate(-50%, -50%), the fabric will be translated 50% of its width to the left, and 50% of its height to the top.

.wood-wrapper {
  position: relative;
}

.fabric {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 200px;
  height: 100px;
  transform: translate(-50%, -50%);
}

Sticky Positioning

The food is centered in the wood wrapper and the cat needs to stick itself to the rope at the top. This is where position: sticky become handy.

The cat will be positioned to the top center of the wood wrapper in order to stick to the rope. Try to scroll inside the next wooden wrapper.

.wood-wrapper {
  position: relative;
}

.the-cat {
  position: sticky;
  top: 0;
}

  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test
  • test

Fixed Positioning

The cat has escaped to the top-left corner of the viewport. This is where position: fixed become handy.

Interactive Demo

Based on your understanding from the above, try to edit the values and see how the result will change.

Learn Box Alignment

I wrote another interactive article about how box alignment works for CSS grid and flexbox. Make sure to check it out!

Learn Box Alignment

Other Articles That I Wrote