SlideShare ist ein Scribd-Unternehmen logo
1 von 33
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

Weitere ähnliche Inhalte

Ähnlich wie Refactoring css

Css for Development
Css for DevelopmentCss for Development
Css for Development
tsengsite
 
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.pptwaxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
Ismaciil2
 

Ähnlich wie Refactoring css (20)

Thuray css3
Thuray css3Thuray css3
Thuray css3
 
Intermediate Web Design
Intermediate Web DesignIntermediate Web Design
Intermediate Web Design
 
Advanced sass
Advanced sassAdvanced sass
Advanced sass
 
David Weliver
David WeliverDavid Weliver
David Weliver
 
Css for Development
Css for DevelopmentCss for Development
Css for Development
 
How to use CSS3 in WordPress
How to use CSS3 in WordPressHow to use CSS3 in WordPress
How to use CSS3 in WordPress
 
Class
ClassClass
Class
 
Class
ClassClass
Class
 
How to use CSS3 in WordPress - Sacramento
How to use CSS3 in WordPress - SacramentoHow to use CSS3 in WordPress - Sacramento
How to use CSS3 in WordPress - Sacramento
 
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.pptwaxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
 
css1.ppt
css1.pptcss1.ppt
css1.ppt
 
6 css week12 2020 2021 for g10
6 css week12 2020 2021 for g106 css week12 2020 2021 for g10
6 css week12 2020 2021 for g10
 
Learn Sass and Compass quick
Learn Sass and Compass quickLearn Sass and Compass quick
Learn Sass and Compass quick
 
CSS Introduction
CSS IntroductionCSS Introduction
CSS Introduction
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End Methodologies
 
Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)
 
Slow kinda sucks
Slow kinda sucksSlow kinda sucks
Slow kinda sucks
 
Hardboiled Front End Development — Found.ation
Hardboiled Front End Development — Found.ationHardboiled Front End Development — Found.ation
Hardboiled Front End Development — Found.ation
 
CSS Extenders
CSS ExtendersCSS Extenders
CSS Extenders
 

Mehr von Ed Charbeneau

Mehr von Ed Charbeneau (20)

Writing JavaScript for C# Blazor.pptx
Writing JavaScript for C# Blazor.pptxWriting JavaScript for C# Blazor.pptx
Writing JavaScript for C# Blazor.pptx
 
Blazor Stability Testing Tools for Bullet Proof Applications
Blazor Stability Testing Tools for Bullet Proof ApplicationsBlazor Stability Testing Tools for Bullet Proof Applications
Blazor Stability Testing Tools for Bullet Proof Applications
 
Secrets of a Blazor Component Artisan (v2)
Secrets of a Blazor Component Artisan (v2)Secrets of a Blazor Component Artisan (v2)
Secrets of a Blazor Component Artisan (v2)
 
Modernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptxModernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptx
 
Modernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptxModernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptx
 
Blazor Full-Stack
Blazor Full-StackBlazor Full-Stack
Blazor Full-Stack
 
Secrets of a Blazor Component Artisan
Secrets of a Blazor Component ArtisanSecrets of a Blazor Component Artisan
Secrets of a Blazor Component Artisan
 
Writing java script for Csharp's Blazor
Writing java script for Csharp's BlazorWriting java script for Csharp's Blazor
Writing java script for Csharp's Blazor
 
Goodbye JavaScript Hello Blazor
Goodbye JavaScript Hello BlazorGoodbye JavaScript Hello Blazor
Goodbye JavaScript Hello Blazor
 
Razor into the Razor'verse
Razor into the Razor'verseRazor into the Razor'verse
Razor into the Razor'verse
 
Blazor
BlazorBlazor
Blazor
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2
 
The future of .NET lightning talk
The future of .NET lightning talkThe future of .NET lightning talk
The future of .NET lightning talk
 
Into the next dimension
Into the next dimensionInto the next dimension
Into the next dimension
 
Giving Clarity to LINQ Queries by Extending Expressions
Giving Clarity to LINQ Queries by Extending ExpressionsGiving Clarity to LINQ Queries by Extending Expressions
Giving Clarity to LINQ Queries by Extending Expressions
 
What is new in Q2 2015
What is new in Q2 2015What is new in Q2 2015
What is new in Q2 2015
 
TelerikNEXT What's new in UI for ASP.NET AJAX
TelerikNEXT What's new in UI for ASP.NET AJAXTelerikNEXT What's new in UI for ASP.NET AJAX
TelerikNEXT What's new in UI for ASP.NET AJAX
 
Journey to JavaScript (from C#)
Journey to JavaScript (from C#)Journey to JavaScript (from C#)
Journey to JavaScript (from C#)
 
Don't be a stereotype: Rapid Prototype
Don't be a stereotype: Rapid PrototypeDon't be a stereotype: Rapid Prototype
Don't be a stereotype: Rapid Prototype
 
A crash course in responsive design
A crash course in responsive designA crash course in responsive design
A crash course in responsive design
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Refactoring css

Hinweis der Redaktion

  1. Have you written CSS?
  2. 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”.
  3. One week the Roach Coach had planned to serve chicken.On the first day, they sold plain chicken.
  4. The next day the Roach Coach decided to serve left overs, so they put chicken on bread and made Chicken Sandwiches.
  5. 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.
  6. 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?
  7. In my opinion, they could have just labeled them Food Poisoning.And what does all of this have to do with writing CSS?...
  8. Developers do the same thing, we cook bad code.A project gets finished, the team moves on and “maintenance mode” begins.
  9. 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.
  10. And we do it over and over again.
  11. Until we get food poisoning. (see: !important)
  12. What can we do to fix this problem?
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. 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
  18. Convert this using http://css2sass.heroku.comhttps://gist.github.com/EdCharbeneau/bb6a1846c55eb05ef2af
  19. 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. 
  20. Discover Sass &amp; Compass ExtensionsFind the perfect tool for your next Sass or Compass project.http://www.sache.in/
  21. 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.