{"id":38761,"date":"2016-07-30T20:55:11","date_gmt":"2016-07-30T15:25:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38761"},"modified":"2017-09-12T12:43:12","modified_gmt":"2017-09-12T07:13:12","slug":"angular-2-component-styling","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angular-2-component-styling\/","title":{"rendered":"Angular 2 Component styling"},"content":{"rendered":"<p>Styling for large <a title=\"Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Angular application<\/a> is a challenging task as most of the times styles easily gets mixed and confusing. Major issue that is encountered when we try to structure our styles and give proper naming for individual styles.<\/p>\n<p>To resolve the issue many patterns were introduced to enhance style organization and most of these patterns are implemented when we make use of pre-processors like Sass and Less. The main thing about these patterns is that they suggest organizing our styles and templates in the form of COMPONENTS.<\/p>\n<p><a title=\"Angular 2.0 Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">Angular 2<\/a> is component based which means that each and every UI functionality is built as a component. A component comes with HTML, JavaScript and also has it\u2019s own styles that belong to it. All we need to do is to define the styles in our component, or at least declare, where to get those from. There are three ways to associate CSS styles to a component in Angular 2: Component inline styles,style urls and template inline styles. Let\u2019s explore them one by one.<\/p>\n<p><strong>Component inline styles<\/strong><br \/>\nThe simplest way to add styles to a component is taking advantage of the @Component decorators it allow us to define component inline styles. All we need to do is to add a styles property to the decorator and define the styles. To see what that looks like, below is the code snippet:<\/p>\n<p>[html]@Component({<br \/>\n  selector: &#8216;test&#8217;,<br \/>\n  templateUrl: test.html&#8217;,<br \/>\n  styles: [`<br \/>\n    .test {<br \/>\n    color: yellow;<br \/>\n    }<br \/>\n  `]<br \/>\n  })<br \/>\n  class Test{<br \/>\n  @Input() title: string;<br \/>\n}<br \/>\n[\/html]<\/p>\n<p><strong>So where did these style end up in the DOM?<\/strong><br \/>\nIf we run this in our browser, we see that Angular takes the defined styles, and writes them into the head of the HTML document. Here\u2019s what it looks like:<\/p>\n<p>[html]<\/p>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n  &lt;head&gt;<br \/>\n    &lt;style&gt;<br \/>\n      .test {<br \/>\n      \tcolor: yellow;<br \/>\n      }<br \/>\n    &lt;\/style&gt;<br \/>\n  &lt;\/head&gt;<br \/>\n  &lt;body&gt;<\/p>\n<p>  &lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/html]<\/p>\n<p><strong>So why Angular takes all the styles and puts them up there?<\/strong><br \/>\nIt is because of View Encapsulation. Angular 2 comes with three different view encapsulation types in order to support both, browsers that don\u2019t support Shadow DOM, and also the ones that do support it.<\/p>\n<p>Angular 2 uses the Emulated View Encapsulation by default which means there\u2019s no usage of any Shadow DOM at all. One of the features of Shadow DOM is style encapsulation. It allows us to scope styles to a specific component without affecting the outer world.<\/p>\n<p>To take advantage of style encapsulation, styles have to be put into the shadowRoot of a component. Due to the Shadow DOM strategy that is used, there is no shadowRoot to put our styles into. That\u2019s why Angular writes them into the head.<\/p>\n<p><strong>Styles urls<\/strong><br \/>\nWe never mix our styles with our application code. The tag, allows us to fetch and embed a style sheet from a server. Angular 2 components allow us to define styleUrls, so that styles don\u2019t have to be written into the component. Below is an example:<\/p>\n<p>[html]@Component({<br \/>\nselector: test,<br \/>\ntemplateUrl:\u2019test.html&#8217;,<br \/>\nstyleUrls: [test.css&#8217;]<br \/>\n})<br \/>\nclass Test{<br \/>\n@Input() title: string;<br \/>\n}<br \/>\n[\/html]<\/p>\n<p>Where do those end up in the DOM?<\/p>\n<p>Well, for the same reason as explained earlier, they are written into the head of the document. But not only that, when Angular fetches the style resources, it takes the text response, inlines and appends them after all component inline styles. So if we have a configuration like this:<\/p>\n<p>[html]@Component ({<br \/>\nselector: &#8216;test\u2019&#8217;,<br \/>\ntemplateUrl: &#8216;test.html&#8217;,<br \/>\nstyles: [&#8216;.test { color: yellow; }&#8217;],<br \/>\nstyleUrls: [&#8216;test.css&#8217;]<br \/>\n})<br \/>\nclass Test{<br \/>\n@Input() title: string;<br \/>\n}<br \/>\nAnd the test.css content would look like this:<br \/>\n.test {<br \/>\ncolor: green;<br \/>\n}[\/html]<\/p>\n<p>The document head that looks something like this:<\/p>\n<p>[html]<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n  &lt;head&gt;<br \/>\n    &lt;style&gt;<br \/>\n      .test {<br \/>\n      color: yellow;<br \/>\n      }<br \/>\n    &lt;\/style&gt;<br \/>\n    &lt;style&gt;<br \/>\n      .test {<br \/>\n      color: green;<br \/>\n      }<br \/>\n    &lt;\/style&gt;<br \/>\n  &lt;\/head&gt;<br \/>\n  &lt;body&gt;<br \/>\n  &#8230;<br \/>\n  &lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>[\/html]<\/p>\n<p>It brings us to the conclusion that styles defined in style urls will always be appended and therefore override styles defined in the component, unless the inline styles don\u2019t have a higher specificity.<\/p>\n<p><strong>Template inline styles<\/strong><br \/>\nWe can write our styles directly into the DOM. When thinking in Web Components it\u2019s quite common to put styles directly into the template of a component, since they will be encapsulated when Shadow DOM is used.Those will be appended in the head of document, after the ones defined in the component or as style urls. Template inline styles always have the highest priority.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Styling for large Angular application is a challenging task as most of the times styles easily gets mixed and confusing. Major issue that is encountered when we try to structure our styles and give proper naming for individual styles. To resolve the issue many patterns were introduced to enhance style organization and most of these [&hellip;]<\/p>\n","protected":false},"author":888,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1439,3429,1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38761"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/888"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38761"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38761\/revisions"}],"predecessor-version":[{"id":51792,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38761\/revisions\/51792"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}