How to Optimize CSS with Code Style Guides

File Structure

 .class_1 .class_2 #id_1 #id_2 li a span { color: #bad; }

So it’s always good to set up a reasonable nesting limit, for example GitHub chose three levels in its style guide. By limiting nesting we can also force ourselves to write a better structured code.

Some style guides such as ThinkUp’s, also warns us about not using inline or embedded styles unless it’s unavoidable; it’s also a useful rule that’s worth applying.

Nesting

Just imagine a site that alternately uses px, em, and rem length measurements. It won’t only look bad in the code editor, but most likely some elements will be surprisingly small or large on that site.

Code Style Guides vs. Pattern Libraries

.class { // good margin: 0; // bad margin: 0px; // bad margin: 0em; // bad margin: 0rem; }

Commenting

We also need to make decisions about colour values (hexadecimal, rgb, or hsl), and whether we want to use shorthand properties and according to which rules. There’s an instruction that’s included in every CSS code style guide I bumped into, i.e. don’t specify units for 0 values (really, just don’t).

Putting proper code style guides into use provides us with a better organized, consistent code base, improved code readability, and more maintainable code. It’s not a coincidence that major tech companies, such as Google, AirBnB, or Dropbox make good use of them.

Mail

Nesting is a great feature in CSS, but at times it can go out of control. No one feels particularly happy, especially in the middle of a frustrating debugging process, bumping into extra long selectors like this:

ThinkUp's Style Guide

It’s important to note that the best style guides don’t only contain the styling rules themselves, but also examples of good and bad usage, as this way developers can more intuitively understand the rules.

Build Your CSS Code Style Guide

Idiomatic CSS for example establishes a meaningful commenting system that even uses some basic ASCII art, and results in beautifully organized code:

Both code style guides and pattern libraries include a styling strategy, but a different kind. Pattern libraries, such as Bootstrap, Zurb Foundation, BBC’s Global Experience Language, or MailChimp’s pattern library, provide us with a UI with premade CSS classes, typography, colour scheme, sometimes a grid system and other design patterns.

Dropbox for example requires developers to put spaces after the colon in property declarations, while Evernote uses two spaces for indentation. We can set up as many formatting rules as we are comfortable with, but never more than it’s possible to grasp.

AirBnB's Style Guide

Moreover if we want to change its colour from red to something else in the future, we can easily do it without a hassle. There are also premade CSS naming conventions, such as the BEM (Block, Element, Modifier) convention, that result in a consistent naming structure with unique and meaningful names.

The four common styles used in selector naming are .lowercase, .under_scores, .dash-es, and .lowerCamelCase. It’s okay to choose any of them but we need to follow the same logic across the whole project.

Declaration Order

When designers talk about style guides, they usually mean an agreed upon manual on the coherent look and feel of a website or an application, with a well-designed colour scheme, typography, and UI that’s used across the whole project.

In this post we will take a look at how we can smartly optimize our CSS with the help of CSS code style guides.

Naming Rules

Formatting Rules

Note that living style guide generators, such as KSS, Styledown, or Pattern Lab, generate pattern libraries and not coding style guides. While pattern libraries are also highly useful and elevate the web development process, they don’t allow us to optimize code itself.

Using only semantic selector names is also essential if we want to have meaningful code. For example, instead of .red-button (which doesn’t show what the button does) it’s better to use the .alert-button name (which says what it does), as this way, we enable developers (and our future selves) to understand what said button does.

Code formatting includes things like usage of whitespace, tabs, indentation, spacing, line breaks, etc. There’s not really a universally good or bad method in formatting, the only rule of thumb is to choose coherent rules that result in a readable code, and follow them through.

BEM Naming Convention

First, we need to figure out a logic according to which we will organize our CSS files. For smaller projects one CSS file may be enough, but for larger ones it’s always better to break up the code, and concatenate the separate files later in production.

Units and Values

In our industry there is a certain degree of uncertainty about what we can call a style guide. A List Apart for example uses it synonymously with the term pattern library in this article, but we can bump into this kind of definition in other posts as well.

Ordered things are always easier to see through, and ordering CSS declarations (properties with their values) according to a rule that makes sense results in a better organized code.

Take a look at for example WordPress’s property ordering rules, it defines the following simple but logical baseline for ordering in which properties are grouped by their meaning:

For example AirBnB shows good and bad examples to developers in the following easily digestable way:

  1. Display
  2. Positioning
  3. Box model
  4. Colors and Typography
  5. Other

Using coherent naming rules for CSS selectors is crucial if we want to understand our code months or even years later. There are many solutions out there, and there’s only one strict rule we need to follow i.e. a selector name cannot start with a number.

Deciding upon how we want to use units and values is not only important to achieve a consistent code look, but also if we don’t do so, we may end up with something weird

Commenting code is essential in all languages, but in CSS it doesn’t only facilitate debugging and documentation making, but also sections CSS rules into logical groups. We can use either the /* ... */ or the // ... notation style for comments in CSS, the important thing is to stay consistent with comments throughout our project.

The final goal of a CSS code style guide is to ensure we can work with a consistent, easily debuggable code base written by developers who all follow the same code styling rules. Creating a CSS code style guide may take a little time but it’s worth the effort, as we only have to do it once. Then we can use the same style guide across different projects.

CSS code style guides, such as Evernote’s or ThinkUp’s (or the ones mentioned in the intro) contain rules about how to write CSS including things like naming conventions, file structure, property order, code formatting, and others.

There’s another type of style guide we can use in web development as well, and it’s just as important but much more rarely discussed: style guides for the code itself. Code style guides are rather for developers than designers, and their main goal is to optimize CSS, or other code.

On the other hand, there are also publications, such as CSS Tricks or Brad Frost’s blog, that distinguish code style guides from pattern libraries. This latter approach probably takes us closer to a well-optimized website, as it allows us to handle code and design separately, so we will use this in this post.

Idiomatic CSS Commenting


How to Blur Selected Parts of Your YouTube Video

YouTube editor has recently offered a handy feature to blur any part of a video frame for a customized duration of time. Although, previously YouTube editor has provided an easy way to blur (only) faces automatically but the feature to blurring a specific part of your choice in a video is only recently possible.

(Step 3) Click on Apply button to blur faces automatically in video.

Enhancement Option

By default, the object you select to blur is tracked automatically by YouTube as the video proceeds. But if you don’t want the automated object tracking and prefer to blur a specific portion inside the video frame then click on the Pin inside the same timeline (see image above).

Blurring Effects

(Step 1) Once you are done uploading a video or if you have already uploaded a video on YouTube, click the Enhancements option right below your video.

Blurring Face

Let’s quickly check out how you can do this. The video used for this demonstration is the Top 10 Funny Baby Videos 2015.

Customized Blurring

(Step 2) Click on Blurring effects and you will see the two options.

Customized Blurring

(Step 4) Click on the Edit button to start selecting portions of the video to blur.

(Step 5) Click on the video and a resizable frosted box will appear. Drag this box inside the video frame and adjust its size as per requirements. Finally, you can define the length of time (ie how long) the blur effects should appear in your video. Do this by adjusting the width of the tab in the timeline below your video.

Once you have added the required blurring effects in your video, click on the Done button to apply the process.


50 Spectacular Sights of Nature You Need to See Before You Die – Part II

Seljalandsfoss Waterfall, Iceland. (Image Source: Giuseppe Milo)

  • 30 beautiful temples in Asia you must visit
  • 45 awe-inspiring landmarks around the world
  • 40 spectacular sights of nature you need to see before you die

Red Beach, China. (Image Source: Kashif Pathan)

Antelope Canyon, USA. (Image Source: Moyan Brenn)

Giant’s Causeway, Northern Ireland. (Image Source: philnewberryphotography)

Pamukkale, Turkey. (Image Source: Esther Lee)

Volcanic Eruption, Italy. (Image Source: gnuckx)

Whitehaven Beach, Australia. (Image Source: Ma Première Fois En Australie)

Victoria Falls, Southern Africa. (Image Source: Harvey Barrison)

Socotra, Yemen. (Image Source: Valerian Guillot)

Pangong Tso Lake, India-China. (Image Source: )

Perito Moreno Glacier, Argentina. (Image Source: Dimitry B.)

Whitehaven Beach, Australia. (Image Source: PROPetra Bensted)

Street in Bonn, Germany. (Image Source: Matthias Buehler)

No matter what time of the year it is, you’re surely to have a vacation already planned out in your mind. The only question that changes year in, year out is where to go. Here are a few places that you might want to get started on:

To add to that growing list, we have another list of spectacular places you might want to add to your bucket list. The thing with natural attractions like these is that you can’t replicate them in other places due to the different environment and climate. That’s what make these sights so alluring.

Fields of Tea, China. (Image Source: SteFou!)

Geiranger, Norway. (Image Source: Ub66)

Mount Roraima, USA. (Image Source: Paulo Fassina)

Nyingchi, China. (Image Source: Amy Zhang)

Ngorogoro Crater, Tanzania. (Image Source: Kate Nevens)

William Bay, Western Australia. (Image Source: fvanrenterghem)

Spotted Lake, Canada. (Image Source: MIKANSU 2008)

Plitvice Lakes National Park, Croatia. (Image Source: Clark & Kim Kays)

Angel Falls, Venezuela. (Image Source: ENT108)

Siwa Oasis, Egypt. (Image Source: Bobby McKay)

Blue Grotto Sea Cave, Italy. (Image Source: Glen Scarborough)

Pehoe Lake, Chile. (Image Source: Frank Kehren)

Tulip Fields, Netherlands. (Image Source: MissTessmache)

Mendenhall Ice Caves, Alaska. (Image Source: Andrew E. Russell)

Salar de Uyuni, Bolivia. (Image Source: Haceme un 14)

Lake Retba, Senegal. (Image Source: Kashif Pathan)

Verdon Gorge, France. (Image Source: Miguel Virkkunen Carvalho)

Pulpit Rock, Norway. (Image source: Szczepan Janus)

Great Blue Hole, Belize. (Image Source: Eric Pheterson)

Trolltunga, Norway. (Image Source: Morten Rustad)

If you want more information about the area, just follow the link. We’ve added the location info for each of these destinations. You’ll find yourself on Google Maps where there will be more info and photos of these attractions to enjoy.

Chaldon Herring, United Kingdom. (Image Source: Rob Bye)

Geirangerfjord, Norway. (Image Source: Tristan Taussac)

Canola Flower Fields, Canada. (Image Source: Wayne Stadler)

Skaftafell National Park, Iceland. (Image Source: Jakob Hürner)

Fly Geyser, United States. (Image Source: photosbyflick)

Cliffs of Moher, Ireland. (Image Source: Danny Huizinga)

Lake Titicaca, Bolivia/Peru. (Image Source: Emmanuel DYAN)

Spreewald, Germany. (Image Source: gravitat-OFF)

Lake Louise, Canada. (Image Source: Drew Hays)

Bamboo Forest, Japan. (Image Source: Alex Chen)

Lavender Fields, UK. (Image Source: David Bell)

Zhangjiajie Sandstone Pillars, China. (Image Source: Eric)

Canyonlands National Park, Utah, USA. (Image Source: Kurt Repanshek)

Hitachi Seaside Park, Japan. (Image Source: Zhao !)

Lauterbrunnen Valley, Switzerland. (Image Source: Kosala Bandara)

Moraine Lake, Canada. (Image Source: James Wheeler)

Wisteria Flower Tunnel, Japan. (Image Source: Mindphoto)


How to Write Better CSS with Performance in Mind

Reflows are triggered by layout changes to an element, like changes to the geometric properties such as height or font size, the addition or removal of classes to elements, window resizing, activated :hover, DOM changes by JavaScript, etc.

Browser Rendering Workflow
  1. Recalculate Style (and render tree creation). Browser computes the styles to be applied to the elements in the DOM tree. A render tree is later created while discarding the nodes (elements) from the DOM tree that aren’t to be rendered (elements with display:none) and those that are (pseudo-elements).
  2. Layout (aka Reflow). Using the computed style from before, the browser calculates the position and geometry of each element on the page.
  3. Repaint. Once the layout is mapped, pixels are drawn to the screen.
  4. Composite Layers. During repainting, the painting might be done in different layers autonomously; those layers are then finally combined together.

If you’re animating properties of an element that can trigger Repaint directly or indirectly, it’ll be of great advantage if that element is in its own layer preventing its painting prcoess from affecting the rest of the page and triggering hardware acceleration. In hardware accelaration, the GPU will take up the task of performing the animation changes in the layer, saving the CPU extra work while speeding up the process.

1. Reduce Style Calculations

(1) So up til now, we haven’t touched on CSS file size reduction. We have mentioned that reduction in style rules (and DOM elements) make a significant performance improvement because of how much the browser will have to work less on the process of computing the styles. As a consequence of this code reduction, writing better selectors and the deletion of unused CSS, the file size will automatically decrease.

Style Calc

IMAGE: Aerotwist

If you’re animating an element that goes through layout changes, take it out of the page flow by absoutely positioning it, since Reflow in absolutely positioned elements won’t affect the rest of the elements on the page.

To avoid costly style calculations, reduce complex and deeply nested selectors so that it’s easier for the browser to figure out which element a selector is referring to. This reduces computational time.

2. Reduce Reflows

(2) It’s also advisable to not make too many consequential changes to an element’s styles in JavaScript. Instead add a class to the element (using JavaScript) that holds the new styles to make these changes – this prevents unnecessary Reflows.

To forcefully promote an element to new layer and go into hardware acceleration for animation, there are two techniques invovled:

Just like in style calculation, to reduce Reflows, avoid complex selectors and deep DOM trees (again, this is to prevent excessive cascading of Reflows).

Reflows or Layout changes in an element are very "expensive" processes, and they can be of an even bigger problem when the element that went through the layout changes has a significant amount of children (since Reflows cascade down the hierarchy).

(3) You will want to avoid Layout Thrashing as well (forced synchronous Reflows) which arises due to the accessing and modifying of the Layout properties of elements using JavaScript. Read more about how this kills performance here.

To summarise:

  • Target elements that are lower in the DOM tree when making layout changes
  • Choose absolutely positioned elements for layout changing animations
  • Avoid animating layout properties whenever possible

3. Reduce Repaints

Now let’s continue on to what we can do in the first three stages of the operation to write better-performing CSS codes.

Pixel Art

Like mentioned before, in the "Recalculate Style" stage the browser computes the styles to be applied to the elements. To do this, the browser first finds out all the selectors in the CSS that point to a given element node in the DOM tree. Then it goes through all the style rules in those selectors and decides which ones are to be actually applied to the element.

In today’s post we will ponder over the code choices we can make in CSS for improved site performance. But, before we dive into those choices, let’s first take a brief, closer look at the webpage rendering workflow in order to focus on the problematic (performance-wise) areas that are solvable via CSS.

To avoid frequent and huge repaints, use less of the properties that cause costly repaints like shadows.

If you have to change the layout styles of a component in your page, target the styles of the element that is at the lowest in the hierarchy of elements that the component is made of. This is so that the layout changes doesn’t trigger (almost) any other Reflows.

Here’s the rough flow of operations performed by the browser after DOM tree creation:

  1. add transform: translate3d(0, 0, 0); to the element, tricking the browser into triggering the hardware acceleration for animations and transitions.
  2. add the will-change property to the element, which informs the browser of the properties that are likely to change in the element in the future. Note: Sara Soueidan has an in-depth and super-helpful article on this in the Dev.Opera site.

Repaint refers to the drawing of pixels on the screen, and is an expensive process just like Reflow. Repaints can be triggered by Reflows, page scroll, changes in properties like color, visibility and opacity.

  • Avoid expensive styles that cause Repaints
  • Seek layer promotion and hardware acceleration for hefty animations and transitions.

Take Note

To summarise:

In some browsers, opacity (with a value of less than 1) and transform (value other than none) are automatically promoted to new layers, and hardware acceleration is applied for animations and transitions. Preferring these properties for animations is thusly good.

Other ways to employ include reducing the number of style rules (where possible), removing unused CSS and avoiding redundancy & overrides, so that the browser doesn’t have to go through the same style again and again during style calculations.


Top 5 Android Emulators For Your Desktop

Genymotion is known for its quick boot and high performance for running HD games. Offering latest Android releases quickly is one of the best feature of Genymotion apart from delivering fast performance and offering over 3K hardware configurations to run or test apps in multiple hardware scenarios.

[Download: Windows, Mac OS X, Linux]

You can multi-task between PC/iMac and Android apps using an emulator on your desktop. You can simply run and switch between a few desktop apps while running mobile apps on your emulator. Some of the emulators even support a drag-and-drop feature that eases the task of moving text or data between your PC/Mac/Linux and the emulator.

The emulator integrates well with PCs and iMacs to give the ultimate gaming experience with native graphics support and easy multitasking between different apps or games like a native Android. Compared to other such emulators, it offers more compatibility support for apps and games, and avails a bigger Android interface than any phone or tablet.

Advantages of Android Emulators

Android Emulators let you enjoy all of Android without leaving a hole in your pocket. Nonetheless not all emulators are created equal; they vary in terms of features and specifications.

[Download: Windows, Mac OS X]

1. Run stock Android on a PC, Mac Or Linux

You can also play Android games on your PC or iMac. With Android emulators, you no longer need high-priced smartphones to enjoy your favorite HD games because you can easily enjoy most of those games on your desktop without spending single penny from your pocket. These emulators offer good support for most of apps and games, and a few even offer joystick or phone support for gaming control.

2. Select any Android version, past or present

You can select any Android version (Lollipop, Marshmallow or classic Gingerbread). Since the emulator is just a software running on your system, you’re open to select and use any Android release.

3. You can select any hardware specification

An Android emulator lets you run Android on a Windows, Mac or Linux. This allows you to gain access to Android features and apps without needing to buy an Android device, which is great news for people who don’t really want to buy a new Android device just for testing the app or any other product they are developing.

You can choose any hardware specification – like that of Google Nexus 5 or 6 (smartphones), Samsung Galaxy S5 or S6 (smartphones), Google Nexus 7 or 9 (tablets), etc. The only limitation is the power level of your desktop machine, for example, if it has 4 GB RAM then you can’t allot 3 GB for the emulator.

The emulator replicates a full-featured Android and is basically the same as Android Emulator with exception to performance – Xamarin Android Player runs faster than the default emulator by Google. It offers beautifully designed interface like Genymotion’s and good support for most of Play Store’s apps and games.

4. Bricking isn’t a total loss

BlueStacks, one of the first few third-party Android emulators, supports camera and microphone integrations for making voice and video calls via VoIP apps like WhatsApp, Skype and others. Its features include drag-and-drop support between desktop and emulator for file sharing, sync between desktop emulator and an actual Android device, localizations, Android-on-TV capability and many more.

All you need to do (in most situations) is to delete all the device’s files and create a new emulator device from scratch. You also don’t have to worry about hardware damage like those brought about from dropping the device or getting it wet.

5. Use Android’s social apps while working on Desktops

In this post, we will be briefly touching on the usages and advantages of Android Emulators then detailing the top 5 Android Emulators you can try out.

6. Play high-end games

Which emulator are you going to download? Did we miss your favourite Android Emulator? Kindly let us know via the comments section below.

7. Multi-task between PC/iMac and Android apps

Let’s look at the advantages first to sort of give you an idea of what you can expect from an emulator.

5 Best Android Emulators

The standard requirements for running Android Emulators are dual-core Intel or an AMD processor with virtualization support, 4 GB of RAM and 10 GB of disk space, though less powered ones can run older Android versions (there’s something for everyone).

BlueStacks (Free)

Like any other software, an Android emulator can be easily downloaded and installed, then configured to boot up an emulator instance (which is something like a Chrome or Firefox browser tab).

Bluestacks

Genymotion is a premium offering for primarily Android developers, but it is also suitable for users and gamers with the intention to run Android apps or games on PCs or iMacs. The emulator supports webcam and microphone, like BlueStacks and Andy, for making audio or video calls using WhatsApp, Skype and other VoIP apps. Its paid version offers a reset button for if or when you brick the emulator.

Want More on Emulators? Then Read:

Andy (Free)

[Download: Windows, OS X]

Andy

Coming directly from the Android team, this emulator offers new Android releases faster than any other emulator in the market and even avails pre-release versions for enthusiasts eager to have hands-on experience with the newest trial features. Though this emulator is not as user-friendly as others, it works well for running mobile apps on desktops, and it’s also available for Linux.

If you are determined to get yourself an Android emulator, here are the best 5 picks in the field.

Genymotion (Free | $136/year)

Xamarin Android Player includes a native user interface for desktops and lets you run and test apps efficiently. With a primary focus for developers, Xamarin is actually easily usable by laypersons who want to try out Android’s new features, run apps or play high-end games. It avails features such as network and hardware simulation, native-like Android experience and lots more.

Genymotion

Perhaps the most attractive reason to get Android on your desktop is that you can use Android’s social apps (eg WhatsApp, Telegram, Facebook’s Messenger) like you would on your Android device. You can chat, make voice or video calls with Skype, edit your next blogpost, work on a spreadsheet and coding all at the same time.

[Download: Windows, OS X]

Xamarin Android Player (Free)

Andy gives you easy sync between your desktop (running Andy) and an Android device. It also allows you to easily access the desktop’s filesystem (yes, all office party photos and videos) and allows you to forget storage limitation notifications. Its other features include support for camera and microphone for audio and video calls and desktop push notifications about new messages or events.

Xamarin

[Download: Windows, Mac OS X, Linux]

Because it’s a software running as a virtual hardware on your system, any damage the emulator experiences, e.g. bricking, will not be of total loss (to you), except for maybe the data inside the emulator’s memory.

Android Emulator (Free)

Android Emulator is the de facto emulator offering from Google. It mimics Android with a set of software and hardware configuration. The emulator lets you run and test apps by using AVDs (techno-fancy name for emulator device). With this emulator, you can enjoy multimedia, use multiple services, customize the emulator dashboard, access the network, run apps and play games on your PC, Mac or Linux.

Emulator

You’re also never restricted to use the factory version provided by the device’s manufacturer (since there is no physical device) having to wait for an OTA update for the latest Android version to be made available by the manufacturer. Just pick the version you want once it is made available.

You can experience Google’s stock version of Android using Android Emulators – whether it’s hacking the developer options, switching quick toggles, playing its Flappy Bird game or testing self-developed apps. It’s a good platform to check out the original Android OS in cases where your device manufacturer is one of those that provide a custom Android instead.

You can even choose a customized hardware specification using preferred values for processor, RAM, storage and more. You can create separate emulators for different scenarios, for example, high-end games may require more RAM and data keeping will require more storage.

Andy supports multi-touch on touch-supported devices running Windows 8 and above. It also supports high-end graphics and Xbox/PS controllers, allowing you to play popular games your way yet without any (real) Android device. To take fun to the next level, Andy even lets you use your phone as a remote control for playing games on the big screen (of your computer) without sacrificing the multi-touch or gyro elements (of your phone).

  • Wine + 6 Other Apps To Run Windows Programs On Other Platforms
  • 10 Emulators To Turn Your Computer Into A Retro Console