• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Useful Mixin Functions For Generating Responsive Percentage Widths, Margins and Paddings in LESS

General · February 27, 2013

If you’re not already using LESS, you should be and I advise you start now or else. As you’ll know developing responsive websites involves taking the desired pixel value dividing it by its parent contextual value and then multiplying by 100 to get the desired percentage. I used to use a calculator back in the day before I discovered LESS.

I realised a few developer friends of mine were still doing it the hard way, so I thought I’d share some mixin functions I created and incorporate into my LESS workflow. These could perhaps be merged into one super function, but in responsive development I only ever use percentage widths and margins/paddings left and right values so this works better for me and is for flexible.

Calculating responsive widths:

.pwidth(@target, @context: 960px) {
width: 100% * (@target/@context);
}

Example usage: .pwidth(220px); // Will generate a width of 220px in percentage form based on the parent context

Responsive margin-left

.lmargin(@target, @context: 960px) {
margin-left: 100% * (@target/@context);
}

Example usage: .lmargin(10px); // Will generate a margin-left of 10px in percentage form based on the parent context

Responsive margin-right

.rmargin(@target, @context: 960px) {
margin-right: 100% * (@target/@context);
}

Example usage: .rmargin(10px); // Will generate a margin-rightof 10px in percentage form based on the parent context

Responsive padding-left

.lpadding(@target, @context: 960px) {
padding-left: 100% * (@target/@context);
}

Example usage: .lpadding(10px); // Will generate a padding-left of 10px in percentage form based on the parent context

Responsive padding-right

.rpadding(@target, @context: 960px) {
padding-right: 100% * (@target/@context);
}

Example usage: .rpadding(10px); // Will generate a padding-right of 10px in percentage form based on the parent context

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • How To Paginate An Array In Javascript
  • Waiting for an Element to Exist With JavaScript
  • Reliably waiting for network responses in Playwright
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Calculate A Javascript Date X Months Ago With Vanilla Javascript

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz