RSS Feed

‘css’ Category

  1. CSS3 Box Sizing

    January 8, 2012 by Austin Hallock

    CSS3 let’s you do a lot of cool stuff, most of which add graphical enhancements like gradients and rounded corners, all of which are great. Lately however, I think my favorite new property is box-sizing.

    There are two modes: content-box and border-box. content-box is default, and will calculate the true width for an element as width+padding+border. So if you have a child <div> with 100% width and 10px of padding, it will extend outside the parent <div>. Here’s an example of content-box:

    The parent<div> has a black background and the child has a red background with 50% opacity (rgba is another awesome new tool to use with css)

    With border-box, the padding and border are included in the width of the element. So an element with width: 300px; and padding: 10px; will still have a width of 300px, rather than 320px. Here’s the example from above using border-box:

    This allows for more fluid specification of widths in a lot of instances. Say you need a textarea with width: 100%, you can add padding to it and not have to worry about it extending to be wider than what you really want. Before, I always had to set a fixed width, which obviously isn’t ideal when developing a site you want to work in a variety of resolutions and devices (mobile).

    The box-sizing property doesn’t work in IE 7, but you can get around that with this.

    Here’s how you implement it:

    box-sizing: border-box;
    /* Firefox */
    -moz-box-sizing: border-box;
    /* Webkit */
    -webkit-box-sizing: border-box;
    /* IE */
    -ms-box-sizing: border-box;
    /* Opera */
    -o-box-sizing: border-box;
    

    Hope this helps you out like it has helped me!


  2. Overlay a Lightbox on a Page

    March 20, 2010 by Austin Hallock

    Here’s a cool script I just made that allows you to overlay a lightbox with whatever you want in it on any page (Google, this site, etc)

    Drag the link below into your bookmarks

    and click it on any site you want to cloak / put a “light box” (an overlaying box) on. When you click the bookmarklet it’ll ask what you want to show in the box, just type that, hit save and you’re done.

    Overlay

    YO

    Here’s an example: Google
    Tweet this

    Hope you like it and let me know how I can improve it!


  3. Site Design: CSS Positioning

    March 6, 2010 by Austin Hallock

    With CSS, one of the big things you’ll need to do is position your elements. You may have used something like <table width=’100′ height=’100′> in the past, problem with that is it’s not valid. I’m trying to teach people valid code ;)

    If you don’t understand anything in this article, either leave a comment with a question, or refer to the previous post (the one before my apartment vid). This ended up being pretty lengthy, so grab a glass of water and get comfortable 8-)

    The best element to use for positioning/padding/margins etc is the <div> tag. Next easiest is the <p>, and tags like <span>’s and <a>’s can be moved around and have the height changed with a bit of extra work.

    We’ll start with what you do for <div>’s and <p>’s:

    Width and Height:

    Pretty self-explanatory — You can use pixels, percentages and it looks like centimeters (never used that)
    CSS:

    .pixels
    {
    	width: 400px;
    	height: 400px;
    }
    .percentages
    {
    	width: 70%;
    	height: 50%; /*Though % heights don't always work as expected */
    }
    

    Examples

    <div style='width: 400px; height: 300px;'>Test</div>
    

    Tips
    In general, most people have a resolution of at least 1024px by 768px. This means you probably don’t want a width greater than about 950px.

    Padding

    Padding is just that, it’s extra space that pads the outside of your element, meaning more space between the edge and your content. You can use “padding:” or “padding-left”, “padding-top”, etc.

    CSS

    .all_around
    {
    	padding: 20px;
    }
    .left_right
    {
    	padding: 20px 0;
    }
    .top_right
    {
    	padding: 20px 20px 0 0;
    }
    .left_pad
    {
    	padding-left: 20px;
    }
    

    Examples

    <style type='text/css'>
    .mydiv
    {
    	padding: 2px 10px;
    }
    </style>
    <div class='mydiv'>Test</div>
    

    Tips
    If you just enter one value, ie the all_around class above, it will pad all four sides.
    If you specify two numbers like the left_right class, it will pad the top and bottom with the first number, and left and right with the second.
    If you specify all four numbers like the top_right class, it will put the padding in clockwise order. So top, right, bottom, left.

    Margin

    Margin is very similar to padding. The difference is margin is the amount of space between each element, while padding is extra space inside the element. This makes a big difference when you start adding background colors and borders. Like padding, you can use “margin”, “margin-right”, “margin-bottom”, etc

    CSS

    .all_around
    {
    	margin: 20px;
    }
    .left_right
    {
    	margin: 20px 0;
    }
    .top_right
    {
    	margin: 20px 20px 0 0;
    }
    .left_pad
    {
    	margin-left: 20px;
    }
    

    Examples

    <style type='text/css'>
    .mydiv
    {
    	margin: 10px 0;
    }
    </style>
    <div class='mydiv'>Test</div>
    

    Tips
    The order of top, right, left, bottom is the same for this. And again, use padding when you have a background color for your element and you want some space so the text isn’t smushed up against the edge, use margin when you want space between elements.

    Centering an element

    If you want to center an element in the page, most people probably use <center>. Again, not really valid. So what I do is something like this:

    <style type='text/css'>
    .container
    {
    width: 800px;
    margin: 0 auto;
    }
    </style>
    <div class='container'>Whatever</div>
    

    Aligning your element in the exact center of the page (what’s above just centers horizontally), you want to do something like this or use some javascript.

    Just a random tip that I can’t really write a full blog post for, there’s an order to which elements can be inside which. Basically, don’t put a <div>, <p>, <h1> (h2, h3 etc) inside of a <span> or <a>

    Well that was a lot of text, hopefully you’ll find some use out of some of it. Funny thing about me doing these tutorials/posts on CSS is CSS was the last thing I became fluent in, learned HTML -> PHP -> Javascript -> CSS.

    Next up is more advanced positioning/padding for stuff like links and spans.


  4. Site Design: The Basics

    February 24, 2010 by Austin Hallock

    So, to start, I’m going to say that this all might be confusing, if you need help with something, post it in the comments and I’ll get things worked out for you.

    When coding, I don’t use tables or tags like <font> (which most people tend to use). Why? Because the code can get really jumbled with that (you should see some of the stuff I have to deal with working on ILoveHits).

    Not only that, but all the <font>, border=”0″, a ton of stuff that can be done with CSS is considered deprecated.

    But most importantly in the new version of HTML (HTML5), the following WONT WORK:

    With that said, I generally use <div>’s, <span>’s, <ul>’s and <p>’s the most, and tack on a class=”whatever” so it looks like <div class=”classname”>Content</div>

    • <div>‘s are essentially just blocks, or squares, in your webpage.
    • <span>‘s are kind of like <div>’s but they don’t cause a line break.
    • <ul>‘s are unordered list, so essentially, when used with <li>’s you can create bullets
    • <p>‘s are a paragraph…basically like a <div> but it adds some margin to the top and bottom.

      is the same as <div>

    You have a few options for CSS…One you can have an external .css file and link to it with:

    <link type='text/css' href='mycssfile.css' rel='Stylesheet' />
    

    Or…You can have it internally in your .html file with:

    <style type='text/css'>
    	CSS goes here
    </style>
    

    And finally you can specify it for certain tags with style=’whatever’ so:

    <div style='font-size:10px;'></div>
    

    With the first two options, you can use 3 different things to change how your page looks.

    The first is using the element type, so if I want all <div>’s to look the same way I would do:

    div
    {
    	font-size: 14px;
    	background-color: #111111;
    	color: #ffffff;
    }
    

    The second option is to style certain classes (where you define <div class=’whatever’> To do this, you put a period before the class name in the css:

    .whatever
    {
    	color: #333333;
    }
    

    Finally, you can style based on id‘s. While classes can be used on multiple elements, id’s can only be used on 1 element. To style something like <div id=’whatever’> you would use a octothorpe (or for regular people, a pound sign):

    #whatever
    {
    	font-size: 30px;
    }
    

    You’ll notice I use stuff like #333333 these are hex codes that represent specific colors, you’ve probably seen them before. To find which color is associate with which hex code, you can use a tool like this (also, feel free to take a look at the source of that to get a further look at CSS and HTML).

    Let’s start off with some of the more basic CSS uses. Since you wont be able to use <font color=’green’> in HTML5, you’ll have to use CSS like below.

    Fonts

    Sure, Times New Roman does just fine for writing a letter, or paper, but when it comes to web design, 99% of the time you want to use a sans-serif font. What does sans-serif mean? Well, no serifs, hard for me to explain but this link should do the trick. The font faces I generally use are Helvetica Neue, Helvetica, Verdana, Lucida Grande and sometimes Arial.

    So how do you change your font? That’s easy … This series involves the use of CSS, simply because it’s what’s considered valid rather than <font _____>Text</font> (which is what most people tend to use). You can put it in any of the forms I have above.

    CSS:

    font-family: 'Helvetica Neue', helvetica, verdana, arial;
    

    The reason I have 4 listed, is if the person viewing your page doesn’t have the first font installed, it goes to the 2nd, then the 3rd etc.

    As for font sizes and colors, you put those in the same way you do the font face/family. The code for these are:

    font-size: 14px;
    

    and

    color: #111111;
    

    To pick a color

    To summarize, what I would do for fonts is make a class by doing:

    <style type='text/css'>
    .big_green_font
    {
    	font-size: 40px;
    	font-color: green;
    	font-family: helvetica, verdana, arial;
    }
    </style>
    

    Then, in my code I would use something like <span class=’big_green_font’>My text</span> to have big, green font.

    Background Colors

    Generally, you’ll want to associate a background color with a >div< (block/box) or <p>. To do this, you’ll use the CSS:

    background-color: #111111;
    

    So create a class:

    <style type='text/css'>
    .black_box
    {
    	background-color: #000000;
    	color: #ffffff;
    	width: 500px;
    	height: 200px;
    }
    </style>
    

    Then make your div: <div class=’black_box’>My content<>

    With each post in this series I’ll get a bit more advanced, so keep checking back! The next post will probably deal with dimensions, padding and margins. Again, if you have any questions, post them below.