Tuesday, August 5, 2008

Creating DIV box with round corners

One of the most intriguing problems I've ever come across on HTML is creating boxes using DIV that have round corners.

Gone are the days when a TABLE was used for almost anything on a webpage. People generally find it quite convenient to make the whole webpage using a TABLE. But the best practices hold that TABLEs should only be used to display data, not design UI.

So what do we use for designing UIs? DIV, of course :) along with CSS.

If you are a TABLE lover and wish to design better by switching over to DIV-based design, chances are that you'd end up being frustrated. Initially you might find it a bit troublesome to grasp the idea, but with practice you can make your life easy.

I will not go into the details of the DIV based designs, but I'll definitely point you to the page which helped me get the initial idea:
http://www.devarticles.com/c/a/Web-Style-Sheets/DIV-Based-Layout-with-CSS/
Please visit this article to understand what this article is talking about.

So lets get down with the basic idea of round corners. Let us see how this could have been done using tables:



With tables, you could have 3 TRs:
  1. First TR has 3 TDs. The ones at the end have the image for round corners.
  2. Second TR has one TD, the content area, for the main content that needs to be shown in the box.
  3. And the third TR is same as first with the rounded corners for the bottom.
I'm sure you knew all these. But the problem arises when you decide to be a part of the DIV community and take a pledge to use nothing but DIVs.

If you've designed a few DIV based web-pages, you would know that you need to arrange the elements of the page into different layers of DIVs.

Here's a plan of how our rounded-corner DIV should look like:




Now we can see that what were TRs earlier are now reduced to different DIVs.

Let me explain the different layers of divs here. There are a total of eight DIVs in this approach. Everything that you see above is a DIV:

  1. The whole of the big box you see above is a DIV. Its called a container and it is required to hold and control the remaining DIVs.
  2. The Top Bar is DIV #2. This is used to control DIVs #3 and #4, top-left and top-right respectively.
  3. The content area is DIV #5.
  4. The Bottom Bar is DIV #6 and is used to hold the DIVs #7 and #8, bottom-left and bottom-right respectively.
So, you might have figured out by now how we're going to do this. Here are the steps:
  1. Create the container DIV. Create all the following DIVs inside the container DIV in order to keep them controlled.
  2. Create the "topbar" DIV and make it float:top
  3. Create DIVs "top-left" and "top-right" inside the "topbar" and make them float left and right respectively.
  4. Create the DIV "content". Make it float:top so that it joins the topbar from bottom.
  5. Create the DIV "bottombar" and make it float:top
  6. Create the DIVs bottom-left and bottom-right inside the "bottombar" and make them float left and right respectively.
Of course, the DIVs top-left, top-right, bottom-left and bottom-right need to contain the rounded-corners image.

For the demo code that follows, i've used the follows, i've used the following images ( I bet you cannot see any of the images below. So click on the adjoining links if you wish to save them to run the demo):

<- Top Left

<- Top Right

<- Bottom Left

<- Bottom Right


And, here's the code that does the trick:
<html>
<head>
<style>
body{
background-color: #DADADA;
}
.rounder-container{
background-color: #FFFFFF;
width: 400px;
}
.topbar{
height: 5px;
width: 100%;
float: top;
}
.top-left{
background-image: url('top-left.png');
background-repeat: no-repeat;
height: 5px;
width: 5px;
float: left;
}
.top-right{
background-image: url('top-right.png');
background-repeat: no-repeat;
height: 5px;
width: 5px;
float: right;
}
.content{
float: top;
}
.bottombar{
height: 5px;
width: 100%;
float: bottom;
}
.bottom-left{
background-image: url('bottom-left.png');
background-repeat: no-repeat;
height: 5px;
width: 5px;
float: left;
}
.bottom-right{
background-image: url('bottom-right.png');
background-repeat: no-repeat;
height: 5px;
width: 5px;
float: right;
}
</style>
</head>

<body>
<div class="rounder-container">
<div class="topbar">
<div class="top-left"></div>
<div class="top-right"></div>
</div>
<div class="content">
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
just some content!<br/>
</div>
<div class="bottombar">
<div class="bottom-left"></div>
<div class="bottom-right"></div>
</div>
</div>
</body>
</html>

Save the above code as HTML in a folder along with the round corner images. Open the page in Mozilla Firefox for now. If you've done everything right, the box should appear correctly.

Now for the frustrating part. Open the same page in IE. Aren't you frustrated now to see the output? I'm sure you are.

But we have a solution for that. Declare the following doctype at the top of the HTML source. It should be the first line in the file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Now refresh the page in IE. Things should appear fine now.

This should suffice for most of you, but you might also find that the text is too near the boundary. You might want to put in some padding there.
Now there are quite some sets of issues related to padding, so we're not going to get into that right now. What we'll do is create another DIV inside the DIV "content", and specify a reasonable margin to keep it a bit away from the boundaries of the parent DIV.

So modify the code as:

<div class="content">
<div class="rounder-content">
some content!
....
....
</div>
</div>
And update the style sheet with:
.rounder-content{
margin: 3px 7px 3px 7px;
}
That's all you need to do and you'll get a brand-new, shining and glazing round-cornered DIV ready to be used. :)

Wednesday, April 2, 2008

Windows - Creating a blank file of specified size

The other day, i required a big file to test my application. First i thought i'd use Explorer.exe thinking it to be a really big file. But that turned out to be only 1Mb in size.
So googling around, i found some good solutions. I could find two ways to do it:

The first way:
fsutil file createnew <FileName> <Size
>
fsutil stands for File System Utility and it has quite some functionalities. Check them out by typing fsutil /? at the command prompt.
The parameter FileName is, obviously, the name of the file that you wish to create
Size is the size of the file which you want to create. This parameter needs to be specified in bytes. So if you wish to create a file of size 2Gb (2 * 1024 * 1024 * 1024), then you need to specify the size as: 2147483648.

The second way:
You will need cygwin to run this command on Windows, because this is a Unix command:
eg.
dd if=/dev/zero of=10mb-file.bin bs=1024k count=10
dd : Wikipedia says, "dd is a common UNIX program whose primary purpose is the low-level copying and conversion of raw data. dd is an abbreviation for "data definition" in IBM JCL, and the command's syntax is meant to be reminiscent of this."
if : Stands for Input File.
/dev/zero: Wikipedia explains this as, "In Unix-like operating systems, /dev/zero is a special file that provides as many null characters (ASCII NUL, 0x00; not ASCII character "digit zero", "0", 0x30) as are read from it."
of : Stands for Output File
bs : Block size
count : Copies only the specified number of input blocks
The command shown above creates a 10 Mb file.