<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[patterns - Stapps.io]]></title><description><![CDATA[patterns - Stapps.io]]></description><link>https://blog.stapps.io/</link><generator>Ghost 0.11</generator><lastBuildDate>Thu, 01 Jan 2026 04:51:08 GMT</lastBuildDate><atom:link href="https://blog.stapps.io/tag/patterns/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[CSS structure & anti patterns (Part 2)]]></title><description><![CDATA[<p>This Is Part 2, you may want to read <strong><a href="https://blog.stapps.io/css-structure-anti-patterns-part1">Part 1 on CSS anti patterns first</a></strong></p>

<h2 id="howtostructureyourcss">How to structure your css.</h2>

<p>This is not to say this is the perfect way to work. But it's the way I've been working for years and has really helped me. Inspired by others</p>]]></description><link>https://blog.stapps.io/css-structure-anti-patterns-part2/</link><guid isPermaLink="false">c2077e88-beff-4ba3-a8ee-245a055fee7a</guid><category><![CDATA[CSS]]></category><category><![CDATA[Design]]></category><category><![CDATA[REC]]></category><category><![CDATA[Responsive]]></category><category><![CDATA[patterns]]></category><dc:creator><![CDATA[Andrew Stilliard]]></dc:creator><pubDate>Wed, 08 Jun 2016 15:07:19 GMT</pubDate><content:encoded><![CDATA[<p>This Is Part 2, you may want to read <strong><a href="https://blog.stapps.io/css-structure-anti-patterns-part1">Part 1 on CSS anti patterns first</a></strong></p>

<h2 id="howtostructureyourcss">How to structure your css.</h2>

<p>This is not to say this is the perfect way to work. But it's the way I've been working for years and has really helped me. Inspired by others and by the book <a href="https://smacss.com/">SMACSS</a>.</p>

<h3 id="thinkinginmodules">Thinking in modules.</h3>

<p>Modular design for me means to separate logical pieces of code for maintainability and reuse. </p>

<p>If everything you do is in one big file, no comments or anything separating parts, all code is very specific and cannot be easily re-used, then it'll be difficult to maintain. </p>

<p>Working in modules you'll find you work faster and more precisely: you'll become a ninja at making quick changes for the client and the client will love you, or at least you'll be happier with your own code. </p>

<h3 id="exampletime">Example time:</h3>

<p>Here's some CSS maybe you've written before. Some text node colors, some buttons have a fancy style (I'll call that fancy-button for later), some text content areas with minor changes between them and a couple minor tweaks on a larger screen. </p>

<p><em>This will be a long code example but feel free to comment if you have any questions about it</em>/</p>

<p>Original code / what not to do:</p>

<p><strong># File: site.css</strong></p>

<pre><code class="language-css">h1 { color: #444444; }  
h2 { color: #444444; }  
h3 { color: #444444; }  
p { color: #000000; }

.header-search .button {
    background: orange;
    color: #FFF;
    padding: 1em;
}
.sidebar-filter #submit-button {
    background: orange;
    color: #FFF;
font-size: 20px;  
    padding: 1em;
}
#contact-form-button {
    padding: 1em;
}
.home-page-content {
  padding: 1em;
  *padding: 15px;
  border-top: 5px solid red;
}
.home-page-content h1 {
   color: red;
}
.about-page-content {
  padding: 1em;
  *padding: 15px;
 border-top: 5px solid blue;
}
.about-page-content h1{
color: blue;  
}

@media screen and (min-width: 35.5em) {
  h1, h2, h3 {
    border-bottom: 1px solid #CCC;
  }
  .home-page-content {
    margin: 0 1em;
  }
  .about-page-content {
    margin: 0 1em;
  }
}
</code></pre>

<p>To me, you can work with it, but it's not ideal, and as that grows it will get unmanageable. Lets separate it out to more modular files: <em>(The following files would be included/imported into your site.css file)</em> <br>
I'll comment above each part with the file name, and then under it will be notes on what changed and why its better. </p>

<p><strong># File: base.css</strong></p>

<pre><code class="language-css">/* ===== Base styles for site ===== */

/* --- main font colors --- */
h1,  
h2,  
h3 {  
    color: #444;
}
p {  
    color: #000;
}

/* --- default button style --- */
button,  
input[type="button"],  
input[type="submit"],  
.button {
    padding: 1em;
}

/* first breakpoint */
@media screen and (min-width: 35.5em) {
    h1,
    h2,
    h3 {
        /* add a clean border under headings */
        border-bottom: 1px solid #CCC;
    }
}
</code></pre>

<p>The above shows base styles for making the page, e.g. fonts, button styles etc.. <br>
Separating out the base styles like this helps you quickly find default style applied to all elements. It should be fairly simple, anything more complex should be it's own module or layout file. </p>

<p><strong># File: modules/fancy-button.css</strong></p>

<pre><code class="language-css ">/* ===== fancier designed buttons ===== */

.fancy-button {
    background: orange;
    color: #FFF;
}

/* --- fancy button mods --- */

/* larger text version (could be abstracted further) */
.fancy-button-large {
    font-size: 160%;
}
</code></pre>

<p>This module for the fancy-buttons assumes you have access to change the HTML, if that's not the case you could make the same module file, but instead of <code>.fancy-button</code> and <code>.fancy-button-large</code> you can comma separate the selectors like so:  </p>

<pre><code class="language-css ">/* ===== fancier designed buttons ===== */

.header-search .button,
#submit-button {
    background: orange;
    color: #FFF;
}

/* --- fancy button mods --- */

/* larger text version (could be abstracted further) */
#contact-form-button {
    font-size: 160%;
}
</code></pre>

<p>This keeps it modular in it's design, it's only the name of the selectors used that isn't as organised so that's still awesome. </p>

<p><em>Also notice <code>.sidebar-filter #submit-button</code> has been changed to <code>#submit-button</code> as an ID should be unique and the class prefix is therefore redundant.</em></p>

<p><strong># File: modules/page-content.css</strong></p>

<pre><code class="language-css ">/* ===== Page content areas styling ===== */

.page-content {
    border-top: 5px solid;
    padding: 1em;
    *padding: 15px; /* slight IE hack needed to account for a margin bug */
}

@media screen and (min-width: 35.5em) {
    .page-content {
        /* space out the content a bit when available */
        margin: 1em;
    }
}
</code></pre>

<p>This offers a generic module for page content, which could quickly be applied to all pages without need for copy and paste. As the design grows, this would be much more important. </p>

<p><strong># File: pages.css</strong></p>

<pre><code class="language-css ">/* ===== Specific page mods ===== */

/* --- home page (red theme) --- */

.home-page-content {
    border-top-color: red;
}
.home-page-content h1 {
    color: red;
}

/* --- about page (blue theme) --- */

.about-page-content {
    border-top-color: blue;
}
.about-page-content h1{
    color: blue;
}
</code></pre>

<p>This layout file for specific pages offers per page differences, which are now very minor and get down to exactly what is different, making maintaining and debugging much easier. </p>

<p>Once you've written your code like this, go back over and review it. Spending a little time re-reading, improving and re-factoring your own code is worth it. You'll learn more about the way you write code, patterns you could reuse. <br>
Using dev tools, you can look over some of your code too, specifically you can inspect the elements you've been working on and view the style tab. Here if you see that code is being overridden a lot of the time with large amounts crossed out then you could probably re-write the code, or re-think the way that element works and the css modules you have selectors to it. </p>

<p>Check out these resources on truly making your css skills awesome!</p>

<ul>
<li><a href="https://smacss.com/book/">https://smacss.com/book/</a></li>
<li><a href="http://maintainablecss.com/">http://maintainablecss.com/</a></li>
</ul>

<p>Also I've previously wrote about a <a href="https://blog.stapps.io/reading-list-for-modern-web-design/">reading list for modern web design</a>.</p>

<h3 id="somebestpracticestotakeaway">Some best practices to take away.</h3>

<ul>
<li>Think in modules.</li>
<li>Take pride in your code, format it well.</li>
<li>Understand what your code does and why and avoid magic.</li>
<li>Use comments and files to organise code and additional comments to explain why some things are needed.</li>
<li>Avoid specific browser hacks.</li>
</ul>

<p>My closing comment is: invest in your knowledge now, your future self will thank you. </p>

<p>If you have any questions? Or found a bug ;), please leave me a comment and I'll try to reply as quick as I can. </p>]]></content:encoded></item><item><title><![CDATA[CSS structure & anti patterns (Part 1)]]></title><description><![CDATA[<blockquote>
  <p>"CSS is quick to learn, but takes years to master". <br>
  - Not sure where I first read this but it's still true today. </p>
</blockquote>

<p>It's difficult to give years of experience as training. People expect designers to know things after a day of training that you'd normally learn with months or</p>]]></description><link>https://blog.stapps.io/css-structure-anti-patterns-part1/</link><guid isPermaLink="false">500dcc73-448f-4c96-85cc-d3cb3a22eca8</guid><category><![CDATA[CSS]]></category><category><![CDATA[Design]]></category><category><![CDATA[REC]]></category><category><![CDATA[Responsive]]></category><category><![CDATA[patterns]]></category><dc:creator><![CDATA[Andrew Stilliard]]></dc:creator><pubDate>Wed, 08 Jun 2016 15:06:45 GMT</pubDate><content:encoded><![CDATA[<blockquote>
  <p>"CSS is quick to learn, but takes years to master". <br>
  - Not sure where I first read this but it's still true today. </p>
</blockquote>

<p>It's difficult to give years of experience as training. People expect designers to know things after a day of training that you'd normally learn with months or years of experience. Some patterns I'll talk about in this post only reveal themselves as bad decisions later on. The best I can do is show you mistakes I've made, and other designers have made. Hopefully you can learn from where we went wrong ;). </p>

<h2 id="antipatterns">Anti-patterns.</h2>

<p><a href="https://en.wikipedia.org/wiki/Anti-pattern">Anti-patterns explained</a>: What not to do and why. <br>
<em>Found something controversial? Leave me a comment and we can discuss it further :).</em></p>

<h3 id="1avoidseparatingstylesforthesamethings">1. Avoid separating styles for the same things.</h3>

<p>A case of this I've seen before is separating into different device groups, e.g. mobile.css, tablet.css, desktop.css. <br>
Similar used to be done on sites with a ie7.css file etc. </p>

<p>On the face of it, it sounds like a good idea. Everything is separated out, but it's separated in the wrong way and ultimately it's a naive approach.  It often encourages duplication because the blocks of style are separated. This makes it difficult to track down specific styles for an element as they then span multiple files, which often ends in code being copied for each file as it's forgotten it's already set and inherited.  </p>

<p>Instead my advice is to build specific module css files, we'll come back to how you do this later in the post. </p>

<h3 id="2dontcopypastechunksofcode">2. Don't copy paste chunks of code.</h3>

<p>This one seems obvious when thinking about most coding languages, but maybe not so with your css. <br>
While working on your css, think about how some styles can be joined together. Remember you can join css selectors with commas so code such as:  </p>

<pre><code class="language-css">.banner p {
  margin: 0 0.5em;
  color: #222;
}
.banner h3 {
  margin: 0 0.5em;
  color: #444;
}
</code></pre>

<p>Can become this, which is much easier to maintain and keep consistent:  </p>

<pre><code class="language-css">/* ===== Banner module ===== */
.banner h3,
.banner p {
  margin: 0 0.5em;
}
.banner p {
  color: #222;
}
.banner h3 {
  color: #444;
}
</code></pre>

<p>Yes the above code is longer but maintainability is what we are aiming for. </p>

<h3 id="3avoidnamingclassesjustbytheircurrentproperties">3. Avoid naming classes just by their current properties</h3>

<p>Such as <code>.red</code>, because what is red? Will it always be red, what if we change to green... Do we go round everywhere changing the class name to green, or do we change the property and will red now mean green. <br>
Ok this one seems obvious especially with my example, but often when designing more modular css you can get carried away and go too abstract to the point of classes for everything like <code>.red, .font-serif, .text-align-right</code> which leads to even worse code. </p>

<p>Instead, try to think of what the element is, such as a block on the sidebar for latest news. you could have a module called <code>.latest-news</code>, but then you could go further and make that a generic <code>.sidebar-block</code> which can be reused for all your sidebar blocks. On top of this you could then still have <code>.latest-news</code> or <code>.sidebar-block-latest-news</code> for specific extra changes on top of the default <code>.sidebar-block</code>, but consider how this could be shared with other blocks on the page too. </p>

<h3 id="4dontjustslapimportantonit">4. Don't just slap !important on it ;)</h3>

<p>I find <code>!important</code> is very rarely needed, but too often designers try a selector, find it doesn't work and is somewhere overridden. So they give up and slap an <code>!important</code> on it. <br>
Learn about how selectors and inheritance works! <br>
<img src="https://blog.stapps.io/content/images/2016/06/css-specificity-meme.jpg" alt=""></p>

<p>The topic of CSS specificity deserves its own post, luckily there's several out there that do great jobs. <a href="https://web.archive.org/web/20140302025313/http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html">Specificity Wars</a> is a fun introduction to it, along with articles on <a href="https://css-tricks.com/specifics-on-css-specificity/">CSS-Tricks</a>, <a href="https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/">Smashing Magazine</a> and the <a href="https://developer.mozilla.org/en/docs/Web/CSS/Specificity">Mozilla Developer Network</a></p>

<p>Tools such as <a href="http://specificity.keegan.st/">Specificity Calculator</a> are also helpful for you to work out why a selector is more specific. </p>

<p>If you don't bother to learn about it, you'll be forever doomed to fight your forest fire of !importants later. </p>

<h3 id="5readthesourcecode">5. Read the source code!</h3>

<p>While you're working in systems or you have build scripts that generate a main/master css file from all your other files, don't be afraid to jump in and take a look to work out why an issue is happening or to check your code is being built the way you expect. <br>
Systems sometimes also have to place some css in page, maybe because of their current module architectures, whatever the reason, if that's the system you work within, you'd better be confident you can find where some css comes from. (Luckily dev tools will tell you if css is from a file or on the page). CSS like this deserves care and attention to override. The source code wont lie to you. </p>

<h3 id="6devtoolsisyourbestfriendknowitinsideout">6. Dev tools is your best friend, know it inside out!</h3>

<p>Better than reading the source code (though that's still important) is using dev tools. </p>

<p>You need to learn dev tools for the browser you're working on. No two ways about it, go learn them now, at least the inspector :). <br>
Here's a link for <a href="https://www.codeschool.com/courses/discover-devtools">Chrome</a> and <a href="https://developer.mozilla.org/en-US/docs/Tools">Firefox</a>. </p>

<p>The inspector will save you minutes, hours, days, weeks and more in trial and error. </p>

<h3 id="7avoidfixingheightwidthswhenyoucan">7. Avoid fixing height &amp; widths when you can.</h3>

<p>This often happens when you have a mockup in photoshop and then you try to match the exact dimensions, which is great, except that this likely wont work as the screen size decreases. <br>
Try to make your modules fluid, then they can fit into a grid or take the widths of the container. Patterns like this are core to responsive design. </p>

<h3 id="8designingfordesktopdownormobileup">8. Designing for desktop down or mobile up?</h3>

<p>Instead try to think of mobile and up where you can. Often this will lead to simpler css breakpoints and less cruft around content. With this you can think about what's really needed in the design, without too much extra that on mobile wouldn't make sense. <br>
Read more on <a href="http://zurb.com/word/mobile-first">Mobile First here</a>, or check out Luke Wroblewski's <a href="https://abookapart.com/products/mobile-first">Mobile First book</a></p>

<h3 id="9clearfixthecontainersofyourfloats">9. Clearfix the containers of your floats.</h3>

<p>The clearfix class is somewhat magical, but really it's very simple. There's a few implimentations of it, but generally the practice involves adding a hidden pseudo element at the bottom of the content that clears the floats. <br>
Sometimes you'll find this called ".clearfix", or ".cf". <br>
<a href="http://nicolasgallagher.com/micro-clearfix-hack/">Read more about one great example of this here</a>
<img src="https://blog.stapps.io/content/images/2016/06/css-floats-y-u-no-clear-yourself.jpg" alt=""></p>

<h3 id="10avoidmagic">10. Avoid magic.</h3>

<p>Maybe this seems weird when I just said clearfix is magic. But clearfix is simple when you understand how it works. Try to avoid code that seems to magically make things better if you don't understand why. Otherwise, how can you trust it will always work? How do you customize around it without breaking it? <br>
Clearfix is a great example of this as if you did not understand how it works, you may try and add a pseudo element after it in your own css and find the clearfix no longer works. </p>

<h3 id="11usetheappropriatemethodforlayout">11. Use the appropriate method for layout.</h3>

<p>Often grids or inside blocks instead of floats for layout. <br>
E.g. if you have 3 columns of evenly sized text next to each other, a grid is perfect. If you have a navigation list or boxes next to each other that are sized by their content, normal grids would be a terrible solution. Inline blocks are best for that as they are sized based on the content. If you have a title with a link to the right, float the link to the side, grids again are not really the best for that. There's no one way of doing things right, don't just follow one specific method, think about how it looks and what would really make the most sense. It may take a little longer to stop and think about each part. But if you do it will save you so many headaches later.</p>

<h3 id="12dontstyleeverythingasunique">12. Don't style every thing as unique.</h3>

<p>Try to reuse modules, again this is not to copy and paste, instead create a generic look for basic elements within content, and then create re-usable classes / modules for applying different styles. </p>

<h3 id="13optforrelativemeasurementsoverfixedwhenyoucan">13. Opt for relative measurements over fixed when you can.</h3>

<p>Simple one, relative measurements such as em, %, rem etc. are more flexible and will often work better with your responsive design than px. <br>
Not that you should cast aside px as a dirty relic, instead, consider the best measurement for the use case. Often in responsive design this will be relative measurements though. </p>

<h3 id="14remembernotallinteractionsrequirejquery">14. Remember not all interactions require jQuery.</h3>

<p><img src="https://blog.stapps.io/content/images/2016/06/css-can-do-that.jpg" alt=""></p>

<p><a href="https://developer.mozilla.org/en/docs/Web/CSS/:hover">:hover</a>, <a href="https://developer.mozilla.org/en/docs/Web/CSS/:active">:active</a>, <a href="https://developer.mozilla.org/en/docs/Web/CSS/:focus">:focus</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes">more</a>. Learn them, wield them like the awesome tools they are. </p>

<p>You probably already use these and know them well. But if you're working on something in javascript, it's easy to forget and use jquery or similar to do it instead.</p>

<h3 id="15combineproperties">15. Combine properties</h3>

<p>Properties such as <code>font</code> and <code>background</code> can be combined. It's worth using the combined versions for simplicity such as:</p>

<pre><code class="language-css">background-color: rebeccapurple;  
background-image: url(/images/logo.png);  
background-repeat: no-repeat;  
background-position: top left;  
</code></pre>

<p>Could be:  </p>

<pre><code class="language-css">background: rebeccapurple url(/images/logo.png) no-repeat top left;  
</code></pre>

<h3 id="16avoidunnecessarilylongselectors">16. Avoid unnecessarily long selectors</h3>

<p>Sure, referencing <code>body #content .sidebar a</code> works, but would <code>.sidebar a</code> work just as well?</p>

<h3 id="17alwaystestyourideas">17. Always test your ideas.</h3>

<p>Developer tools allow us to test our ideas live, why wait to compile or ftp a file to a server to test your idea for a fix. Change it live in the developer tools and you'll have instant feedback.</p>

<h3 id="18browsertesttoo">18. Browser test too!</h3>

<p>With complex modules, check ideas for them as you're going in browsers you think may be problematic, this will make things a lot easier when you return to fully browser check it later. </p>

<p><a href="https://blog.stapps.io/modern-browser-testing/">I've written more about browser testing here</a>.</p>

<p>You can also use <a href="http://caniuse.com/">Can I Use...</a> to find out browser support for new CSS properties etc. to find which browsers are likely problematic to start with. </p>

<p><img src="https://blog.stapps.io/content/images/2016/06/css-i-see-problem.jpg" alt=""></p>

<h3 id="19avoidspecificbrowserhacks">19. Avoid specific browser hacks.</h3>

<p>Try to find the cause of the problem, rather than a quick fix that may lead to further problems. You may find it's a problem in your CSS and you can work around a problem easier than just hacking it. <br>
If you have to use a <a href="http://www.browserhacks.com/">hack</a>, explain why in a comment, make sure that in a month when you return to this project you'll know why you added it in. </p>

<h3 id="20formatyourcssinaclearanduniformway">20. Format your CSS in a clear and uniform way.</h3>

<p>This one is a somewhat personal preference, but I try to always write CSS in the same way. I use 4 spaces for indentation (some people use tabs), I separate each selector onto a new line as I believe selectors are kind of the most important part of the css definition, and each property onto a new line.</p>

<p>You can see more of my style choices for CSS in the following post.</p>

<p><strong>Can you expand this list past 20? Leave a comment with your own anti patterns or advice.</strong></p>

<hr>

<p>Enough with what not to do...  </p>

<h3 id="continuetopart2forhowtostructureyourcssandmoretipscssstructureantipatternspart2"><a href="https://blog.stapps.io/css-structure-anti-patterns-part2">Continue to Part 2 for how to structure your CSS and more tips.</a></h3>

<hr>

<p>If you disagree with this list or do you have your own suggestions? Leave a comment below and I'll try to reply quickly.</p>]]></content:encoded></item></channel></rss>