30.05.2017 Views

Web_Designer_Issue_262_2017

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UPDATE CSS CUSTOM PROPERTIES WITH JAVASCRIPT<br />

USING CUSTOM PROPERTIES AND CALC() WE CAN ENSURE HIERARCHY AND VERTICAL RHYTHM<br />

In this short tutorial we’ll be creating a simple page<br />

with input controls. The CSS will contain a number<br />

of CSS Custom Properties, which we can update<br />

with input from the user using JavaScript.<br />

We’ll be using the calc() function to set the<br />

font-sizes and spacing, allowing everything to be<br />

updated by changing a single Custom Property.<br />

01. HTML SETUP<br />

We’ll start by building a simple page with a<br />

headline, paragraph and some basic form elements<br />

to control our Custom Properties with input from<br />

the user, using the ‘onchange’ property on the form<br />

elements to fire our JavaScript function.<br />

<br />

...<br />

...<br />

<br />

<br />

02. OUR FIRST CUSTOM PROPERTY<br />

In our CSS we’ll create a :root selector and create<br />

our first Custom Property. Using the double dash<br />

(‘--’) prefix we can then add any name we want, so<br />

we’ll start by creating a base font size.<br />

:root {<br />

--base-font-size: 1rem;<br />

}<br />

03. CALC() MAGIC<br />

Once we’ve created a Custom Property for the<br />

font-size, we can create our spacing unit Custom<br />

Property and base that on a calculation of the<br />

font-size Custom Property using the CSS calc()<br />

BROWSER SUPPORT<br />

At the moment Scroll Snap Points have quite<br />

limited browser support, with Firefox ofering the<br />

best support. However, the specification became<br />

a candidate recommendation in February this<br />

year so wider support should soon follow (srt.<br />

lt/rT2xJr). There’s also a polyfill available to<br />

provide support for Chrome (srt.lt/Er6Ri).<br />

In development<br />

12+<br />

function. Our calculation works like this; 1rem is<br />

equal to 16px (the default browser font-size), so our<br />

base-font-size is 16px. Our Spacing unit is then 16px<br />

multiplied by one and half. So by default our<br />

spacing-unit is equal to 24px.<br />

--spacing-unit: calc(var(--base-font-size) *<br />

1.5);<br />

04. USING OUR CUSTOM PROPERTIES<br />

Now we’ve set our Custom Properties on the :root<br />

element we can use them when styling elements in<br />

the page by using the new syntax var(). Inside this<br />

we recall our Custom Property name and optionally<br />

multiple fallback values, which can be other<br />

Custom Properties or values.<br />

body {<br />

font-size: var(--base-font-size, 1rem);<br />

padding: calc(var(--spacing-unit, 1.5rem) *<br />

2);<br />

}<br />

h1 {<br />

font-size: calc(var(--base-font-size, 1rem) *<br />

2.4);<br />

margin: 0 auto calc(var(--spacing-unit,<br />

1.5rem) * 1.5);<br />

}<br />

05. JAVASCRIPT FUNCTION<br />

Using JavaScript we can take the input from the<br />

range slider to update the Custom Property<br />

controlling the font size, which will in turn update<br />

the spacing unit using calc().<br />

function updateCustomProperties() {<br />

var size = document.getElementById(“getsize”).value;<br />

document.body.style.setProperty(‘--base-font-<br />

NEW GENERATION CSS<br />

size’, size + ‘rem’);<br />

}<br />

We can also use HTML5 colour inputs to change the<br />

background and text colours by adding them to the<br />

HTML and then adding new variables and<br />

setProperty values inside the JavaScript function.<br />

BROWSER SUPPORT<br />

CSS Custom Properties are now supported in the<br />

current version of all modern browsers, Edge<br />

included. Mobile support as of the most recent<br />

versions is good too with only IE, Blackberry and<br />

Opera Mini lacking support.<br />

49+<br />

(Current version 57)<br />

15+<br />

Uses an older version of the<br />

spec (Current version 15)<br />

31+<br />

(Current version 53)<br />

9.1+<br />

(Current version 10.1)<br />

ESSENTIAL READING<br />

• CSS-Tricks<br />

Covers everything you need to know about<br />

front-end development and blurring the lines<br />

into UI/UX, design and back-end development.<br />

CSS Tricks ofers blogs, videos, forums and an<br />

impeccable CSS Almanac making it well worth<br />

adding to your bookmarks.<br />

• Smashing Magazine<br />

Through their articles, digital books, job<br />

resources and industry conferences they<br />

have a lot to ofer anyone in the web industry.<br />

Consistently provides quality resources for<br />

beginners and pros alike.<br />

39+<br />

9+<br />

Above: When the user scrolling has stopped the browser will automatically scroll to the closest snap point<br />

feature ________________________________________________________________49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!