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

Tuesday, September 14, 2010

Webdesign, chapter eight: CSS media query (fixing chapter 6 !)

I just wanted to post a quick entry about the chapter 6 that I kinda screwed up.
In the mean time I have remade the layout that I had wanted to use for my website using float instead of fixed position. I do think that it should work using fixed position, but it apparently does not.
Mainly we just have 2 lines that give us the 2 column layout that we would like to have:
div.sidebar-left-outter {float: left; width: 15%; height: 100%; }
div.main-outter {float: right; width: 85%; height: 100%; }

And then in the media query CSS file we have the following block of code:
@media screen and (max-width: 799px) {
div.main-outter, div.sidebar-left-outter {float: none; width: auto; height: auto; margin: 0 auto;}
}

So mainly we're just clearing the float to make text not float around our 2 columns (which it never does, which is why I think it's a misapplication of the float property, but many people recommend doing it this way), then we set the width to auto, which tells the browser to use all the available space. Obviously we can't have 100% height on both elements that both use 100% width, since then we could only see one of them, and we set the margins to 0.

Monday, September 13, 2010

Webdesign, chapter seven: CSS media query part 2

Ever since i wrote the last blog entry I've been wondering how we can have a layout for internet explorer (who to this day doesn't get media queries) than for the browsers who understand media queries.
Now we have made a beautiful layout that changes from a standard 2 column layout to a 1 column layout depending on how wide the browser window is, and there's no way to tell internet explorer what to do without the risk of screwing it all up.... well obviously there is. Actually there are a few ways, but i didn't really like any of them, so eventually I came up with my own.
So for that we just take advantage of the fact that IE doesn't get the media queries.
We just include the basic CSS file first like so:
<link rel=stylesheet href="include/body.css" type="text/css">
<link rel=stylesheet href="include/print.css" type="text/css" media="print">
and then ! below the basic CSS file (so that it's closer to the content, and therefore overwrites the basic CSS if neeeded:
<script>
@import url('include/queries.css') screen and (min-width: 1px);
</script>

Now we have a basic style for our website in case the browser doesn't get media queries yet, and we can have a nice layout for everyone who uses a decent browser (whole thing visible on my homepage).

Friday, September 3, 2010

Webdesign, chapter six: CSS media query

I think I have a new pet peeve. There are way too many designers who put specific dimensions on their websites/blogs/etc. Even the default blogger theme has a set width in pixels. Most designs on the CSS Zen Garden have a width in pixels. Now obviously they want their stuff to look good, and I have to respect that. However, I don't really think it looks good when viewed on a monitor that isn't the specific single one that they used to make the design. You can't even make your design for the "most popular" monitor in use today since there are so many different sizes and even aspect ratios that it just doesn't make sense. Yet still, A LOT, of websites use dimensions in pixels rather than percentages.
Now that I'm done with my rant.... We can't really just make our content width and sidebar width use percentages instead of pixels and be happy with it. In the best case we will have a few users that will see a scrollbar on the navigation while not having very much room to see the content, in the worst case our site isn't usable at all for some people. Also when someone wants to print out our page because of the excellent content we have, they're probably not interested in seeing what links you have on the left there.
With that I give you CSS media types (not usable in Internet Explorer until version 9 comes out).
You can define CSS media types in 3 different ways (seems like anything in CSS has 3 different ways of doing it):
When loading a separate CSS file as we have been doing it:
<link rel="stylesheet" href="style.css" type="text/css" media="screen">,
The @media rule:
@media screen { p {color: blue;}}
The @import rule:
@import url("style.css") screen;

Now obviously we're making a new style sheet for the print media type with dark text on white background, in case we're not already having that as our standard style sheet.
But, and even more importantly, now we can use CSS 3 media queries to see how wide the user's screen is, and, if necessary, put the navigation below the content:
In our print style sheet we change the three lines below:
div.sidebar-left {display: none;}
body {color: #000; background-color: #fff; font-size: 100%}
h1 {text-align: center; margin: 0;}

we also took out the div.main code since we only have the 2 columns and if we don't give it any style information it will just inherit the styles from the body which is what we want right now. (note I actually changed the sidebar class to sidebar-left !)

Now we just have to change the style sheet for the screen a bit by putting in a few media queries:
For now I just appended the following to my style sheet:
@media screen and (max-width: 600px) {
div.main {position: fixed; top: 0%; left: 0%; width: 100%; height: auto;}
div.sidebar-left {position: static; width: 100%;}
}

The whole thing is visible here. It's quite interesting to see what happens when you resize the window (changing width to less than 600 pixels).

Now as i said before, the Internet Explorer can not handle media queries and neither can older browsers. The first part of that last sentence is important, older browsers are obviously less and less of a problem as we go forward, all of the talk about having your browser up to date so you don't get hacked and the media attention it has received lately has obviously helped that situation along a bit. Either way for now we have a style sheet that includes the styles that we want and we just hope that everyone using a browser that can't handle media queries has at least 601 pixel wide monitors.

Saturday, August 28, 2010

Webdesign, chapter four: CSS part three: replacing navigation frames

Everyone wants a navigation frame on the left, but everyone knows that frames have been outlawed.... or frowned upon anyway. Now you could use inline frames, which have not been obsoleted yet, but i think for the navigation of a website CSS (and a bit of php) works better.
Replacing the frames is pretty simple, but it would require us to put the navigation into every single page of the website.... and i will show you how ! I will later show you how to make it so you have a separate file for your navigation, just like you would when using frames.
For now, we just use a bit of CSS to make things appear where we want it to, which is navigation on the left and the main content on the right.
CSS has a few different ways of defining position: absolute, fixed, relative, static, and inherit. Inherit just inherits the position from the parent element. Static would place the element where the browser would ordinarily put it, in the flow of the website. Relative specifies where the element would be relative to the parent (you can move it to the left, right, up, down, relative to the parent element). Fixed positioned elements will stay where you put them, even if the website gets scrolled, even if an element would be covered by scrolling, the fixed element will stay right where you want it to be. Absolute position would put an element at a defined position relative to the first element that has a position other than static, if there aren't any, the html tag is the one that will be picked.
So with that we're obviously going to separate our website into 2 different areas with fixed positioning. The left one will be 15% wide and the main one will be 84% wide, with 1% left as a separator.
We're going to use an external style sheet for that which will look like this (for now (saved in post4.css)):
body {color: #08f; background: #fff; font-size: 100%}
h1 {text-align: center; margin: 0;}
div.sidebar-right {position: fixed; top: 0; left: 0; width: 15%; height: 100%; background-color: #00b;}
div.main {position: fixed; top: 0%; left: 16%; width: 84%; height: 100%; background-color: #000;}

and the html for the page looks like this (viewable here):

<head>
<title>Webdesign: chapter four</title>
<link rel="stylesheet" type="text/css" href="post4.css" />
</head>
<html>
<body>
<div class="sidebar-right">
<h1>Navigation</h1>
<p>link to something</p></div>
<div class="main">
<h1>Hi There !</h1>
<p>some text</p>
</div>
</body>
</html>

obviously the colors aren't all that great yet, but it's reasonably good for demonstration purposes. The body itself has a background color of fff (white), which is then covered by 00b (reasonably dark blue) on the left and 000 (black) on the right, leaving a 1% separator between the navigation and the main content, which is not covered and thus white.

Wednesday, August 25, 2010

Webdesign, chapter three: CSS syntax

This is going to be a very short post because all I'm saying here I should have said in the last one.
In the last post I have just briefly mentioned how to put CSS into an html document. I failed to mention what it is we're actually doing. It is all pretty simple and the syntax is the same in all three forms of integrating it into an html document. In the previous post I had two examples:
<body style="background-color: #000; color: #F00">
and:
p {color: #00F;}.
The only difference here is that in the first option we put the CSS code inside the html tag itself and thus we obviously don't have to specify which tag the CSS code applies to.
The second one is how it usually looks: We have a Selector and a declaration of property and value. Here we're giving the property color (for text foreground color) the value #00F, which is blue. The Selector can be either a tag, a class, or an ID. If we're writing code specific to one html tag we just put the exact tag into the CSS code, without the angle brackets, and define what we want to change. If we write code for an ID we're changing one specific tag inside the html document, for example:
#uniqueelement {color: #F00;}
defines a specific element that would look something like this in html:
<p ID="uniqueelement">some red text</p>.
If we were to put two html tags into the same document with the same ID, the browser should only apply the CSS to the first of them.
Classes are more useful in my opinion, in the CSS code you define a class by putting a dot before the name of the class and then just add your declarations:
.italic {font-style:italic;}. Then you add that class to the tags that you want to be printed italic:
<p class="italic">This part should be italic</p>. You can even use multiple classes inside the same html tag.
So with that you should be able to write your own basic CSS code and get all the style information out of the html tags. enjoy.
ps. I also think it's weird that I capitalize CSS but not html, but I think HTML looks weird.

Sunday, August 22, 2010

Webdesign, chapter two: CSS

This is just going to be a short introduction to CSS, it will only show a fraction of the things that you can do with CSS. This post is just meant to introduce how to use CSS and how to replace things that were formerly done with pure html with CSS.
I have decided to just focus on html5 and CSS3 since both of those are already usable even though they're still in development. That obviously changes the doctype of the previous post to the much easier <!doctype html>. we're also going to throw in a <meta charset="UTF-8"> to define which character set we're using. Now we're going to have to save all of our files in utf8 which is never a bad idea (unless, of course, if you're speaking a language that uses an alphabet not covered in utf8, in which case i'll leave it up to you to find the right charset to use (all "western" languages use only characters that are included in utf8 (English, German, French, Italian, etc.), Chinese, Japanese, Korean, Farsi, Arabic, etc. are not included)).
Now with all of that in mind our previous html code changes to this (viewable here):
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
<title>My First Website</title>
</head>
<body>
<p>This is my first website !</p>
</body>
</html>

Back to the CSS at last....
There are three different locations inside an html document where you can put your CSS code:
in a seperate file,
inside the <head> tag,
inside the html tag of the content you're trying to beautify.
If you want to put your CSS into a seperate file (which is strongly recommended, at least for things that you're using a lot), you have to link to that file inside the <head> tag. Just insert a tag into the <head> like this: <link href="./style.css" type="text/css" rel="stylesheet"/>. That assumes that you have a file named style.css in the same folder as your html file.
You can put all of the CSS code for the current document inside the <head> tag itself as well. For that you just need to add a new tag like this: <style type="text/css">, add your CSS code in there just like you would put it into a different file, for example: body {background-color: #000000; color: #0000FF} and close the tag with </style>.
The third option is to put all of the CSS inside each of the tags that you're changing, like so <body style="color: #ff0000">.
If you now add both of the latter CSS statements to your website the color of the text will be red (ff0000), unless you change it to a different color inside a deeper tag. That is because the code inside the html tag is closer to the content than the code in the head tag, which in turn would be closer to the content than an external stylesheet, and thus would overwrite the external stylesheet should there be anything that is defined contradictingly (for some reason that's not a word) in both.
so with that we have our new website (viewable here):
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
<style type="text/css">
p {color: #00F;}
</style>
<title>My First Website</title>
</head>
<body style="background-color: #000; color: #F00">
<p>This is my first website !</p>
</body>
</html>

Facebook