Sunday, 23 June 2013

Hacking CSS For Rounded Colors In All Browsers No Images And No Javascript


There are lots of tricks to get rounded/curved corners for our CSS websites. For example, you may want rounded corner for a box. You may easily think of using images and aligning it top left, top right, bottom left and bottom right. It sounds easy, but is way too complicated as we’ll need to create large number of blank divisions. Also another minus point can be, it is little bit harder to crop the exact pixel of the corner. Consequently, we’ll have something irregular display of corners and the plain line.
Also we’ve another technique, border-radius, which is supported from CSS3. But CSS3 is not supported in enemy of web developers, i.e. Internet Explorer. However CSS3 is partially supported in other popular browsers like Chrome, Firefox, Safari, etc.
The main problem here became the internet explorer. Even Internet Explorer 8 doesn’t support CSS3 rounded corners. However, it is expected in Internet Explorer 9.
Now, instead of trying images and CSS3, we’ll focus on CSS2 as every of the browsers supports it.
Before creating rounded corners we must technically have pixel to pixel knowledge about rounded corners.
Rounded corners are created from pixels. Imagine a bunch of few lines combining to form a rounded corner. To get clearer, refer to following example:
The above illustration is the illustrated microscopic view of rounded corner in raster image.
Again when the lines come closer together, then it looks like this:
Finally when all of the line combines then it gives a pixilated image of rounded corner.
In normal view, a rounded corner will look like the image below:
We are going to work on the same principle.
Assume that you want rounded corner of about 6 pixel, then you’ll need 7 different lines of 1 pixel height with several width.
As you can see the first line illustrated image above, the height is made constant with few gaps and the width keeps increasing by 1px or few more pixels.
For your easy, again refer to following illustration.
The first line is 93px long, and second line is 2px more than first one. After that the width of the line is kept increasing by 1px till it gives 99px.
Note:  For larger radius, you’ll need larger number of lines.
Now we’ll do the CSS trick here which is applicable as CSS2.
We’ll create 6 spans with different classes. We’ll name the class as class1, class2, class3, class 4, class5, class6. And we’ll put each line in each span class. But we are not going to place image, we’ll be instead using background color for each span.

You might question right here, why 6 classes only when we have 7 lines in above image. Well, this is because, the last two lines have similar width and we can set the height to make it as two lines.
Again to define the width of each span, we’ll use margin, where the actual width remains same but the background color will skip the marginal areas. And we’ll again set the height of each span.
OK, now let me show you how this can be accomplished in CSS script.
We will first set default values for all the classes that lies in span.
.class1, .class2, .class3, .class4, .class5, .class6 {
display:block;
overflow:hidden;
}
This will make each span as a block instead of inline content and set the overflow hidden.
We’ll now set the width of each line. But we are not using width CSS code as we actually will have dynamic div wrapping this. So we are doing only pixels work here not a line. Therefore, we’ll use margin.
Here is a code for each class,
.class1 {
Height:1px
Background:#ddd;
Margin:0 6px; /* this will make the first line of the rounded corner 6px smaller than normal maximum line width */
}
.class2 {
Height:1px
Background:#ddd;
Margin:0 4px; /* this will make the second line of the rounded corner 4px smaller than normal maximum line width */
}
.class3 {
Height:1px
Background:#ddd;
Margin:0 3px; /* this will make the third line of the rounded corner 6px smaller than normal maximum line width */
}
.class4 {
Height:1px
Background:#ddd;
Margin:0 2px; /* this will make the fourth line of the rounded corner 2px smaller than normal maximum line width */
}
.class5 {
Height:1px
Background:#ddd;
Margin:0 1px; /* this will make the fifth line of the rounded corner 2px smaller than normal maximum line width */
}
.class6 {
Height:2px /* this will double the line */
Background:#ddd;
Margin:0 0; /* this will make the last line of the rounded exactly the maximum width of the wrapper */
}
Now we’ll be coding for XHTML.
<div class=”outerwrapper”>
<span></span><span class="class2"></span><span></span><span class="class4"></span><span></span><span class="class6"></span>
<div class="content">
Rounded Corner here!!
</div>
<span></span><span class="class5"></span><span></span><span class="class3"></span><span></span><span class="class1"></span>
</div>
The bottom rounded corner must have reversed order of span class. Like we have class1 then class 2 then class 3 then class 4 up to class 6 for the top rounded corner. We’ll need exactly reverse, class6 then class5 … for the bottom rounded corner.
Finalized style sheet:
<style>
.outerwrapper {
Width:960px;  /* whatever width you set here, the rounded corner will automatically adjust itself to it.
}
.class1, .class2, .class3, .class4, .class5, .class6 {
display:block;
overflow:hidden;
}
.class1 {
Height:1px
Background:#ddd;
Margin:0 6px; /* this will make the first line of the rounded corner 6px smaller than normal maximum line width */
}
.class2 {
Height:1px
Background:#ddd;
Margin:0 4px; /* this will make the second line of the rounded corner 4px smaller than normal maximum line width */
}
.class3 {
Height:1px
Background:#ddd;
Margin:0 3px; /* this will make the third line of the rounded corner 6px smaller than normal maximum line width */
}
.class4 {
Height:1px
Background:#ddd;
Margin:0 2px; /* this will make the fourth line of the rounded corner 2px smaller than normal maximum line width */
}
.class5 {
Height:1px
Background:#ddd;
Margin:0 1px; /* this will make the fifth line of the rounded corner 2px smaller than normal maximum line width */
}
.class6 {
Height:2px /* this will double the line */
Background:#ddd;
Margin:0 0; /* this will make the last line of the rounded exactly the maximum width of the wrapper */
}
.content {
Padding:5px;
Background:#ddd;
}
</style>
Apply the above code and see the result in every browser. It is supported in every browser, as we have used nothing complicated here.
Hope this was helpful.

Tuesday, 11 June 2013

How To Create Simple Horizontal Navigation Bar From CSS

Though there are lots of tutorial about Horizontal listings, I founds lots of beginners having problem in understanding the mess of extra CSS codes added in it. There are several methods to achieve horizontal navigations.
One of them can be simply achieved by placing the sequence of links in a single line and styling it with CSS. Let’s see how to do this.
First of all, lets create a simple links arranged in a same horizontal line.
<A HREF=”#”>Home</A>
<A HREF=”#”>About us</A>
<A HREF=”#”>Portfolio</A>
<A HREF=”#”>Contact us</A>
Now when you save and load it in browser, it will look similar to the image below:
If you are satisfied with just these links in single line, then it is ok you can end the tutorial right here. Else if you want the horizontal navigation links like in Hackspc.com, you’ll have to follow up to the end of this tutorial.
We cannot easily create background effect in <A> without using CSS. So, we’ll need to style it with proper CSS.
Now let’s apply some CSS to the link. But before applying any CSS to the <A> tag, you must note that it must be specific to the horizontal links only. Otherwise, your other content links will also look same as the navigation links. So, let’s make it unique by keeping it inside a div with id menu.
<DIV ID=”MENU”>
<A HREF=”#”>Home</A>
<A HREF=”#”>About us</A>
<A HREF=”#”>Portfolio</A>
<A HREF=”#”>Contact us</A>
</DIV>
Now we’ll define the compound CSS to <A> so that the CSS style only applies to the link inside the MENU id.
Here is the simple CSS:
#MENU A {
Display:block;
Float:left;
Padding: 0 10px;
Background:#CCCCCC;
}
Now the above CSS will be applied to all the <a> tags inside <div id=”menu”>.  By default, <A> tag is a inline tag that doesn’t makes extra spaces, in simple terms it is an object without a box. While blocks are the one which contains four sides, in simple terms it is an object inside a box.
Since, <A> is not a block, we’ll have to manually make it block, this can be done by using display:block; property.  Generally, block occupies full width that they can reach up to. So, whenever we create block, it takes whole width. Look at the image below.
In the above image, you can see that how blocks behaved by occupying the full width. Since, it appeared in different lines, it is not according to our interest. We want all these to appear in same line.
We’ll use CSS floats to appear it in same line. We’ll float these blocks to left. And this is what we have after applying floats.
You can see that, though it appeared in single line this time, it is not what we wanted. The clumsy appearance of the navigation links in the above image looks like it is the single image. We won’t press SPACE key to give some white space here. We’ll be using padding to left and right side to give a little bit space to it.
Let’s set padding of 10 pixels on both sides,
Padding: 0 10px;
We’ll get following results after padding.
Whoa! Finally it appeared in same line. But you might not find any good remarkable difference here with the first image of this tutorial.
At least, we’ve kept it in a box we can apply any CSS effect to it.
Let’s play with the background and border, remove underline and change link color to white,
background:#ccc;
border-left:1px solid #000;
text-decoration:none;
color:#fff;
This will be the result:
Now let’s apply some hover effect to it. To do so, we again have to specify the compound CSS to the links and add pseudo class, :hover to it.
#MENU A:hover {
Background:#00CCFF;
}
Simply we changed the background color to hex value #00CCFF. Now whenever the mouse is hovered the link box will be highlighted with a color.
The final CSS will look like this:
.MENU A  {
display:block;
float:left;
padding:0 20px;
background:#ccc;
border-right:1px solid #000;
text-decoration:none;
color:#fff;
}
.MENU A:hover  {
background:#0CF;
}
In next tutorial, we’ll see how we can use this method to lists. Till then stay tuned and bookmark us.