23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

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

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

<br />

Content goes here<br />

<br />

<br />

This is really all you ever see with the ViewBox as it has no other options or properties, no methods,<br />

and no events—very simple! Note also that because the ViewBox is just a control, you can use it for any<br />

fixed aspect-ratio content in an otherwise adaptive layout; it’s not only for the layout of an entire page.<br />

To set the reference size of the ViewBox—the dimensions against which you’ll write the rest of your<br />

code—simply set the width and height styles of the child element in <strong>CSS</strong>. For example, to set a base size<br />

of 1024x768, we’d set those properties in the rule for the fixedlayout class:<br />

.fixedlayout {<br />

width: 1024px;<br />

height: 768px;<br />

}<br />

Once instantiated, the ViewBox simply listens for window.onresize events, and it then applies a <strong>CSS</strong><br />

2D scaling transform to its child element based on the difference between the reference size and the<br />

actual size. This preserves the aspect ratio. This works to scale the contents up as well as down.<br />

Automatic letterboxing or sidepillars are also applied around the child element, and you can set the<br />

appearance of those areas (really any area not obscured by the child element) by using the win-viewbox<br />

class. As always, scope that selector to your specific control if you’re using more than one ViewBox in<br />

your app, unless you want styles to be applied everywhere.<br />

The basic structure above is what you get with a new app created from the Fixed Layout App project<br />

template in Visual Studio and Blend. As shown here, it creates a layout with a 1024x768 reference size,<br />

but you can use whatever dimensions you like.<br />

The <strong>CSS</strong> for this project template reveals that the whole page itself is actually styled as a <strong>CSS</strong> flexbox<br />

to make sure the ViewBox is centered, and that the fixedlayout element is given a default grid:<br />

html, body {<br />

height: 100%;<br />

margin: 0;<br />

padding: 0;<br />

}<br />

body {<br />

-ms-flex-align: center;<br />

-ms-flex-direction: column;<br />

-ms-flex-pack: center;<br />

display: -ms-flexbox;<br />

}<br />

.fixedlayout {<br />

-ms-grid-columns: 1fr;<br />

-ms-grid-rows: 1fr;<br />

display: -ms-grid;<br />

255

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

Saved successfully!

Ooh no, something went wrong!