KEMBAR78
Refactoring css | PPTX
Ed Charbeneau
Sr. Web Developer for Sypris Solutions
Code PaLOUsa Co-Chairman
Author: Simple-Talk
ResponsiveMVC.net
Twitter: @EdCharbeneau
Refactoring
CSS
CSS??
CSSS??
Menu
Day 1
Chicken
Refactoring
CSS
Menu
Day 2
Chicken Sandwich
Refactoring
CSS
Menu
Day 3
Chicken Sandwich
Refactoring
CSS
Menu
Day 4
Chicken Sandwich
Refactoring
CSS
Menu
Day 5?
Chicken Salsa Salad Sandwich
Food Poisoning!
Refactoring
CSS
Cooking bad code
Revision 1
.menu .chicken { display: block }
Refactoring
CSS
Cooking bad code
Revision 1
.menu .chicken { display: block }
Revision 2
.menu .chicken { float: left }
Refactoring
CSS
Cooking bad code
Revision 1
.menu .chicken { display: block }
Revision 2
.menu .chicken { float: left }
Revision 3
.menu .chicken { float: right }
Refactoring
CSS
Cooking bad code
Revision 1
.menu .chicken { display: block }
Revision 2
.menu .chicken { float: left }
Revision 3
.menu .chicken { float: right }
Revision 4
.menu .chicken { float: none !important } //food poisoning
Refactoring
CSS
Maintainability & Readability
Extensibility
Improving performance
Semantics
Reasons to refactor
Maintainability & Readability
Smaller, more concise chunks of code
Semantic naming conventions
We need to make it easier for others to read our code,
and to understand the intention behind it.
Refactoring
CSS
Extensibility
module
Object Oriented Programming (OOP)
Don’t Repeat Yourself (DRY)
Modular programming & Portability
Refactoring
CSS
Improving Performance
.min
Reduce page load times by reducing requests to the server
request
Refactoring
CSS
Separation of Concerns (SoC), decoupling of CSS and HTML
Semantic Layout
CSS
visual style
HTML
document
layout
article row
Refactoring
CSS
Tools
Sass
CSS
Compass
(mixins)
compile
Sass overview
Sass code .SCSS is a superset of CSS
CSS is valid SCSS
Nesting
Variables
Mixins
http://sass-lang.com/
Refactoring
CSS
Converting CSS to Sass
Conversion tools
Web Workbench
Online http://css2sass.heroku.com/
Rename .css to .scss
Refactoring
CSS
Cleaning up with variables
// Color variables
$base-color: #d0d0d0;
$accent-color: #0ca0c6;
$highlight-color: #FFF;
$contrast-color: #1e1e1e;
Refactoring
CSS
Reusable modules with mixins
@mixin button-base($margin: 2px, $padding: 10px) {
color: $contrast-color;
background-color: $base-color;
text-decoration: none;
display: block;
padding:$padding;
margin: $margin;
&:hover {
background-color: $highlight-color;
}
}
Refactoring
CSS
Cleaning up your HTML with semantic styles
<header id="master-header">
<div class=“row">
<h1 class="col third">Example</h1>
<nav class="col two-thirds" id="primary">
Content…
</nav>
</div>
</header>
#master-header > div {
@include row;
h1 {
@include column($third);
}
nav#primary {
@include column($two-thirds);
}
}
CSSHTML
Refactoring
CSS
module
Prefixing an underscore to a Sass file name tells Sass
we intend to import the code as a module
No .css file is generated by the compiler
Partials and Imports
Directory / Files
/modules/_typography.scss
/modules/_grid.scss
Site.scss
@import "modules/typography";
@import "modules/grid";
Refactoring
CSS
Compass Overview
Collection of Sass Mixins
Common code that would otherwise be duplicated
across other frameworks and extensions
http://compass-style.org/
Refactoring
CSS
Using Compass
Replace standard boilerplate markup with Compass
mixins
Site.scss
//removed @import "modules/reset";
@import "compass/reset";
Refactoring
CSS
Finalizing the project
.min
Bundle and minify
output_style = :compressed
Refactoring
CSS
Does refactoring make sense?
Some projects may be too large or complex
Practice refactoring with a smaller project first
Apply the patterns to new projects
Questions?
?
?
Resources
Reading
– Refactoring CSS with Sass and Compass
– Using SASS and Compass in ASP.Net MVC with Web Workbench
Code Samples
– github.com/EdCharbeneau/RefactoringCSS/ (.NET MVC)
– github.com/EdCharbeneau/RefactoringCSSWithSass (Platform independent)
Tools
– Web Workbench (Visual Studio Plugin)
– Sass & Compass
Refactoring
CSS
Refactoring css

Refactoring css

Editor's Notes

  • #4 Have you written CSS?
  • #5 How about CSSS?I have a story that reminds me of how bad CSS is written. Before I was a web developer, I worked in manufacturing. Every day at lunch a food truck stopped by. A food truck we affectionately named “The Roach Coach”.
  • #6 One week the Roach Coach had planned to serve chicken.On the first day, they sold plain chicken.
  • #7 The next day the Roach Coach decided to serve left overs, so they put chicken on bread and made Chicken Sandwiches.
  • #8 By the third day, they were still getting rid of leftovers, so they chopped up the chicken, added some dressing, and served Chicken Salad Sandwiches.
  • #9 By day 4 things started got scary when they took their chicken salad sandwiches and decided re-serve the sandwiches again. This time they added salsa to the mix and changed the sticker to Chicken Salsa Sandwich.They even struck through “Salad” the old label, maybe to save money?
  • #10 In my opinion, they could have just labeled them Food Poisoning.And what does all of this have to do with writing CSS?...
  • #11 Developers do the same thing, we cook bad code.A project gets finished, the team moves on and “maintenance mode” begins.
  • #12 For some reason, the code requires an update. So what is the easiest thing to do?Open the CSS file and override the styles by adding new code to the bottom.
  • #13 And we do it over and over again.
  • #14 Until we get food poisoning. (see: !important)
  • #15 What can we do to fix this problem?
  • #16 We need to make it easier for others to read our code, and to understand the intention behind it. Sass and Compass are preprocessors, and we can use them with CSS to provide semantic naming conventions for values (eg: $base-color) and dividing code into smaller, more concise, chunks. Sass allows us to use variables in place of literals to hold values, while Compass encapsulates common coding patterns. By using them, there is less code to maintain.
  • #17 Familiar software development practices like OOP (object oriented programming) and DRY (don’t repeat yourself) are made possible by using Sass mixins. Mixins can be used to define styles of common UI elements with a modular approach so that they are portable within a project as well as in other projects.
  • #18 You can certainly achieve better performance in page-loading by refactoring your CSS code but, unlike refactoring C#, VB, or JavaScript code, the benefits are more likely from decreasing the load time of the code rather than from increasing the execution (rendering) speed.
  • #19 We will use refactoring to define a clear SoC (Separation of Concerns). HTML is responsible for describing the document, while the role of CSS is to provide a visual style. We can create a clear separation between HTML and CSS by writing styles that can be applied directly to HTML elements rather than through class attributes.
  • #20 Read the article https://www.simple-talk.com/dotnet/asp.net/refactoring-css-with-sass-and-compass/ for more details.Follow commits on https://github.com/EdCharbeneau/RefactoringCSS for step by step techniques
  • #22 Convert this using http://css2sass.heroku.comhttps://gist.github.com/EdCharbeneau/bb6a1846c55eb05ef2af
  • #26 We can make our code more modular by using ‘partials’ and imports in order to separate code by its responsibilities. By modularizing the code, we can restructure our files so they are portable and easier to locate within a project.Partials are a naming convention that tells Sass that we do not wish to generate a .cssfile from the Sass code. Instead of generating a .css file for each module in our project, we will instead use the @import method. 
  • #27 Discover Sass &amp; Compass ExtensionsFind the perfect tool for your next Sass or Compass project.http://www.sache.in/
  • #31 Practice the techniques of refactoring with Sass on a smaller project first and learn a disciplined approach to writing DRY Sass code and understand better when modular code can be abstracted.