Web_Designer_UK__May_2018
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Tutorials<br />
Shrink your code with CSS4 variables<br />
Is it safe to use<br />
CSS variables?<br />
As with any relatively new web<br />
technologythequestionevery<br />
web designer wants to know is:<br />
“what’s the support like?”. The great<br />
news if you visit www.caniuse.<br />
com is that currently most of the<br />
major browsers support this, the<br />
exceptions being IE, Opera Mini and<br />
UC Browser for Android.<br />
So all in all this has very good<br />
support from the main desktop and<br />
mobile browsers at just under 79%<br />
browser support globally. As always<br />
whether you can use this depends<br />
on who your audience is.<br />
If you need to know more about<br />
CSS variables then the Mozilla<br />
Developer Network has good<br />
documentation on this, and it’s easy<br />
to understand (https://developer.<br />
mozilla.org/en-US/docs/<strong>Web</strong>/<br />
CSS/Using_CSS_variables). A more<br />
in-depth look at CSS variables,<br />
featuring a good description of why<br />
you would want to use them can be<br />
found on Medium (https://medium.<br />
com/dev-channel/css-variablesno-really-76f8c91bd34e).<br />
padding: 20px;<br />
text-align: center;<br />
background-color: var(--alt-bg);<br />
color: var(--main-type);<br />
}<br />
15. Body building<br />
The next part of the page to be targeted is the body<br />
element, so find the appropriate CSS that controls the<br />
look of this. The background will now be controlled by the<br />
main background CSS variable to give global control to<br />
the variables.<br />
body {<br />
margin: 0;<br />
padding: 0;<br />
font-family: “HelveticaNeue-Light”,<br />
“Helvetica Neue Light”, “Helvetica Neue”,<br />
Helvetica, Arial, “Lucida Grande”, sansserif;<br />
font-weight: 300;<br />
line-height: 1.4em;<br />
background: var(--main-bg);<br />
}<br />
16. Box fresh<br />
The ‘box’ CSS is going to be updated now, so find the<br />
code for this and change both the background colour<br />
and text colour to have control from CSS variables. Save<br />
this CSS page and just check the page has updated in the<br />
browser. Now move into the ‘index.html’ page.<br />
.box {<br />
float: left;<br />
width: calc(var(--width) - 40px);<br />
margin: 20px;<br />
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);<br />
background: var(--alt-bg);<br />
color: var(--main-type); }<br />
17. Adding buttons<br />
In the ‘index.html’ page find the main header section and<br />
just after the heading two tag, some content will be<br />
added. Just to help you locate this in the code we’ve<br />
shown where the code will go with the HTML comment<br />
in this step. Once you’ve found it add the code in the<br />
next step.<br />
<br />
Make Me Hungry<br />
<br />
<br />
18. Span and chips<br />
The span is added to the page. This gives the instruction<br />
to change the theme. Two buttons are placed after this,<br />
and they will talk to the JavaScript to update the CSS<br />
variables so that they switch between light and dark.<br />
Choose Theme LIGHT<br />
DARK<br />
19. Stick to the script<br />
The code here should be added right before the final<br />
body tag of the page. The code in the remaining two<br />
steps will be placed inside these script tags. This function<br />
will be used to set a specific CSS variable with a value<br />
dynamically. Change it at run time via user control.<br />
<br />
function setDocumentVariable(propertyName,<br />
value) {<br />
document.documentElement.style.<br />
setProperty(propertyName, value);<br />
};<br />
<br />
20. Going dark<br />
When the user clicks the ‘dark’ button the function here<br />
will be called. As you can see it sets three variables with<br />
new values and these update the page immediately,<br />
changing the theme. Save this and try the button in your<br />
browser to see the theme change.<br />
function dark() {<br />
setDocumentVariable(‘--main-bg’,<br />
‘#918d82’);<br />
setDocumentVariable(‘--alt-bg’,<br />
‘#57534c’);<br />
setDocumentVariable(‘--main-type’,<br />
‘#ddd’);<br />
}<br />
21. Seeing the light<br />
The final step is to add the functionality for the ‘light’<br />
button. This switches the colours back to the regular light<br />
colours of the page. Save your work and refresh your<br />
browser to see the update take effect. Now you can<br />
switch between themes just by updating three CSS<br />
variables that, in turn, control many other elements on<br />
the page.<br />
function light() {<br />
setDocumentVariable(‘--main-bg’, ‘#fff’);<br />
setDocumentVariable(‘--alt-bg’,<br />
‘#f5f5f5’);<br />
setDocumentVariable(‘--main-type’,<br />
‘#333’);<br />
}<br />
tutorial _________________________________________________ 73