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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. CSS from the Definition Up: Keeping Boxes Square

CSS from the Definition Up: Keeping Boxes Square

John Esposito user avatar by
John Esposito
·
Nov. 08, 11 · Interview
Like (0)
Save
Tweet
Share
7.94K Views

Join the DZone community and get the full member experience.

Join For Free

Today I came across a slick little HTML5 tool called Fluid Squares -- keeps squares square even within a fluid layout. Neat, but pretty darn specific. (Though maybe not all that specific, if the grid layout lies at the heart of much 20th-century design, and perhaps enjoys deep roots in human visual processing.)

But Fluid Squares is now in version 2, and the difference between v1 and v2 is what really caught my eye.

Version 1 uses transparent images to maintain square proportions -- which seems a little old-school hackish (remember invisible spacer gifs?).

Version 2 is much improved by Marco Lago's careful interpretation of the CSS box model.

Here's how simple it is to maintain equal measurement along two dimensions:

It is possible to make full CSS fluid squares without images hack or javascript workarounds? YES! Just think how paddings (and margins) works in the box-model definition. If the vertical paddings (and margins) are specified in percent (%) values the size is a percent of the width of the containing element.

So if you write:

width: 50%;
height: 0;
padding-bottom: 50%;

you get a fluid square box with only a three row CSS declaration.

The relevant spec passage isn't actually located in the box model definition. Instead, this text:

<percentage>
Specifies a percentage width. The percentage is calculated with respect to the width of the generated box's containing block. If the containing block's width depends on this element's width, then the resulting layout is undefined in CSS 2.1.  

is contained in a subsection of the 'Visual formatting model details' chapter, right after the definition of 'containing block'.

This isn't just smart CSS; it's drilling down to the exact specifications, and turning an element's basic definition into an elegant, real-world solution.

You can find plenty of great CSS snippets on the web, but not too many grow so immediately from the definition of the element itself. Reading specs carefully pays off, I guess, even for very small (but, for heavy gridders, quite significant) applications.

 

CSS

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Elasticsearch Works
  • Monolithic First
  • Introduction to Container Orchestration
  • Apache Kafka Is NOT Real Real-Time Data Streaming!

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

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: