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
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • How Agile Works at Tesla [Video]
  • Integrating AWS With Salesforce Using Terraform
  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)

Trending

  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • How Agile Works at Tesla [Video]
  • Integrating AWS With Salesforce Using Terraform
  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
  1. DZone
  2. Coding
  3. Languages
  4. Proportional Responsive CSS: An Expansion of the Padding Solution

Proportional Responsive CSS: An Expansion of the Padding Solution

Niels Matthijs user avatar by
Niels Matthijs
·
Oct. 15, 13 · Interview
Like (0)
Save
Tweet
Share
5.84K Views

Join the DZone community and get the full member experience.

Join For Free

Earlier this year, I wrote a post on proportional responsive css, detailing two techniques in order to create proportionally responsive elements. The third option I'll be detailing here is little more than an expansion on the padding solution, but one that will save you the need for an extra structural wrapper at the cost of out of the box browser compatibility.

The padding technique is based on the fact that top/bottom padding is relative to the width of its parent. Exploiting this behavior, you can easily create a rectangle that grows and shrinks proportionally. Once you have the proportional box, you can span (position:absolute and all sides set to 0) a deeper nested box across the parent box. For this technique you need two extra wrappers, though. One to set the padding (as it only works on the width of its parent), and one to span the content across the box.

/* html */

<section class="list"> 
  <div class="atom"> 
    <div class="content"> ... </div> 
  </div> 
  <div class="atom">...</div> 
</section> 

The .atom elements are our proportional blocks, the .content elements are the spanned elements that contain the actual content of the block. Now for the CSS:

 /* css */ 
.list {overflow:hidden;} 
.atom {width:25%; float:left; position:relative;} .
.atom:before {content:""; display:block; padding-top:100%;} 
.atom .content {position:absolute; left:0; right:0; top:0; bottom:0;} 

The trick is actually pretty simple. We simply use a pseudo-element to trigger the padding magic, which means we don't need the extra structural wrapper in the HTML. The pseudo-element is set to display:block, so it acts like a regular structural element, then it's given a padding depending on the ratio you want to support (100% makes a square).

The problem with pseudo-elements is that they are not supported in IE7. JavaScript fallbacks are easy to do (just insert a div with JS and apply the exact same styling), but ideal it is not. Then again, if you don't need to support IE7, this third technique makes things a lot easier.


CSS

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

Opinions expressed by DZone contributors are their own.

Trending

  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • How Agile Works at Tesla [Video]
  • Integrating AWS With Salesforce Using Terraform
  • Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)

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

Let's be friends: