DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

[DZone Research] Observability + Performance: We want to hear your experience and insights. Join us for our annual survey (enter to win $$).

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Elevating Web Design: The Art and Science of CSS Nesting and the Cascade
  • Demystifying the ‘Class’ Attribute in HTML: A Comprehensive Guide With Examples
  • Introduction To Template-Based Email Design With Spring Boot
  • Front-End Development Trends

Trending

  • GenAI-Infused ChatGPT: A Guide To Effective Prompt Engineering
  • Auto-Scaling DynamoDB Streams Applications on Kubernetes
  • LLMs for Bad Content Detection: Pros and Cons
  • AI for Web Devs: Project Introduction and Setup
  1. DZone
  2. Coding
  3. Languages
  4. Relative CSS Positioning: Parent vs. Self

Relative CSS Positioning: Parent vs. Self

Niels Matthijs user avatar by
Niels Matthijs
·
Mar. 28, 13 · Interview
Like (0)
Save
Tweet
Share
8.11K Views

Join the DZone community and get the full member experience.

Join For Free

even though css has been getting a lot of love the past few years (with plenty of animation options, overall visual effects and more granular targeting controls added to the spec), positioning elements remains a big weak spot of css. while page layouting is slowly getting there (the grid and flexbox modules are advancing steadily), we're still missing out on some necessary tools when it comes to relative positioning of elements with css.

relative positioning

when talking about "relative positioning" i mean positioning an element relative to one of its parents (not just limited to position:relative). most positioning work is done between components residing on the same dom level and even though there is still plenty of room for improvement there too (cfr grid and flexbox modules), people often tend to forget about relative positioning (mostly because at first glance it seems like everything is already there).

relative positioning as it exists now is mostly focused on the parent container. any type of positioning you're trying to do, you'll be working with the width of your parent container. but sometimes you want to position something based on the dimensions of the element you're positioning (not to worry, i'll come up with two good examples in a bit). that's when it suddenly become tricky. first things first though.

parent-based relative positioning

 /* html */ <div class="parent"> <div class="self"> ... </div> </div> /* css */ .parent {widht:800px;} .self {padding:0 25%;} /* padding */ .self {margin:0 25%;} /* margin */ .self {position:relative; left:25%;} /* relative */ .self {position:absolute; left:25%; right:25%; top:0 bottom:0;} /* absolute */ 

in the 4 css examples above you'll find 4 different methods for relative positioning. in all 4 examples i've used offsets of 25% which will all result in an effective offset of 200px (since the values are computed based on the width of the .parent element). the reality of the matter is a bit more complex than i put it here (for example, a percentual top-padding is also based on the width of its parent and not on the height as you might expect - which does lead up to one of two ways to pull off proportional fluid behavior ) but that's a little out of scope for this article.

this is all nice and well, but what if we want to define an offset on an element's own dimensions?

self-based relative positioning

self-based relative positioning becomes essential when you want to center an element. there are two common situations where i've needed this so far:

  • center-cropping images
  • centering modals/overlays
    .self {position:relative; left:50%; margin-left:-(50% of width .self); top:50%; margin-top:-(50% of height .self);} 

a common technique to do this kind of centering is to first align the left side of .self to the middle of its .parent (ie position:relative; left:50%), then pull .self back to the left by assigning a negative margin half the width of .self. that effectively centers an element horizontally. this is fine if you know the dimensions of .self beforehand, but when dealing with variable dimensions (like the dimensions of an image or the height of an overlay) you're pretty much stuck.

 .self {transform:translatex(-50%) translatey(-50%);} 

it turns out there is actually one property that does exactly this, the downside is that support for css transforms isn't what you call universal and it still relies on a heavy set of prefixes to work cross-browser. a minor downside that can easily be captured in some fallback javascript, but even then using this somehow feels like a hack (or substitute) for a property that doesn't exist just yet.

conclusion

it would be nice to get a few extra options to position elements based on its own dimensions rather than the dimensions of its parent. for now we can make do with the solution above, but it does feel kinda icky and javascript support is required for graceful degradation (whether it degrades gracefully without javascript is up for discussion).

it could be that i missed a css property somewhere that exhibits the same behavior, if so please let me know in the comment section as i'm dying to find a better solution for this.




CSS Self (programming language)

Published at DZone with permission of Niels Matthijs, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Elevating Web Design: The Art and Science of CSS Nesting and the Cascade
  • Demystifying the ‘Class’ Attribute in HTML: A Comprehensive Guide With Examples
  • Introduction To Template-Based Email Design With Spring Boot
  • Front-End Development Trends

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: