Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Sunday, January 11, 2015

Andy's CSS Best Practices



While a lot of languages have pretty well defined sets of best practices, CSS seems to fluctuate on a monthly basis. Is the cascade good or bad this week? Am I trying to be modular, or trying to have a few selectors as possible? Are classes good or bad? If you search in Google for "CSS Best Practices", you'll numerous articles explaining why "X Method" is the "RIGHT WAY". Problem is, that a lot of times these different methods contradict. The most sane article I've read on the topic in a long time was by Chris Coyier over on CSS-Tricks, entitled, "CSS: Just Try and Do A Good Job". He brought up a lot of these issues, and reached the conclusion that as long as you are trying to keep things logical and easy to read, then you're on the right track. Don't be lazy, do what makes sense, etc. Inspired by this, I'd like to share my set of CSS Best Practices ... the method to my madness, if you will.

Use One Stylesheet


As tempting as it is to split up stylesheets, I try to avoid this for performance issues. In this, as with everything, there are other opinions, but I prefer to avoid multiple files being downloaded.

Attach Styles to Elements (Not Classes or IDs)


This is the main point. With the rise of HTML5, we have a lot more elements to play with. I like to attach my styles directly to the elements, and then use classes as modifiers. I try to avoid using IDs at all (for styles). Example:

<article>
  <h2>Header</h2>
  <p>Content goes here.</p>
  <p class="highlighted">Content goes here.</p>
</article>


h2 {styles here...}
span {styles here...}
span.highlight {modified styles}


My styles are attached to the h2 and span elements, while the class is used solely as a modifier. I never use classes by themselves, and I almost never use IDs. As with any rule, there are exceptions. The website logo, for example, I might identify with #logo, since it's unique on the entire site.

Nesting Is Okay


If I have a span inside an article that behaves in one way, plus a span in the footer which behaves differently, I'll use:

article span {styles here...}
footer span {styles here...}


Rather than give them classes. Many people say that we should try to avoid over specificity, and that nesting is entirely bad. I agree that we should try to minimize specificity, but we only have a limited number of elements to work with so there's going to be some. I do sometimes get around the element limit by "cheating though", which leads me to my next point:

Custom Elements (The "Bad" Way)


HTML5 gives us an official way to make custom elements. It's clunky because they have to have two words separated by a dash. Plus browser compatibility sucks. But here's the thing. As long as we are just talking container elements, I can name it anything I want to. I just have to make sure to define it with "display:block" in the CSS. I can have , or , or . This opens up everything, and the code starts to look Very nice. Plus it works all browsers. It's officially a bad idea, but so what? It works and it makes your HTML and CSS look really nice. Example:

<person>
  <h3>
    <display>John Smith</display>
    <edit></edit>
    <cancel></cancel>
  </h3>
  <span class="editable">
    <display>(555)-555-5555</display>
    edit></edit>
    cancel></cancel>
  </span>
  <span>
    display>johnsmith@email.com</display>
    edit></edit>
    <cancel></cancel>
  </span>
</person>


person {Styles here...}
person display {Styles here...}
person edit {Styles here...}
person cancel {Styles here...}


Person, Display, Edit, and Cancel are all fake elements. But in this example (taken from a web app I'm currently building) they allow for code which is much easier on the eyes. Take a look at the same structure without custom elements.

<div>
  <h3>
    <div>John Smith</div>
    <button class="edit"></button>
    <button class="cancel"></button>
  </h3>
  <span class="editable">
    <div>(555)-555-5555</div>
    <button class="edit"></button>
    <button class="cancel"></button>
  </span>
  <span>
    div>johnsmith@email.com</div>
    button class="edit"></button>
    button class="cancel"></button>
  </span>
</div>


div.person {Styles here...}
div.person h3 div {Styles here...}
div.person span div {Styles here...}
div.person button.edit {Styles here...}
div.person button.cancel {Styles here...}


The HTML is longer and we have to add another line of CSS, or add more classes.

Summary


As stated above, there is no one right answer here for how to structure your CSS. my method is to use custom elements, apply styles to elements, use classes as modifiers, and avoid IDs. What about you? Let me know in the comments what you think.

Friday, July 11, 2014

WordPress Plugin Update

A while back, I wrote about an idea for a plugin that I was going to try and write. It would add a folder system to WordPress's media capabilities. As I later explained, this was somewhat beyond my abilities at that point. I have since, however, written two separate plugins, which are currently up on the WordPress repository.

Admin Classic Borders


In WP 3.8, the admin backend of WordPress was completely redesigned. Taking queues from Windows 8, WP was made "cleaner", which means flatter and with less borders. Overall I think it looks good, but I like borders between items. I think borders make a site or app easier to use by cleanly dividing items.



To address what I perceive as shortcomings with the new design, I wrote some custom CSS to re-add borders, along with a few other things. Admin Classic Borders is simply a plugin wrapper for that CSS, along with a settings page to let users customize.



It's currently has a 5-star rating an almost 1000 downloads, which I think says quite a bit for my school of design thought. My second plugin, Featured Galleries, is a little more in-depth, so I'll discuss it in a separate post.

Monday, October 15, 2012

Dynamic CSS

So for a long while I've wanted to be able to dynamically update CSS. This is especially useful in creating a dynamic Wordpress theme framework. I've finally figured out a way to do so, and it's really simple. There are three steps.



Step 1:

First, rename your CSS file from '.css' to '.css.php'.

style.css

style.css.php



Step 2:

Next, (and this might be obvious), update the link in your HTML file from ...

... to ...




Step 3:

At the top of your CSS document, insert this:
<?php
    header("Content-type: text/css; charset: UTF-8");
?>



And now you can put in whatever CSS you want statically, plus insert PHP variables the same as you'd do with HTML.

Static:
body {
    background-color:rgb(200,230,255);
    margin:0;
    padding:0;
}

Semi-Static
body {
    background-color:<?php echo 'rgb(200,230,255)'; ?>;
    margin:0;
    padding:0;
}

Dynamic
body {
    background-color:<?php echo bg_color_light; ?>;
    margin:0;
    padding:0;
}
 

Friday, April 6, 2012

Responsive Desgn - More Thoughts

As detailed in my post a few days ago, I'm planning out my sites on a 4 layout setup, (mobile, narrow, normal, wide). I'm not sure that's enough though, and I'm debating splitting mobile into two versions, mobile narrow and mobile wide. Or mobile landscape and mobile portrait.

I starting thinking about this when I had to change up the way my header looked at the high end of mobile down to the low end, and I ended up using a media query inside the mobile css sheet to do it. Which leads me to another point, how to apply media queries.

The first method is to apply them inside a css sheet. You'd link one css sheet to the webpage, then inside that sheet, you'd specify all the styling. Example (inside the css stylesheet):

#wrapper {width:1000px;}

@media (max-width:1050px) {

#wrapper {width:100%;}

}

The advantage of doing this lies in the fact that you can write less code. Styling which doesn't change can be written just once, only things that change have to be re-written.

However, this can get overly complicated. Trying to find the source of a problem can involve digging through the entire sheet, finding the styling on an element in multiple places.

The alternative to this is just creating entirely separate style sheets. Create multiple css files, name them mobile.css, wide.css, etc, and use media queries to link them in the web page. Example (inside the webpage file):

<link rel="stylesheet" type="text/css" media="(min-width:0px) and (max-width:660px)" href="css/mobile.css"/>

<link rel="stylesheet" type="text/css" media="(min-width:661px) and (max-width:1020px)" href="css/narrow.css"/>

<link rel="stylesheet" type="text/css" media="(min-width:1021px) and (max-width:1401px)" href="css/normal.css"/>

<link rel="stylesheet" type="text/css" media="(min-width:1401px)" href="css/wide.css"/>

Besides the opposites advantage and disadvantage of the first method, this method has another advantage which is loading speed. When a page loads, it'll only load the CSS sheets that it needs. If one is on a widescreen monitor, it will load only the wide.css. This takes less time than loading all the css, for every possible screen size.

This is the method that I'm using at the moment. There is one problem though, which is that the most common script for giving IE7-8 the functionality of media queries doesn't work with the second method. I'm trying to find a hack around it, and I'll post it here if I figure it out.
 

Sunday, April 1, 2012

Things are Moving!

Any reader of this blog knows that I love web design. HTML, CSS, PHP, jQuery. Recently I've gotten really into mobile, and I'm trying to push myself with the actual graphic design part, though I'm still quite terrible in that aspect. Yet, what is the point in all this, besides it being fun?

I was approached by a good friend of mine a while back about starting a web design company, and turning my hobby into a part time job on the side. (A profitable one, since an average personal wordpress site goes for anywhere from $700 to $2000). Still, it's been slow going, weeks passing with really nothing happening. However, over the past week things have finally started moving.

The first surprisingly difficult hurdle was creating a name. Catchy, simple, and most importantly, not already taken. And if you think that there are lots of names out there not taken, try googling random stuff followed by the word design. We progressed through Blue Hat, to Blue Beard, to Cat Beard, and finally came up with out new name: CatStasche.

Picture, the face of a cat, with a prominent mustache.

That done, we registered the URL, www.catstachedesign.com, and I put up a coming soon page. Then work started on our actual site. It'll be powered by WordPress eventually, but right now we're working with static HTML and CSS just to get the layout working. One major aspect I'm putting a lot of effort into is responsive design, which is one of the few areas that I both A, understand well, and B, am getting in on the ground floor of.

I've decided on standardizing upon 4 widths, and making the design flexible within each of the four.

Mobile
0px - 600px
Smartphones, tablets in portrait mode
Narrow
600px - 1000px
Old monitors, and tablets in landscape mode
Normal
1000px - 1400px
Traditional 1024x768, and very common 1280x1024.
Wide
1400px - Infinity
Widescreen large monitors, normally 22" and above


Some sites have the region that I have as mobile split again, for phones in landscape vs portrait, but all my mobile designs are going to be 100% flexible, using no fixed-length values, so it'll scale, and make another split unnecessary. I've got the layout tentatively working for Wide and Normal, and will be working on the Narrow and Mobile versions over the next few days while my partner finishes the logo and starts working on the color scheme. Hopefully the site should be completely designed within the next week, so I can then spend the week after moving translating it to WordPress. And then we'll really be ready to rock.  

Wednesday, March 14, 2012

Mobile Thumbnail Gallery

A new site I'm starting work on is a personal website with the primary purpose of showing off the owner's artwork. She didn't indicate a desire for the website to work on mobile devices, but given that mobile is what I'm focusing heavily on right now, I'm going to create and offer that functionality just in case.

The main design of the gallery page will consist of two pieces. The first is a full gallery. This will be squares, 3 across the most narrow dimension, height or width depending on orientation. The key will be making them square, which will require javascript, because despite my CSS height proposal, no changes are likely to occur in the near future.

So, javascript will be used to set the height of the square thumbnail links. What about the images though? Will all be square? Probably not. And do we want to resize ahead of time so that the images loaded are all small thumbnails? It will require extra steps beforehand, but it will pay off in page-load time. Loading the entire image and jut resizing will work, but it will load much more slowly.

Right now my squares are just black, I'm trying to find some images to use for the moment in place of the real ones we don't have yet. We'll see what happens though.
 

Friday, February 24, 2012

CSS3 Proposal - Width/Height % Behavior Change

Under current CSS rules, specifying width or height (or their respective min-'s) by percentage is relatively easy, yet it's unnecessarily limited. I say this because when you specify a width using a percentage, you only have control over one of the three things you are specifying.

Example:

<div id="container">
    <div id="content">
        Content
    </div>
</div>

#container
{
    width:500px;
    height:200px;
}
#content
{
    width:100%;
    height:100px;
}

Content is inside Container, and Container is Content's parent. Content has a width of 100%. What exactly does that mean though? It means that Content's width should be equal to 100% of it's parent's width. That is three separate values, but under current CSS rules, we only can change the first. The second is locked into Parent, and the third is locked into being the same as the value being specified.

Imagine for a moment if the user wanted to make a flexible grid of squares, for a mobile phone. A container, set to 100% width of the screen, and then inside of it, three squares, each 28%, with margin-left of 4%.

<div id="container">
    <div id="content">Content</div>
    <div id="content">Content</div>
    <div id="content">Content</div>
</div>

#container
{
    width:400px;
    height:200px;
}
#content
{
    width:28%;
    height:28%;
    margin-left:4%;
    float:left;
}

Square
Square
Square

Height and width are both set to 28%. But the panels aren't square. This is because you want both width and height to be 28% of the parent's width, and right now height is set to 28% of the parent's height. Looking around, there is no way to change this as of now, so here is my proposal.

Height: valueof location value

Ex: Height: 28% auto width;

  • The first of the three values would be exactly the same as it is now, a percent.

  • The second would be location. It could be any class or id, or auto, which would revert to the parent container. Additionally leaving it blank would revert to auto, to parent container.

  • The third value would be what value to grab from the location. Currently, by default it is set by the value that you are changing. Width = width, height = height. But you'd be able to put in any other number based css value instead.

  • An added benefit is full backwards compatibility. Old browsers would ignore the second two values, and browsers that support the new rule will set any blanks to auto, so all old code will function exactly as it does today.

    I'm not sure how one goes about submitting a new rule proposal to be added to CSS, so if anyone knows, please comment and let me know. Also, if you have concerns or suggestions about the proposal, please let me know as well.
     

    Saturday, February 11, 2012

    Flexible Website Layout Tricks

    In the past week I've spent quite a bit of time playing around with flexible layouts. Or to be more accurate, layouts with fixed elements on the edges a flexible center. Sites like Wikipedia utilize this category of layout very successfully, and I've been trying to figure out how it's done.

    First, a review. Websites in general are built around containers. Containers within containers, each one's attributes can be control, from dimensions on the page to the way text inside looks. For the layout of the page, we are only concerned with three attributes. Size (height, width), extra spacing (margin, padding), and borders. CSS allows height and width to be set both as fixed lengths and as percentages of the parent container. The problem though, is that one can't use a combination.

    Say you have a header, it needs to be exactly 50px in height. Then you have a content box, that should take up the rest of the page. Ideally, you'd tell the content box to by 100% minus 50px. But you can't combine % and px. They best way around this is not to set height at all. You instead use positioning.

    position:absolute;
    top:50px;
    bottom:0px;

    And suddenly, you have it going all the way to the bottom. But what happens if your content is longer than your page? Doesn't work anymore. In that case, you put another div inside the positioned one, and add your styling to it. Don't give it positioning information. Without it, it will just conform to the content inside it, and will apply the styling. But when the content is less than page length, the positioning of the main outside one will take precedence.

    Another thing I've figured out recently is how to set inside borders. Borders, along with padding and margin, are set "in addition" to height. If you have a box that is 100px wide, with a 1px thick border around it, it's actually 102px wide total. If you set a box to be 100% wide, it will take up 100% of the screen. But you add a 1px border, and suddenly, it's exactly 2px wider than the screen, no matter how wide the screen is. There is a way around this though. Take the box below as an example.

    <div style="height:100px; width:100px; text-align:center; background-color:lightblue;">
        Test
    </div>

    Test

    Now, add a new div container, inside the one you wish to add a border to. Make it 1px wide, 100% tall, background-color whatever you want to the border color to be, and display:block, so that it displays with no text in it. Finally, set it to float to the left.

    <div style="height:100px; width:100px; text-align:center; background-color:lightblue;">
        <div style="height:100%; width:1px; display:block; float:left; background-color:black;"></div>
        Test
    </div>

    Test

    Now, add the same div, still inside the container, still before the text, and change it to float right instead of left.

    <div style="height:100px; width:100px; text-align:center; background-color:lightblue;">
        <div style="height:100%; width:1px; display:block; float:left; background-color:black;"></div>
        Test
        <div style="height:100%; width:1px; display:block; float:right; background-color:black;"></div>
    </div>

    Test

    And you have a border inside. In a real website of course, you'd place that styling in a css style sheet, and just give the border divs a class of something like leftborder, and rightborder. And it does make the HTML code look somewhat bulky, admittedly, but it's the best way I could find.

    The same thing can be accomplished with padding, by just making the width thicker, and the background-color transparent. Margin I haven't figured out yet, but I'm working on it.

    Know a better way? Leave a comment!
     

    Sunday, February 5, 2012

    Flexible Layout Problems

    As I said yesterday, I'm working on a new project for CESAC. It will automate our date auction nomination process, letting students enter their nominations via a laptop, instead of a sheet of paper.

    Yesterday I drew some logic chains to figure out what all I'd need to do, if I wanted to do it right and include proper validation. I then started working on it, and immediately got stuck on the design of the submission page.

    The design is fixed width at 1000px, but I wanted it to be flexible vertically, so that it always fills the page up and down, but without a scroll bar. The problem is that the bottom section is comprised of 4 small images, all of which are different heights. Having them change in height would be a mess.

    So I decided, they'd be fixed. The top picture though, would change. It would need to be 100% of the height of the page, minus 384px of the bottom section. Then slap in the CSS3 border-size:cover; style, and you've got a winner.

    Simple no? No. Problems abound. First, there's no easy way to specify a percentage minus pixels. A gross oversite of CSS I believe. Second, any solution is only going to work in modern browsers. Firefox 4+, IE9+, Opera, Chrome. The big problems being IE7 and IE8, which are still very widespread. If you want to do it though, without javascript though, you'll have to abandon the older browsers.

    As off CSS3, if you specify "height:100%-384px;", along with "top:0; bottom:384px;", if works. Having "top" before "bottom" fixes the position from the top, and having "bottom:384px;" does the same thing as a "margin-bottom:384px;". But you need all 3 of those, along with "position:absolute", or it won't work. Seems repetitive, I know.

    Anyway, while the PHP still hasn't been created (my plans for today until the Super Bowl tonight), the design is finalize .. I think. Check it out below. If if goes longer than your page, causes scroll bars, please leave a comment and let me know what browser you are using. (Unless it's IE7 or 8, in which case do a better job reading).

    Nomination Page

    PS. Thoughts on the submit button? I designed the the images myself with Photoshop.