DZone
Web Dev Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > 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 · Web Dev Zone · Interview
Like (0)
Save
Tweet
7.75K 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

  • Product Owner Anti-Patterns
  • Top 20 Git Commands With Examples
  • Synchronization Methods for Many-To-Many Associations
  • Choosing Between GraphQL Vs REST

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo