Showing posts with label Div. Show all posts
Showing posts with label Div. Show all posts

Saturday, December 31, 2011

Building a Site With CSS3

Introduction

This post will go over my naive implementation of a style sheet to build the "Chicken Sandwich" page, as it is currently implemented.  As mentioned in the previous post, this site functions well under all the major browsers (IE9, Firefox, Chrome, Safari).  IE9 currently doesn't support the gradient in the background (to my knowledge) so It uses a fallback of a solid color background that is somewhere between the two values in the start/end gradient you see in the other browsers.

There are many ways to style your web page elements.   Step one is to identify the element you want to style.  This can be done by referencing either the element as it's known by the DOM, the element's class, or it's id.

So let's take a look at the "div" element <div> in HTML.
<div class="divider" id="divStyle"> This is my Divider</div>
If I want to identify this object in my CSS file, I can do that four  ways. (to my knowledge)

div{} - assigns default style to all elements of this type.

div.divider{} - assigns style to all div elements with the class "divider" set by <div class="divider">

.divider{} - assigns the style to all elements with the class set to "divider" set by <element class="divider"> This could be anything defined by the user. For instance:

.hamburger{} -- <element class="hamburger">

#divStyle{} - assigns style to the element who's id is "divStyle". Set by <div id="divStyle">

Most of the time the id for an element is going to be unique and this method should be applied to elements who have their own unique style, not for general style settings.   A good design rule would be to stylize your elements through their class, and only use the id reference for edge cases where you need a one-off style for a unique element or element that derives from a class of elements.

So let's get started.   

The first thing everyone wants to know is, "how do I position elements on my page in an aesthetically pleasing way?"  

 Well, first you have to have an aesthetically pleasing design, which we can go over at another time, but for now let's assume that you have a design in mind.  

You'll want to put it together in a program similar to Photoshop, or at the very least sketch it out on paper.  Designing in an graphical program is benificial because you have the advantage of getting measurements and color data from the images you create.  Also, you'll want to create some images yourself anyhow, and not rely solely on clip art and images you find off the web. 
That in mind, you don't want your website to be too image heavy because these types of graphics take longer to load than elements generated through CSS and script (sometimes).


Containers

All web pages should have a main container.  This container can be any element that wraps around the content and establishes the main dimensions of the page.
In this example, we'll be using a <div>.
Dividers are awesome with the power of CSS.  Basically everything that used to be done using tables can now be done way easier and cleaner with a <div> element.
We'll start by opening up our basic HTML and adding a <div> in the body of the page.
<body>
                 <div class="container" id="master">
                 </div>
</body>
Notice the class is "container" and the id is "master"
Now we need to add the CSS block for this container to our stylesheet.
In this particular instance it's ok to reference it by the ID, because there will be only one "master" div for our template, but there could be many "container" divs.
This should be what your stylesheet looks like at this point:
html{
background:#e00000;
height:100%;
background:-moz-linear-gradient(top,#fe471d,#e00000) no-repeat #e00000;
background:-webkit-gradient(linear,left top, left bottom, from(#fe471d), to(#e00000)) no-repeat #e00000;
font-family: "helvetica", Arial, sans-serif;
color:#000;
}

body{
margin:auto;
height:100%;
}

#master{
}
Open it up in your browser and take a look.
Pretty unremarkable, I know... but hey, it's a start. 

Notice the div isn't visible.  This is because we have both, no content, and no style.

Type some text between the <div> tags, and reload the page. 

You'll notice the content shows up just like it would if you had just put it in the body tag.  That's because elements like the <div> element take on the properties of the element they are a child of.  So if you have a style set for the <body> unless the element the content is contained within has its own stylesheet data, the content will at like it's parent.  This makes sense, how else would the element get formatting information if not from the element it is contained within?

So let's add some style to the divider!
#master{
width:900px;
min-height:85%;
margin:auto;
padding:20px;
background:#ffffff;
}
Now take a look.


We've told the master div element to take on the following properties:

Width of 900 pixels.  This makes sure the content is contained within the standard visible space of a page, for most devices.  Anything larger and you run the risk of the page looking cramped on some devices.

Minimum height of 85% of the body, so that it doesn't go all the way to the bottom of the screen, but it will not be too high on the screen in cases where we have very little content.

Automatic margin is basically saying, "center my business to the parent" in this case, the parent is the body, which is basically the document.  In some cases you may have more than one body, and this would change.

padding of 20 pixels will make sure that all the content of the page will be 20 pixels away from the edge of the element.

And finally, the background is white.

Moving Our  <DIV>

I don't care for having the div element at the top of the page, so let's move it down a bit.  This is simple enough to do, all we have to add to our css is the following line:
top: 20px;
This will move the div element down 20 pixels.

oh, but wait, It won't!

because out element's position attribute is not defined.

there are three types of positioning:
fixed - absolute - and relative
for this example, we will set the div element position to relative by adding the following line to our CSS:
position:relative;
Easy enough, right?  reload the page and see what happens.  If it doesn't work, make sure your css looks like so:
#master{
background:#ffffff;
position:relative;
width:900px;
min-height:85%;
padding:20px;
margin:auto;
top:20px;
}
None of these attributes require a specific order, but some do.  It depends on what you're trying to achieve.  This is particularly true in the case of some vender specific attributes, like the ones mentioned earlier that require the prefix -moz- and -webkit-.  Usually this only matters in IE because IE is awful.

Adding a Border

Adding a border is easy to do, and should be done with minimalism in mind.  Less is always more.  In this case, I'll add a solid black border of 2 pixels.
Add the following line to the #master css.
border:2px solid #000;
What?  It's boring?  Ok, fine, let's add borders for the top, left, right and bottom of different thicknesses.
border-top:1px solid #000;
border-left:1px solid #000;
border-right:3px solid #000;
border-bottom: 5px solid #000;
Looks a bit better yeah?  Well, how bout a shadow?  That's right, the latest iteration of CSS provides you with the ability to add shadows to your situations.  And it's easy, and supported in nearly all browsers.
Try this:
box-shadow: 20px 20px 15px rgba(0,0,0,0.5);
This will add a boxed shadow on the element that is offset on x 20 pixels, on y 20 pixels, blurred 15 pixels, and with the color of black.  The rgba color format also allows you to specify an opacity, which is 0.5 (or 50%) in this example.

After you see the effect you can tweak it.  But here it is in case you don't feel like writing all this code.


Adding Layered Effects

Now let's add a div within this div that will float underneath and serve as a "menu" or something.
replace your text with another <div> element.
<div class="menu" id="leftMenu">
Copy and paste your #master css block and change the #master portion as follows:
#master --> .menu
and change the dimensions and what-not to be what you'd like.  I changed them to 200px and 200px to start with.  Remove the formatting of the margin as well, this will push the div to the far left of the parent (left justified being the default of the browsers).  I also changed the padding to 5px.  My css now looks like this:

#master{

                background:#ffffff;
                position:relative;
                width:900px;
                min-height:85%;
                padding:20px;
                margin:auto;
                top:20px;
                border-top:1px solid #000;
                border-left:1px solid #000;
                border-right:3px solid #000;
                border-bottom: 5px solid #000;
                box-shadow: 20px 20px 15px rgba(0,0,0,0.5);

}

.menu{

                background:#cccccc;
                position:relative;
                width:200px;
                min-height:200px;
                padding:5px;
                top:20px;
                border-top:1px solid #000;
                border-left:1px solid #000;
                border-right:3px solid #000;
                border-bottom: 5px solid #000;
                box-shadow: 20px 20px 15px rgba(0,0,0,0.5);

}

Save and reload your page, and it will now look like this:


Blowing Your Mind

This next part may blow your mind, so I suggest you be setting down.  If you're not already, we need to have a different discussion...
We now want to position this div offset from the main body.
Let's do that by adding the following line to the css of the menu block.
left:-200px;
It's that easy...
Z-Index
To change the depth of your elements, we need to add formatting for the "z-index".  Z-index is commonly used to describe the location of objects in the "depth buffer" for most renderers and APIs.  Z refers to an axis.  And when thinking about the elements in your document you should think of them in terms of how they relate to 3d space.  x being left to right, y being up and down, and z being forward and back. 
Add z-index information for both the menu and the master dividers.
#master{
      z-index:20;
}

#menu{
      z-index:19;
Notice the z-index isn't 1 and 2.  This is because we may want to add background elements, and if we make them 0 and 1, it will make it more difficult to go back and change our indexes once we have more blocks of css.

OH BUT WAIT!!!

This isn't going to work!  Because the div doesn't have anything to sort against.  The z-index of an object is always inherited from its parent.  Starting to see a pattern?  So before we can affect the sorting of these elements relative to one another, we need to pull the formatting from the master div and create a "content" div to put within it.  This makes sense for several reasons.  We don't want to rely on our master div for any visual elements, but rather to use it as the container.  Visualizing it initially is good for debugging purposes, and with the copy paste technology, we haven't wasted any work.  SO let's create our content div css block by copying the master div block.  Then add a <div> for content in the master div...

So, this is what our css looks like after making the appropriate changes.  This included:
·         removing padding from master, since there was no content in it.
·         copying and pasting master and changing the #master --> .content
·         removing all visual formatting from the #master css.

#master{


                position:relative;
                width:900px;
                min-height:100%;
                padding:0px;
                margin:auto;
                top:20px;


}

.menu{

                background:#cccccc;
                position:relative;
                width:200px;
                min-height:200px;
                padding:20px;
                top:20px;
                left:-200px;
                border-top:1px solid #000;
                border-left:1px solid #000;
                border-right:3px solid #000;
                border-bottom: 5px solid #000;
                box-shadow: 20px 20px 15px rgba(0,0,0,0.5);
                z-index:19;

}

.content{

                background:#ffffff;
                position:relative;
                width:900px;
                min-height:85%;
                padding:20px;
                top:0px;
                left:0px;
                border-top:1px solid #000;
                border-left:1px solid #000;
                border-right:3px solid #000;
                border-bottom: 5px solid #000;
                box-shadow: 20px 20px 15px rgba(0,0,0,0.5);
                z-index:20;

}

And this is what our document looks like now...



Not exactly what we had in mind?  Hrmm... ok... 

That's because our positioning isn't right for our "content" divider.  Changing the "position:relative" to "position:absolute" will fix this problem.  

The definitions of the 3 different positions will be explained later on (as i get a better understanding of them) but for now, just do it, and you'll see what I mean.

I've read a few definitions of the three positioning rules, but haven't fully wrapped my head around it in a way that will allow me to break it down one time (so to speak.)

So here's what you've got after all that jazz!


With these techniques, I've created this page:


I'll do more complex and aesthetically pleasing stuff later, but for now, this will do.

Hope you've enjoyed the read, and if there are ways I can improve it, or any errors you can spot, please let me know by adding a comment or sending me message!

Cheers!

Friday, December 30, 2011

Add Some Style(sheets)

Congrats if you made it this far, you've successfully created an HTML document that looks like dog balls.  We all know you're creative and want to add a little bit of your own personality to this business.  So let's get started learning CSS3, which is essentially the way we'll change nearly every single visual element of our web game and page containing it. 

CSS3 is wicked powerful and is capable of doing animations and all kinds of stuff.

We're going to add a background gradient... and then some more pretty shit... so let's do this.

We'll start by looking at the thing we're trying to create:

CSS3 Gradient Tutorial :

And yes, I'm aware that it still looks like dog balls.  But there is most definately a gradient on this page.  So how did we do this?

<Style> Tag

The style tag is used to define (inside an html file) the style of the page.  For this part of the tutorial, we'll use this method.  So the first thing you'll need is to add <style></style> tags to your existing document (from the previous post, or whatever html document you're adding style to).  This should make your document look like:
<!DOCTYPE HTML> <html>
     <head>
         <title>
            Title of my project: A learning experience.
         </title>
         <style>

         </style>
     </head>
     <body>
           ANYTHING!!!
     </body>
</html>
Now everything that you put between the <style> and </style> tag will be interpreted as CSS (cascading style sheet) data.  You can google, "why they call it cascading style sheet" if you want, but it's not terribly interesting, and doesn't really provide you with any more information than the name itself does.

Changing Appearance of Page Elements

You can change nearly every visual aspect of your page with CSS.  The only thing required is to know the "class" of the element as it is specified in the markup, script, or standard elements available through the DOM. (I think that's right).  So basically if you want to change the background of the body of a page, you can do that.  If you want to change the background of a paragraph you can do that.  If you want to change the... you can do that.

But how?

Simply tell the CSS which element you want to change and add {} after it like so:

body{}
Now, everything between the {} will be interpreted as CSS for that specific element.

There are all kinds of things you can change about the element, but for this example we will stick to the background: attribute.

body{
     background: your-style-goes-here ;
}
And "your-Style-Goes-here"; is not proper syntax for anything (duh).  But there's some things to explain about syntax for CSS stuff.  CSS is one of those things that isn't standardize across browsers yet.  This is because the different browser venders haven't been able to agree on what the best way to do it would be (I think).  But as soon as they do, this shit's all going to change.  Personally, I'd think it would be pretty easy to get the shit standardized, but whatever, we'll talk about that another day.

The point is, when dealing with CSS3 you'll want to make sure you're putting CSS code in your style sheet to handle all the browsers you plan on supporting with your site, or at the very least a nice fallback for all browsers you don't want to worry about.  

In the case of gradients, they are a relatively new addition to CSS and you basically have to do it twice for that reason... but luckily only once.  When we're done with this tutorial you'll be able to make one style sheet for all your pages.  This is how templates are made.

The Gradients
-moz-linear-gradient
-webkit-gradient

I'm sure there's different ways to do it in Opera and IE but fuck those browsers... they can use the fallback of a solid color. (basically I googled it, and it was way to difficult to explain and wasn't even CLOSE to the same way it's achieved in the cool browsers.)

-moz- basically says "Hey, CSS, this is a Mozilla specific joint, ignore for other browsers, and the same for -webkit- which is basically Safari and Chrome.

You're going to see this alot (until they all get their shit together)... these are referred to as different things, but mainly as vender specific markups (at least in my readings).

So now for the actual gradient.  We'll do moz first, since It's my favourite browser.

-moz-linear-gradient(top,#ccc,#000);

The above line basically says, "hey mozilla, make a linear gradiant starting at the top and with the color of this crappy grey and go to black".

Sounds simple enough.

Then there's -webkit-

-webkit-gradient(linear,left top, left bottom, from(#ccc), to(#000));

This basically says the same thing, but says it like this... 

"Yo, uhm... ok... look webkit browser... this is what I want you to do... i think.. Uhmm.. yeah.. so let's make a gradient.  Ok... Uhh... linear.. yeah.. linear.  And should we start it at the left corner?  Sure... ok... left corner.  Oh fuck.. that's right.. left top corner.  Ok.. now uhhh.. go to the bottom left corner.  Wait, what?...  whatever... ok... let's make it start at a shitty grey... and go to ... black.   Ok, cool.. yeah I think that'll do.. Thanks webkit browser!"

There's actually good reason the webkit version is more verbose.  It's not easy to read, because it doesn't make sense to people, but it makes sense to the computer.  This method of doing the gradient leaves room for scalability of the feature, as well as gives a more literal representation to the browser. (This is all assumption at this point, and should be interpreted as nothing more than bullshit... and you should probably have skipped to the next section by now).

So what does it look like now?

Still looks like dog balls huh?  Hrmm... well let's look at our document.

<!DOCTYPE HTML> <html>
     <head>
         <title>
            Title of my project: A learning experience.
         </title>
         <style>

                body{
                      background:-moz-linear-gradient(top,#ccc,#000) ;
                      background:-webkit-gradient(linear,left top, left bottom, from(#ccc), to(#000));

                }

         </style>
     </head>
     <body>
           ANYTHING!!!
     </body>
</html>

Well, by now, you're noticing that the gradient is stopping after your content and repeating forever many times down the length of your document.  This sucks.  So to fix this you need to add "no-repeat" to your style.  This is because your computer and the internet will assume nothing about the instructions you give it.  This is the most valuable lesson I've learned... The computer will ASSUME NOTHING!... 

so go ahead and change the gradient lines to include "no-repeat" after the last ) and before the ;
background:-moz-linear-gradient(top,#ccc,#000) no-repeat;
background:-webkit-gradient(linear,left top, left bottom, from(#ccc), to(#000)) no-repeat;

luckily, they've agreed on this part.  

But it still looks terrible and is only covering the content...

That's because it's important to remember that the body of your page is only as long as the content within it.  And in order to make sure your background fills the page, you need to give the thing that's wrapping around the entire document the style declarations.  This wrapper would be the <html> tag.  

So change body to be html.

<!DOCTYPE HTML> <html>
     <head>
         <title>
            Title of my project: A learning experience.
         </title>
         <style>

                html{
                  background:-moz-linear-gradient(top,#ccc,#000)  no-repeat ;
                  background:-webkit-gradient(linear,left top, left bottom, from(#ccc), to(#000)) no-repeat;

                }

         </style>
     </head>
     <body>
           ANYTHING!!!
     </body>
</html>

And for good measure, we want to add a background color to these tags, and a fallback background color.  This fallback background color should exist before the gradients, because we want the gradients to be the last thing applied.
<!DOCTYPE HTML> <html>
     <head>
         <title>
            Title of my project: A learning experience.
         </title>
         <style>

                html{
                  background:#222222;
                  background:-moz-linear-gradient(top,#ccc,#000)  no-repeat  #000;
                  background:-webkit-gradient(linear,left top, left bottom, from(#ccc), to(#000)) no-repeat #000;

                }

         </style>
     </head>
     <body>
           ANYTHING!!!
     </body>
</html>

And that should just about do it for the gradient.  Try opening it in different browsers and changing the background colors to get a cool gradient you like. 

And remember these lessons (I know I will):
  • The browser assumes nothing
  • There is always more than one way to skin a cat and no two venders will agree on which way is the correct way. -moz- vs -webkit- vs -ms- vs -o-
  • The body element is only as long as is specified in the HTML or is defined by the content
  • CSS3 is powerful shit.  Start reading up on it.
Thanks for reading!