Saturday, January 16, 2016

5 Angularjs seeds to start your angular app

Ref: http://www.sitepoint.com/5-angular-js-seeds-bootstrap-apps-2/
http://angularjs4u.com/linemanjs/top-10-angularjs-template-projects-seeds-demos/


In this post you will find 5 Angular JS Seeds and Bootstraps Apps you may want to add to your list for future use. Enjoy =)

1. Official Angular Seed

This project is an application skeleton for a typical AngularJS web app. You can use it to quickly bootstrap your angular webapp projects and dev environment for these projects.

2. Cleverstack Angular Seed

CleverTech AngularJSSeed Application – Use this as a starting point for your NodeJS application!

3. Angular Enterprise Seed

This project is meant to serve as a more feature-rich starting point for enterprise and commercial web applications. It integrates AngularJS with projects like Angular-UI, Angular-Strap, Bootstrap, and jQuery, along with examples of angular services, directives, controllers, and filters.

4. YeoMan

Use Yeo to startup your new angular project.

5. AngularJS Skeleton App

This simple web application will allow its users to view, search and filter TV Show Premieres for the next 30 days.

Saturday, January 9, 2016

Steps to deploy tomcat project in amazon aws ec2

Ref: http://stackoverflow.com/questions/15996741/how-to-deploy-a-eclipse-java-web-dynamic-project-on-amazon-ec2

Follow the steps below:
  1. Setup Apache Tomcat on your Amazon EC2 instance.
    • Usually all you have to do is download the current version, unzip it, and start it by running apache-tomcat-folder\bin\startup.bat. (You can also donwload an installer and set it up as windows service. Check this link for more details).
    • Make sure you test it before continuing (open its address on a browser, something like http://yourinstaceaddress.com:8080/).
  2. Export your web application .war file
    • In Eclipse, right click on a Web project and select Export. Then select WAR file in the Export window and then select Next. Choose the project, the .war file name and folder to export. More detailed explanation can be found here and here (with pictures).
  3. Deploy the .war file to your Tomcat Server
    • The, by far, simplest way to do this is to place your .war (say myapp.war) file in your apache-tomcat-folder\webapps\ folder.
    • There are other ways, like via Tomcat Manager. But they can be tricky and, as a new user, you should avoid them. (Don't worry: the simple method is ok for production deployment).
  4. Test your web app
That's it. If you ever edit the app, repeat steps 2-4 (but delete the webapps\myapp\ folder created before executing step 3).


Set Up an Amazon EC2 Instance with Tomcat and MySQL – 5 Minute Tutorial

REF: http://coenraets.org/blog/2011/11/set-up-an-amazon-ec2-instance-with-tomcat-and-mysql-5-minutes-tutorial/

Create an AWS Account

First things first: you need to create your AWS account. You can sign up here. You’ll have to provide a credit card and a phone number where you will be called as part of the online registration process for verification purposes. Amazon offers a Free Usage Tier, which is great to explore the services and even host real apps without being charged. Check the details here.

Create an Instance

Now that you have an AWS account, access the AWS Management Console and click the EC2 tab to create a new instance:
  • Choose an AMI in the classic instance wizard: I chose the Basic 64-bit Amazon Linux AMI.
  • Instance details: keep the default settings.
  • Create a new key pair. Enter a name for your key pair (i.e. christophe) and download your key pair (i.e. christophe.pem).
  • Select the quick start security group.
  • Launch your instance.

SSH

Once your instance is running, you can ssh into it. First, you need to identify the address of your instance: Select the instance in the AWS Management Console, and look for the Public DNS in the instance description (bottom part of the screen).
Use that address (and a path to your .pem file) to ssh into your instance:
ssh ec2-user@ec2-50-17-14-16.compute-1.amazonaws.com -i ~/christophe.pem
Important Notes:
  1. You may have to chmod your .pem file as follows:
    chmod 600 ~/christophe.pem
  2. Depending on the image you chose, you may also have to replace ec2-user with root orubuntu. ec2-user is what you need for an Amazon Linux AMI.

SFTP

At this point, you can also SFTP into your new instance using parameters similar to these:
host: ec2-50-17-14-16.compute-1.amazonaws.com
port: 22
user: ec2-user
password: Select your .pem file

Elastic IP

The public DNS address changes when you restart your instance. To get a permanent IP address, click Elastic IPs in the AWS Management Console (left navigation bar), allocate a new IP address and associate it with your instance.

Install Tomcat

To install tomcat, ssh into your instance and type the following command:
sudo yum install tomcat6 tomcat6-webapps
“tomcat6-webapps” is optional and will install the Tomcat sample apps. The configuration files are in /usr/share/tomcat6. To install your own web app, you can simply SFTP it to /usr/share/tomcat6/webapps.
To start Tomcat:
sudo service tomcat6 start
To stop Tomcat:
sudo service tomcat6 stop
The default Tomcat server uses port 8080. You need to open that port on your instance to make sure your Tomcat server is available on the Web (you could also change the default port). In the AWS Management Console, select Security Groups (left navigation bar), select the quick-start group, the Inbound tab and add port 8080. Make sure you click “Add Rule” and then “Apply Rule Changes”.
You should now be able to access your Tomcat server from a browser using (use your own Public DNS address or Elastic IP):
http://ec2-50-17-14-16.compute-1.amazonaws.com:8080/

MySQL

To install MySQL, ssh into your instance and type the following command:
sudo yum install mysql-server
To start MySQL:
sudo service mysqld start
To stop MySQL:
sudo service mysqld stop

MySQL Workbench

You can administer your database remotely using MySQL Workbench. You don’t have to open an additional port (i.e. 3306). Simply choose Standard TCP/IP over SSH in the Setup New Connection dialog as follows:

Tuesday, January 5, 2016

Angularjs best practices for writing custom directives


Ref: https://dzone.com/articles/angularjs-10-best-practices
http://stackoverflow.com/questions/20018507/angularjs-what-is-the-need-of-the-directives-link-function-when-we-already-had
This article represents top 10 best practices that one may want to apply while creating custom directives. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following is listed the best practices for creating custom directives:
  1. Naming Convention: Prefer using two or three letter prefix (except ng) while naming directives to avoid collision with future HTML releases. Using “ng” as prefix might collide with AngularJS OOTB directives in future.
  2. Directive Definition Object (DDO): Prefer returning DDO rather than a function.
  3. TemplateUrl Usage: Prefer storing HTML template code in a seperate file and assign the path to templateUrl variable.
  4. Attribute vs Element: Whether to create custom directive as an attribute or an element is tricky. If the need is to create a component with core behavior and possibility to decorate the component with additional behavior in future, you would want to make the directive as element (E). However, if the need is to decorate existing element with some custom behavior, you could create dirrective of attribute (A) type.
  5. Isolate Scope: If the need is to reuse the component (directive) throughout your app, consider creating isolate scopes using scope option. The concept of isolate scope is used to separate the scope inside a directive from the scope outside.
  6. Clean-up function: Consider defining clean-up function by registering an event, element.on( ‘$destroy’, …). It is a good practice to remove event listeners once the ‘$destroy’ event is broadcasted, to avoid instances of memory leaks. Listeners registered to scopes and elements are automatically cleaned up when they are destroyed.
  7. Controller vs Link function: It is often confusing to beginners on when to use controller function and when to define link function within the custom directive. Thumb rule is use controller function when there is a need to expose APIs to other directives. In other cases, one could use link function.
  8. DOM Manipulation: Use the “link” option if there is a need to modify the DOM.
  9. Access to Outside Scope Object: If there is a need for directives’ content to access content from outer scope object (not defined within directive), one may want to use “transclude: true”.
  10. Usage of @attr, =attr, &attr:Following is details related with @attr, =attr and &attr:
    • @attr:@ binds a directive scope property to the evaluated value of the DOM attribute. If you use name=name1 or name=”name1″, the value of DOM attribute “name” is simply the string name1. If you use name=”{{name}}”, the value of the DOM attribute “name” is the interpolated value of{{name}}. Simply speaking, with @attr, one can bind a isolated scope property to a DOM attribute. This sets up a one-way databinding from the parent scope to the isolated scope. If the parent scope changes, the isolated scope will reflect that change, but not the other way around.
    • =attr:= binds a directive scope property to a parent scope property. So with =, you use the parent scope property name as the value of the DOM attribute. And if isolated scope property changes, then it will update the property that exists in parent scope.
    • &attr:The & symbol is used to to call an expression on the parent scope from the isolated scope.
    For detailed example, read the details on this page

Monday, January 4, 2016

Best practices for organizing folder structures for large and scalable angular js applications


Conclusion:
Organise based on the functional areas. Eg. for e-commerce shopping app its better to use following structure

  • cart/
    • CartModel.js
    • CartService.js
  • common/
    • directives.js
    • filters.js
  • product/
    • search/
      • SearchResultsController.js
      • SearchResultsModel.js
    • ProductDetailController.js
    • ProductModel.js
    • ProductService.js
  • user/
    • LoginController.js
    • RegistrationController.js
    • UserModel.js
    • UserService.js

For more info read following two articles from John papa and cliff maeyer

Code Organization in Large AngularJS and JavaScript Applications

Many developers struggle with how to organize an application's code base once it grows in size. I've seen this recently in AngularJS and JavaScript applications but historically it's been a problem across all technologies including many Java and Flex apps I've worked on in the past.
The general trend is an obsession with organizing things by type. It bears a striking resemblance to the way people organize their clothing.

Piles on the Floor

Let's take a look at angular-seed, the official starting point for AngularJS apps. The "app" directory contains the following structure:
  • css/
  • img/
  • js/
    • app.js
    • controllers.js
    • directives.js
    • filters.js
    • services.js
  • lib/
  • partials/
The JavaScript directory has one file for every type of object we write. This is much like organizing your clothes into different piles on the floor. You have a pile of socks, underwear, shirts, pants, etc. You know your black wool socks are in that pile in the corner but it's going to take a while to dig them out.
This is a mess. People shouldn't live like this and developers shouldn't code like this. Once you get beyond a half-dozen or so controllers or services these files become unwieldy: objects you're looking for are hard to find, file changesets in source control become opaque, etc.

The Sock Drawer

The next logical pass at organizing JavaScript involves creating a directory for some of the archetypes and splitting objects into their own files. To continue the clothing metaphor, we've now invested in a nice mohaghony dresser and plan to put socks in one drawer, underwear in another, and neatly fold our pants and shirts in still others.
Let's imagine we're building a simple e-commerce site with a login flow, product catalog and shopping cart UI's. We've also defined new archetypes for Models (business logic and state) and Services (proxies to HTTP/JSON endpoints) rather than lumping them into Angular's single "service" archetype. Our JavaScript directory can now look like this:
  • controllers/
    • LoginController.js
    • RegistrationController.js
    • ProductDetailController.js
    • SearchResultsController.js
  • directives.js
  • filters.js
  • models/
    • CartModel.js
    • ProductModel.js
    • SearchResultsModel.js
    • UserModel.js
  • services/
    • CartService.js
    • UserService.js
    • ProductService.js
Nice! Objects can now be located easily by browsing the file tree or using IDE shortcuts, changesets in source control now clearly indicate what was modified, etc. This is a major improvement but still suffers from some limitations.
Imagine you're at the office and realize you need a few outfits dry-cleaned for a business trip tomorrow morning. You call home and ask your significant other to take your black charcoal and blue pinstripe suits to the cleaners. And don't forget the grey shirt with the black paisley tie and the white shirt with the solid yellow tie. Imagine that your significant other is completely unfamiliar with the your dresser and wardrobe. As they sift through your tie drawer they see three yellow ties. Which one to pick?
Wouldn't it be nice if your clothing was organized by outfit? While there are practical constraints like cost and space that make this difficult with clothing in the real world, something similar can be done with code at zero cost.

Modularity

Hopefully the trite metaphors haven't been too tedious but here's the recap:
  • Your significant other is the new developer on the team who's been asked to fix a bug on one of the many screens in your app.
  • The developer sifts through the directory structure and sees all the controllers, models and services neatly organized. Unfortunately it tells him/her nothing about which objects are related or have dependencies on one another.
  • If at some point the developer wants to reuse some of the code, they need to collect files from a bunch of different folders and will invariably forget code from another folder somewhere else.
Believe it or not, you rarely have a need to reuse all of the controllers from the e-commerce app in the new reporting app you're building. You may however have a need to reuse some of the authentication logic. Wouldn't it be nice if that was all in one place? Let's reorganize the app based on functional areas:
  • cart/
    • CartModel.js
    • CartService.js
  • common/
    • directives.js
    • filters.js
  • product/
    • search/
      • SearchResultsController.js
      • SearchResultsModel.js
    • ProductDetailController.js
    • ProductModel.js
    • ProductService.js
  • user/
    • LoginController.js
    • RegistrationController.js
    • UserModel.js
    • UserService.js
Any random developer can now open the top-level folder and immediately gain insight into what the application does. Objects in the same folder have a relationship and some will have dependencies on others. Understanding how the login and registration process work is as easy as browsing the files in that folder. Primitive reuse via copy/paste can at least be accomplished by copying the folder into another project.
With AngularJS we can take this a step further and create a module of this related code:
var userModule = angular.module('userModule',[]);
userModule.factory('userService', ['$http', function($http) {
return new UserService($http);
}]);
userModule.factory('userModel', ['userService', function(userService) {
return new UserModel(userService);
}]);
userModule.controller('loginController', ['$scope', 'userModel', LoginController]);
userModule.controller('registrationController', ['$scope', 'userModel', RegistrationController]);
view rawUserModule.js hosted with ❤ by GitHub
If we then place UserModule.js into the user folder it becomes a "manifest" of the objects used in that module. This would also be a reasonable place to add some loader directives for RequireJS or Browserify.

Tips for Common Code

Every application has common code that is used by many modules. We just need a place for it which can be a folder named "common" or "shared" or whatever you like. In really big applications there tends to be a lot of overlap of functionality and cross-cutting concerns. This can be made manageable through a few techniques:
  1. If your module's objects require direct access to several "common" objects, write one or more Facades for them. This can help reduce the number of collaborators for each object since having too many collaborators is typically a code smell.
  2. If your "common" module becomes large subdivide it into submodules that address a particular functional area or concern. Ensure your application modules use only the "common" modules they need. This is a variant of the "Interface segregation principle" from SOLID.
  3. Add utility methods onto $rootScope so they can be used by child scopes. This can help prevent having to wire the same dependency (such as "PermissionsModel") into every controller in the application. Note that this should be done sparingly to avoid cluttering up the global scope and making dependencies non-obvious.
  4. Use events to decouple two components that don't require an explicit reference to one another. AngularJS makes this possible via the $emit, $broadcast and $on methods on the Scope object. A controller can fire an event to perform some action and then receive a notification that the action completed.

Quick Note on Assets and Tests

I think there's more room for flexibility with respect to organizing HTML, CSS and images. Placing them in an "assets" subfolder of the module probably strikes the best balance between encapsulating the module's asset dependencies and not cluttering things up too much. However I think a separate top-level folder for this content which contains a folder structure that mirrors the app's package structure is reasonable too. I think it works well for tests as well.

Ref: http://www.johnpapa.net/angular-growth-structure/
Your Angular app is growing and you want your structure to adapt with it. Let's explore one way you can approach this.
As your app grows it becomes even more important to structure it in a way that makes it easy to manage and maintain it. I recently posted a common technique I use when I create a new app to help structure it. A simple structure by type is easy out of the gate, as the app generally starts with a single set of features. This usually starts as the first module you write. Now fast forward and imagine you are working on an app that has a handful of modules, with dozens of views and controllers. How do you organize your code now?

Options for Managing Growth

I like the type organization (folders for controllers and views, for example), but as an app grows, instead of putting that at the root and putting all views and controllers in their own folders, sometimes it makes more sense to organize them 1 level deeper into feature areas. For a similar approach, see Cliff Meyer's great post. We tend to look for the one right answer, but often there are many good options. You have to choose what feels right for you.
So how might this look? There are a lot of ways we could do this, but let's focus on two options side by side. NgStructureCompare

Sort By Type

On the left we have the app organized by type. Not too bad for smaller apps, but even here you can start to see it gets more difficult to find what you are looking for. When I want to find a specific view and its controller, they are in different folders. It can be good to start here if you are not sure how else to organize the code as it is quite easy to shift to the technique on the right: structure by feature.

Sort By Feature

On the left the project is organized by feature. All of the layout views and controllers go in the layout folder, the admin content goes in theadmin folder, and the services that are used by all of the areas go in the services folder. The idea here is that when you are looking for the code that makes a feature work, it is located in one place. Services are a bit different as they "service" many features. I like this once my app starts to take shape as it becomes a lot easier to manage for me.

Structuring for Modules

This structure by feature can be extended fairly easily once you start to gather multiple modules. When i see common functionality that can be extracted and re-used, I like to break that out into its own module. In the structure by feature notice that I have a common folder. In there I have another module named common that contains logging, progress bars, and other common features. Sometimes I break this out such that the modules are the first folder under the app folder. But in this case I just had 2 modules: common and the main app.
As the app grows even further, you can have many modules. That's when I like to have the modules in their own folders under app. Specifically when the main app has very separate feature areas like point of sale and inventory, for example. Those could be their own modules. Then I'd also have some other common modules. See where this is heading? Separate silos of features that are easily separated (think Separation of Concerns).

Your Choice

Ultimately how you organize your code is entirely up to you and your team. Don't feel married to your choice, you can adapt when it makes sense. The key is in keeping good separation and making it easy to find your code.
If you are interested in SPA, HTML5, Angular, BreezeJS or JavaScript patterns then you will love my upcoming course at Pluralsight, due out in October 2013. Or if you prefer Knockout and Durandal check out my courses on Pluralsight today.

Ref: http://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/

CSS Specificity: Things You Should Know

Advertisement
Shop Marketplace by InVision
Apart from Floats, the CSS Specificity is one of the most difficult concepts to grasp in Cascading Stylesheets. The different weight of selectors is usually the reason why your CSS-rules don’t apply to some elements, although you think they should have. In order to minimize the time for bug hunting you need to understand, how browsers interpret your code. And to understand that, you need to have a firm understanding on how specificity works. In most cases such problems are caused by the simple fact that somewhere among your CSS-rules you’ve defined a more specific selector.
CSS Specificity isn’t simple. However, there are methods to explain it in a simple and intuitive way. And that’s what this article is all about. You’ll understand the concept if you love Star WarsReally.
Let’s take a look at some important issues related to CSS Specificity as well as examples, rules, principles, common solutions and resources.
  • You can find the most important things you should know about CSS specificity in a brief overview at the beginning of the article.

CSS Specificity: An Overview Link

  1. Specificity determines, which CSS rule is applied by the browsers.
  2. Specificity is usually the reason why your CSS-rules don’t apply to some elements, although you think they should.
  3. Every selector has its place in the specificity hierarchy.
  4. If two selectors apply to the same element, the one with higher specificity wins.
  5. There are four distinct categories which define the specificity level of a given selector: inline styles, IDs, classes+attributes and elements.
  6. You can understand specificity if you love Star WarsCSS Specificity Wars.
  7. You can understand specificity if you love poker: CSS Specificity for Poker Players
  8. When selectors have an equal specificity value, the latest rule is the one that counts.
  9. When selectors have an unequal specificity value, the more specific rule is the one that counts.
  10. Rules with more specific selectors have a greater specificity.
  11. The last rule defined overrides any previous, conflicting rules.
  12. The embedded style sheet has a greater specificity than other rules.
  13. ID selectors have a higher specificity than attribute selectors.
  14. You should always try to use IDs to increase the specificity.
  15. A class selector beats any number of element selectors.
  16. The universal selector and inherited selectors have a specificity of 0, 0, 0, 0.
  17. You can calculate CSS specificity with CSS Specificity Calculator.

What is Specificity? Link

  • If two selectors apply to the same element, the one with higher specificity wins.
AdvertisementShop Marketplace by InVision

Specificity hierarchy Link

  • Every selector has its place in the specificity hierarchy. There are four distinct categories which define the specificity level of a given selector:
    1. Inline styles (Presence of style in document).
    An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g. <h1 style="color: #fff;">
    2. IDs (# of ID selectors)
    ID is an identifier for your page elements, such as #div.
    3. Classes, attributes and pseudo-classes (# of class selectors).
    This group includes .classes[attributes] and pseudo-classes such as :hover:focus etc.
    4. Elements and pseudo-elements (# of Element (type) selectors).
    Including for instance :before and :after.
If you don’t know what exactly each of these terms stands for, you can take a look at the brief overview of them; in the last section of this article.

How to measure specificity? Link

  • Memorize how to measure specificity. “Start at 0, add 1000 for style attribute, add 100 for each ID, add 10 for each attribute, class or pseudo-class, add 1 for each element name or pseudo-element. So in
    body #content .data img:hover
    the specificity value would be 122 (0,1,2,2 or 0122): 100 for #content, 10 for .data, 10 for :hover, 1 for body and 1 for img.” [CSS Specificity]
  • Alternative way: “Count the number of ID attributes in the selector (= a). Count the number of other attributes and pseudo-classes in the selector (= b). Count the number of element names and pseudo-elements in the selector (= c). Concatenating the three numbers a-b-c gives the specificity. [CSS Selector Specificity]
  • CSS Specificity Wars – Cheat Sheet
    To help me understand calculating specificity better I made a chart based on the following specificity (or Sith power) values. Each character (selector) is given its own Sith power (specificity value) depending on how powerful they are in the ways of the Dark Side. A storm trooper is less powerful than Vader who is in turn less powerful than the Emperor.
CSS Specificity Wars

    Specificity Examples: Test Yourself Link

    It’s easier to calculate the specificity using the first method. Let’s find out, how it actually is done.
    1* { }0
    2li { }1 (one element)
    3li:first-line { }2 (one element, one pseudo-element)
    4ul li { }2 (two elements)
    5ul ol+li { }3 (three elements)
    6h1 + *[rel=up] { }11 (one attribute, one element)
    7ul ol li.red { }13 (one class, three elements)
    8li.red.level { }21 (two classes, one element)
    9style=””1000 (one inline styling)
    10p { }1 (one HTML selector)
    11div p { }2 (two HTML selectors)
    12.sith10 (one class selector)
    13div p.sith { }12 (two HTML selectors and a class selector)
    14#sith100 (one id selector)
    15body #darkside .sith p { }112 (HTML selector, id selector, class selector, HTML selector; 1+100+10+1)

    Specificity: Basic Principles Link

    • Equal specificity: the latest rule is the one that counts. “If you have written the same rule into your external style sheet twice, than the lower rule in your style sheet is closer to the element to be styled, it is deemed to be more specific and therefore will be applied.
      When selectors have an equal specificity value, such as
      #content h1 {
      padding: 5px;
      }
      
      #content h1 {
      padding: 10px;
      }
      where both rules have the specificity 0, 1, 0, 1, the latter rule is always applied.

    Specificity Rules Link

    • ID selectors have a higher specificity than attribute selectors. For example, in HTML, the selector #p123 is more specific than [id=p123]in terms of the cascade. Example: in
      A:
      a#a-02 { background-image : url(n.gif); }
      
      and
      
      B:
      a[id="a-02"] { background-image : url(n.png); }
      the first rule (A) is more specific than the second one (B). [W3C CSS 2.1 Specification]
    • Contextual selectors are more specific than a single element selector. It also holds for other selectors involving more than one HTML element selector. [Cascade Inheritance]
    • The embedded style sheet is closer to the element to be styled. So in the following situation
      CSS:
      #content h1 {
      padding: 5px;
      }
      (X)HTML:
      <style type="text/css">
      #content h1 {
      padding: 10px;
      }
      </style>
      the latter rule will be applied.
    • The last rule defined overrides any previous, conflicting rules. For example, given these two rules
      p { color: red; background: yellow }
      p { color: green }
      paragraphs would appear in green text. They would also have a yellow background however, because the first rule is not completely negated. [BrainJar.com]
    • A class selector beats any number of element selectors..introduction beats html body div div h2 p. [CSS Specificity for Poker Players]
    • The universal selector has a specificity of 0, 0, 0, 0. *body * and similar selectors have a zero specificity. Inherited values also have a specificity of 0, 0, 0, 0. [CSS Specificity Clarified]

    Specificity Example Link

    • Consider three code fragments:
      A: h1
      B: #content h1
      C: <div id="content">
      <h1 style="color: #fff">Headline</h1>
      </div>
      The specificity of A is 0,0,0,1 (one element), the specificity of B is0,1,0,1 (one ID reference point and one element), the specificity value of C is 1,0,0,0, since it is an inline styling.
      Since
      0001 = 1 < 0101 = 101 < 1000,
      the third rule has a greater level of specificity, and therefore will be applied. If the third rule didn’t exist, the second rule would have been applied.

    Specificity in Practice Link

    • Use LVHA for link styling.
      “To ensure that you see your various link styles, you’re best off putting your styles in the order “link-visited-hover-active”, or “LVHA” for short.” [Link Specificity]
    • Never use !important.
      “If you’re having specificity issues, there’s some quick ways to solve it. First, avoid !important.” “The !important declaration overrides normal declarations, but is unstructured and rarely required in an author’s style sheet.” [Understanding SpecificitySelector Specificity]
    • Use id to make a rule more specific.
      Replacing a.highlight with ul#blogroll a.highlight changes the specificity from 0, 0, 1, 1 to 0, 1, 1, 2.
    • Minimize the number of selectors.
      “Use the least number of selectors required to style an element.” [Understanding Specificity]

    CSS Specificity Tools & Resources Link

    • CSS Specificity for Poker Players
      If you’re not from the programming world and CSS seems a bit confusing, perhaps this analogy may help clear some concepts up. Think of CSS rules as poker hands. The best hand determines an element’s style.
    • CSS specificity calculator
      Calculates the specificity of a given selector.
    • Understanding Specificity Tutorial
      In this tutorial you will look at specificity. Specificity is a type of weighting that has a bearing on how your cascading style sheet (CSS) rules are displayed. All rules in your style sheet carry a specificity rating regardless of selector type, although the weighting that is given to each selector type varies and will ultimately affect the styling of your web documents.
    • Cascade Inheritance: Specificity
      At this point it might be timely to have a quick discussion of specificity. Both inside a single style sheet, and in a cascade of style sheets, it should be clear that more than one rule can apply to the same element. What happens when two properties in separate rules which both apply to an element contradict one another?
    • CSS 2.1 Selectors Explained
      An extensive overview of CSS 2.1 selectors. Learning how to use the full range of CSS selectors available in CSS 2.1 properly can actually help you keep your HTML a lot cleaner. It will let you minimise unnecessary use of the class attribute and the need for adding extraneous div and spanelements to the markup.
    • CSS Specificity Bugs in IE
      A brief overview of specificity bugs implemented in Microsoft Internet Explorer/Win.
    • CSS Structure and Rules
      Basic Syntax, Pseudo-classes and Pseudo-elements, Cascading Order.
    • Specificity
      It may not seem like something that important, and in most cases you won’t come across any conflicts at all, but the larger and more complex your CSS files become, or the more CSS files you start to juggle with, the greater likelihood there is of conflicts turning up.

    What is what? Link

    • selector is the element that is linked to a particular style. E.g. p in
      p { padding: 10px; }
    • class selector is a selector that uses a defined class (multiple per page). E.g. p.section in
      p.section { padding: 10px; }
    • An ID selector is a selector that uses an individually assigned identifier (one per page). E.g. p#section in
      CSS: #section { padding: 10px; }
      (X)HTML: <p id="section">Text</>
    • contextual selector is a selector that defines a precise cascading order for the rule. E.g. p span in
      p span { font-style: italic; }
      defines that all span-elements within a p-element should be styled in italics.
    • An attribute selector matches elements which have a specific attribute or its value. E.g. p span in
      p[title] { font-weight: bold; }
      matches all p-elements which have a title attribute.
    • Pseudo-classes are special classes that are used to define the behavior of HTML elements. They are used to add special effects to some selectors, which are applied automatically in certain states. E.g. :visitedin
      a:visited { text-decoration: underline; }
    • Pseudo-elements provide designers a way to assign style to content that does not exist in the source document. Pseudo-element is a specific, unique part of an element that can be used to generate content “on the fly”, automatic numbering and lists. E.g. :first-line or :after in
      p:first-line { font-variant: small-caps; }
      a:link:after { content: " (" attr(href) ")";