APPENDIX A

image

CSS Libraries and Resources

image Note   A few links you may want to bookmark.

We have covered CSS in general and focused on how it applies to creating Windows 8 apps, but I can guarantee you that there is a lot more to cover. To master CSS and master Windows 8 app creation you’ll need to spend hundreds of hours actually writing apps, and you’ll need an arsenal of information. I can’t help you with the hundreds of hours, but luckily, I can help provide the arsenal of information.

In this Appendix, I’ll attempt to distill for you the vast amount of information that is available down to just the essence of CSS. I’ll introduce some CSS libraries, helpers, and some learning and reference websites that have been a big help to me.

Book Code

The listings in this book amount to quite a bit of code and you may want the option of copying some of it to paste into your own project. You can access to all of this book’s source code by visiting www.apress.com/9781430249832 or my website at http://codefoster.com/cssbookcode, where you’ll be directed to the source code on CodePlex. The code compiles to a Windows 8 app that indexes all of this book’s code listings by chapter and lets you see them in action.

codeSHOW

codeSHOW is a Windows 8 app written in HTML, CSS, and JavaScript with the sole charter of helping you learn to make your own app in HTML, CSS, and JavaScript. You can access all of the codeSHOW project’s source code by visiting codeshow.codeplex.com or if you have Windows 8 installed you can browse instead to aka.ms/codeshowapp and download the app.

The codeSHOW app shows off a number of demos and allows you to see the code to have a peek at the HTML, CSS, and JavaScript that it took to create it. It’s an excellent way to learn how to implement a feature you know you need. It’s not, however, a big lesson in app architecture. For that, you’re welcome to download the full source code of the codeSHOW project itself from the CodePlex link and take a look at how codeSHOW is created.

The World Wide Web Consortium

image Note   w3.org/style/css

The World Wide Web Consortium—a.k.a. the W3C—is the international standards body that handles the technologies underlying the Internet (and there are a lot of them). The W3C started at the Massachusetts Institute of Technology (MIT) in the mid-90s. It was founded by a guy named Tim Berners-Lee, and he is the acting head even today.

As you might imagine, the W3C has its work cut out for it in facilitating discussions on so many proposed standards for the web and then deciding which standards will be recommended by the organization. Every proposed standard works its way through four stages: working draft, candidate recommendation, proposed recommendation, and finally W3C recommendation (REC).

If you’re working in the web stack, you should frequent the w3.org website. All other discussions (including this book) and even the implementation of web standards are conjecture compared to w3.org, which is the standard by which it should all be measured.

You’d expect a pretty decent website from a web standards organization, and w3.org doesn’t disappoint. To dig in to all of the W3 standards around CSS visit w3.org/Style/CSS/ and take some time to look around.

Microsoft Developer Network (MSDN)

The MSDN network is a vast library and tremendous source of documentation for every flavor of Microsoft development, including CSS. You’ll certainly find corners of Microsoft’s implementation of the CSS standard that I didn’t find space to cover in this book.

When you’ve already referenced the W3C standard on some aspect of CSS and you’re wondering about the nuances of behavior in Internet Explorer or Windows 8 apps, I recommend you look to MSDN.

CSS Tricks

image Note  css-tricks.com

I’ve benefited hugely from CSS Tricks by Chris Coyier. It’s a beautiful site and has a wealth of information on the topic of CSS. The articles are first class and the sheer volume of information is staggering. I usually either search the site or scroll to the bottom and hit “More Blog Posts” to browse articles.

CSS Tricks not only has high-quality articles, but the community forum is good too. You can get some real help with CSS issues and hopefully you’ll find some questions from others that you can answer. I tend to visit CSS Tricks when I’m looking for a little bit of design inspiration as well. The gallery section is particularly effective at this.

Web Platform Docs

image Note   webplatform.org

The Web Platform Docs site is an entirely community-driven web platform documentation website. It offers a lot of help on all of the significant web technologies such as HTML, CSS, JavaScript, SVG, and more.

The site is in alpha and the design is struggling a bit in my opinion, but it’s a good, open platform for learning the web platform, helping to teach others, and cultivating community.

LESS

image Note   lesscss.org

This isn’t just another link to a site with good CSS documentation. Oh no. LESS is more! LESS is a very simple but extremely helpful assistant to CSS. LESS allows us to describe our styles in a bit more intelligent and programmatic way.

We write LESS code and reference it on our HTML page just like we would with CSS, but before the page is requested, a processor transforms the LESS into CSS, and that’s all our HTML file ever really sees. LESS creates CSS that you could have created yourself, but didn’t want to.

Not all LESS code is valid CSS code until it gets processed. On the contrary, all CSS code is valid LESS code. This means that you can take your existing CSS files (whatever their size) and convert them to a LESS file simply by changing their file extension. You can then begin the process of introducing LESS features one at a time.

LESS also allows the definition of variables, mixins, nested rules, and functions and operations. Let’s spend a bit of time of each of those and then how to set up LESS in Windows 8.

Variables

To introduce variables, let’s use an example: your designer (even if you are your designer) informs you that your primary theme color is going to be R71 G82 B49, you can imagine that you’re going to be sprinkling rgb(71,82,49) all over your CSS file. That specific color might end up being the border of the items on the main page, the color of the text in the header, and the background of the app bar.

Sprinkling a value like this all over a file should smell bad to you. It means that if your designer changes her mind, you’re going to be changing colors in more than one place, and you’re opening yourself up to a mistake.

If you’re using LESS, however, you have the opportunity to define a variable to represent this primary color. You might even call the variable primaryColor. An example of the definition of that color and subsequent reference to it from a couple of places can be seen in Listing A-1.

Listing A-1.  A LESS variable used to store a color

@primaryColor: rgb(71,82,49);
 
#myListView .win-item {
    border: 1px solid @primaryColor;
    color: @primaryColor;
}
 
.win-appbar { background-color: @primaryColor; }

Listing A-1 shows how the actual color—rgb(71,82,49)—is defined only one time. If at some point in the future our designer informs us that this hue should be adjusted a bit, it only has to be done in one place.

Mixins

Mixins allow us to mix in one class with another. That is, we can have one class inherit all of the style properties of another class. In Listing A-2 you’ll see a reference class (.reference) that sets a border and a background color. Two subsequent classes (.styleA and .styleB) should both take the same border and background color properties, but also need to add some of their own.

Listing A-2.  An example of a LESS mixin

.reference {
    border: 1px solid black;
    background-color: gray;
}
 
.styleA {
    .reference;
    font-size: large;
}
 
.styleB {
    .reference;
    font-weight: bold;
}

Mixins can save a good deal of typing in some cases.

Besides simply referencing another class, you’ll have the opportunity to pass a parameter to the referenced style with a mixin. See what I mean in Listing A-3.

Listing A-3.  A mixin with a passed parameter

.reference (@borderWidth: 1px) {
    border: @borderWidth solid black;
    background-color: gray;
}
 
.styleA {
    .reference;
    font-size: large;
}
 
.styleB {
    .reference(2px);
    font-weight: bold;
}

Can you see what’s going on there? The .reference style now takes in a parameter called borderWidth with a default value of 1px. The .styleA property includes .reference but doesn’t specify a value for the borderWidth parameter, thus it gets all of the properties of the .reference rule with a default value for the border width. The .styleB property, however, includes .reference but does specify a value of 2px for the borderWidth parameter and thus it gets the properties of the .reference rule with a 2-pixel border.

Mixins allow for some very elegantly defined CSS that is expressive and concise and centralized so it’s easy to maintain.

Nested Rules

You’ve likely noticed that some CSS style selectors can get really lengthy. To access the list items in a list of class error in the div element called myDiv in the main section, you would end up with something like section[role=main] #myDiv .error li { ... }, and they can get much longer than that even.

To avoid this, you can use LESS’s nested rules. Nested rules allow you to define rules inside of one another and the inference is that each rule’s parent selector should apply to it. The two CSS sections in Listing A-4 have an identical effect.

Listing A-4.  Comparison of class CSS to LESS using nested rules

/* using classic CSS */
section[role=main] {
    font-size: large;
}
 
section[role=main] #myDiv {
    font-weight: bold;
}
 
section[role=main] #myDiv .error li {
    color: red;
}
 
/* using LESS nested rules */
section[role=main] {
    font-size: large;
    #myDiv {
        font-weight: bold;
        .error {
            li {
                color: red;
            }
        }
    }
}

Notice the redundancy present in the classic CSS section of Listing A-4. That repetition turns in to difficult maintenance and error-prone code. The LESS version is not a lot shorter in this case, but the point is that it is more logically defined and makes for better readability and better maintainability.

Remember that even when LESS is employed in your app, the client ends up using the resulting CSS anyway. This isn’t something that a browser engine has to decipher. It is still fed the CSS style rules in the format expected.

Functions and Operations

The final feature of LESS that we should look at is the functions and operations that it provides.

LESS gives us some intelligent ways to calculate values, and there are many times when calculated values are helpful. If a base length is 10 pixels, but some elements should be 2 pixels more or less than we should be able to calculate that difference, shouldn’t we?

Functions and operations are very often used in conjunction with variables. Let’s look at a few examples. First we’ll look in Listing A-5 at some calculations of length.

Listing A-5.  LESS calculations to determine border width

@borderWidth: 10px;
 
.styleA {
    border-width: (@borderWidth - 2);
    border-style: solid;
    border-color: black;
}
 
.styleB {
    border-width: (@borderWidth + 2);
    border-style: solid;
    border-color: black;
}

The .styleA rule in Listing A-5 would result in an 8-pixel border, and the .styleB rule would result in a 12-pixel border. The advantage over specifying those values explicitly comes when you decide to change the base border width. At that point all calculated borders are automatically adjusted.

These calculations can be done on colors too and there are some functions that give us some even fancier functionality. Adding colors may be counter-intuitive to you, but it’s really just adding the individual red, green, and blue components of the colors. A hex color of #123456 added with another color of #111111 would result in a color of #234567. Colors can be multiplied, divided, or subtracted too.

Besides the basic mathematical operations, some color functions are available. If I have a base color of #77C121, and want a color that is 10% darker, I can avoid a design time round trip to my favorite graphics package, and instead just use LESS’s darken() function. I’ll show an example of this in Listing A-6.

Listing A-6.  The darken() function used to calculate a darker color

@baseColor: #77C121;
 
.normal {
    background-color: @baseColor;
}
 
.disabled {
    background-color: darken(@baseColor, 10%);
}

In Listing A-6, a disabled item automatically gets a shade that’s 10% darker than the base color. This is probably my favorite trick in LESS. Being able to create basic color pallets and then express derivative colors from it is a very elegant way to style your app. A simple change to the base color amounts to the automatic creation of an entirely new derived color scheme.

Setup

Setting up your Windows 8 app to use LESS is not difficult.

First, let’s recall typical web architecture and where LESS fits into it. CSS normally lives in a web project and is served to the client from a static server file. For a web project using LESS, that CSS code is essentially dynamic, because a LESS processor has to generate it from the LESS code. This process can either happen on the server or on the client.

In Windows 8 apps, our architecture is a little bit different than a typical web application. A Windows 8 device is both the server and the client, so the division between server and client is effectively eliminated. For apps, therefore, I recommend following these steps to use the LESS JavaScript library for your processing:

  1. Change the extension of your CSS files from .css to .less
  2. Change your style sheet references to look like: <link rel="stylesheet/less" type="text/css" href="myLessStylesheet.less">. Pay attention to the rel (relationship) attribute value - stylesheet/less
  3. Download the less.js library from lessjss.org and drop it into the js folder of your Windows 8 app. Remember, if you drop it directly into the folder structure of your app using Windows Explorer then you need to show the hidden files in Visual Studio and then right click on the less.js file and choose “Include in project”.
  4. Add a reference to the less.js library that you downloaded. I recommend you put this on the default.html page. It should look something like: <script src="/js/less.js" type="text/javascript"></script>

I think you’re cruising with LESS now, and I think you’ll like it.

Other Online Galleries

There are a number of galleries you’ll find online that you can use as a learning tool, as a source of inspiration, and as a source of project templates. Sites like CSS Flavor, CSS Collection, CSS Elite, and CSS Templates contain good collections of sample sites created using CSS.

Spend some time browsing the galleries that these sites offer, and when you find a template you like or one that has a style or layout feature you’re looking for just copy and study its CSS style sheets. Be sure to read the usage statements of the various sites to be sure you remain in compliance.

Reference sites like the ones I’ve outlined in this appendix are great when you need to target a piece of information to implement a new feature or overcome a challenging problem, but it’s also very rewarding to simply spend time browsing the content. Try it and you’ll find yourself learning things you never even knew you needed. Browsing online CSS galleries is another excellent way to learn how some of the theory you’ve acquired can be applied. So don’t just find good code and use it. Write good code and share it! Being a part of the developer community at large is yet another way to accelerate your learning and have a good time while you’re at it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.36.231