How CSS containers overlap and float on each other

We all know containers that web designers or even programmers used on the web! If we ignore all the coding you will find that all these things are actually done by CSS! i created two container with the following code,

<html>
<head>

<style>
#box1 {
width:450px;
height:338px;
background:#23e;
float: left;

}
#box2 {
width:450px;
height:338px;
background:#000;
padding: 5px 5px;
margin: 10px 10px;
position: absolute;
float: left;
z-index: 1;
}
</style>
<script>

</script>
</head>

<body>
<div id='box1'></div>
<div id='box2'></div>
</body>
</html>

You can have a look at the example HERE, notice that the black box is overlapping the blue box? The blue box has the CSS of box1 while the black box has the CSS of box2. The reason why it is overlapping was because of the following declaration,

<pre class="brush: php; title: ; notranslate" title="">  position: absolute;
 float: left;
 z-index: 1;

position absolute must be there to tell the page that the position must absolutely be obey and only with this declaration, z-index can be used. z-index tells the box to go 1 up (float upward) while negative means it will go downwards. So if i want the blue box to be overlapping the black instead, i will place z-index:-1, this way it will go below the blue box. While float:left tells the box to appear on the left side. If i do not want them to be overlapped, i will remove the declaration of position:absolute, this way z-index will not be valid and it will become side by side as shown HERE.

CSS is capable of styling and perform layout for your site! If i have another box name box3 and wanted the box to be place below the two box what do i do? assume the code declaration is as below,

<pre class="brush: php; title: ; notranslate" title=""><html>
<head>

<style>
#box1 {
width:250px;
height:338px;
background:#23e;
float: left;

}
#box2 {
width:250px;
height:338px;
background:#000;
padding: 5px 5px;
margin: 10px 10px;
float: left;

}

#box3 {
width:250px;
height:338px;
background:#23e;
float: left;
clear: left;
}
</style>
<script>

</script>
</head>

<body>
<div id='box1'></div>
<div id='box2'></div>
<div id='box3'></div>
</body>
</html> 

in box3, there is a new declaration, clear:left. This tells the page that box3 left side shall not have any other element which makes box3 move down to a new line. Click Here for example.Notice that this kind of layout is called a fixed layout where the boxes width cannot be more than the user screen width. If a liquid layout is being applied, the boxes will resize according to the user screen size. In order to change the fixed layout to a liquid layout, we just have to change the width to % instead of px.

Tutorial: how to attach an image to a link in css

First I made an unordered list in my html body with two links that I wanted. Here's an example below.

<div>
<ul>
	<li id="girl">
		<a href="http://looliwun.com">looliwun</a>
		</li>
	<li id="boy">
		<a href="http://hungred.com">hungred</a>
		</li>
		</ul>
</div>

This would result in a basic unordered list.

link1

I styled it in css using two pictures I made with Photoshop

ul
{
list-style: none; /*removes the underline below the link*/
margin: 0; /*removes the margin*/
padding: 0; /*removes the padding*/
}

ul li
{
float: left; /*this makes the element displays in a horizontal line*/
margin: 0;
padding: 0;
}

ul li a
{
color: #777;
display: block;  /*The element will be displayed as a block level element */
width: 75px; /*This allows the picture to be fit in the block*/
padding: 60px 10px 5px; /*There is 60px of padding on the top, 10px of padding on the right and left, and 5px of padding on the bottom*/
text-align: center;
text-decoration: none; /*removes all text decoration*/
}

ul li#girl a
{
background: transparent url(blablabla_files/rabbit.png) no-repeat top center; /*this makes the picture appears in the top center without any repeats*/
}

ul li#boy a
{
background: transparent url(blablabla_files/rawr.png) no-repeat top center;
}

ul li#girl a:hover,
ul li#boy a:hover
{
background-color: #FFFBC6;
/*this makes the color of the text change when the mouse hovers over the link*/}

ul li a:hover
{
color: #000 /*this makes the color of the text change when the mouse hovers over the link*/
}

This would make my link look like this
link2

When my mouse hovers over the link, the background colour changes to yellow, and the text of the link changes from gray to black.

Tutorial: How to add button image to a link with css

I got an image from the tutorial i wrote previous and wanted to do some simple stuff like adding it to a link so i did the following css code:

a.button {
display: block; 					/* this make it possible to hook the image to the link */
background-color: transparent;
background-image: url(mybutton.png); /*set the button image */
background-repeat: no-repeat;		/*set it once only */
width: 150px;						/*width of the image*/
height: 65px;						/*height of the image*/
margin: 5px auto;					/*align the text to center with top and bottom 5px margin*/
padding: 15px 0 0 0;				/*add a padding of 15px to the top*/
text-align: center;					/*this is required to make it align to center but we usually place this on the body instead*/
font-family: Myriad Pro, Calibri, Arial, sans-serif;/*font of the image*/
font-size: 100%;									/*font size*/
color: #FFF;										/*font color*/
font-weight: bold;									/*font style*/
text-decoration: none;								/*remove the link decoration*/
}

The code is well commented for understanding and you can find the sample code from here here. You will get something like this in HTML,

untitled107

Tutorial: How to create a Curved Border/Corner/Edge with various technique in css

There are many ways web designers use when creating a curved border on the edge. Here are some of the technique used,

n-Image

1. Fire up your photoshop and draw an rounded rectangle with the following slices made.

untitled86

  • Red: Slice option
  • Gray: Style made
  • Brown: rounded rectangle object used

2. Once you have done the above, click File->Save For Web & Devices.. and ensure that the image is .png extension so that it support transparency for those who are dealing with DHTML application.

untitled87

3. Once this is done, you will have the images you need for your CSS to create a curved border div. Now, we will proceed to code our CSS.

CSS code:

body
	{
	margin: 0;							/*set the page margin to 0*/
	padding: 0;							/*set the page padding margin to 0*/
	text-align: center; 				/*set the page alignment of our div to center*/
	}

#box
	{
	margin: 15px auto;					/*set the div to center with top and bottom 15px*/
	text-align: left;					/*set the div alignment to left*/
	width: 55em;						/*set the div size to 55em*/
	word-wrap:break-word; 				/*set the div word-wrap for ie5.5 above so that it will break to next line if it gets over horizontally*/
	overflow: hidden;					/*set the div word to be hidden if it goes over the right border for other browser*/
	background-image:url(images/c.png); /*set the div color with our image*/
	background-repeat: repeat;			/*repeat the image of our background*/
	}

#content
	{
	padding:3em; 						/*set the padding of our div content so that it will be nicely written with a fair amount of space*/
	}

/* ---=== border code follows ===--- */
/*
	tlc = top left corner
	trc = top right corner
	blc = bottom left corner
	brc = bottom right corner
	l = left border
	r = right border
	t = top border
	b = bottom border
*/

#tlc, #trc, #blc, #brc
	{
	background-color: transparent;		/*set background color to transparent for our 4 corner so that it will not go overlapped*/
	background-repeat: no-repeat;		/*set background to only display once for our 4 corner*/

	}

#tlc
	{
	background-image:url(images/tlc.png); 	/*set top left hand corner image*/
	background-position: 0% 0%;				/*set the position to start at 0,0, top left*/
	}

#trc
	{
	background-image:url(images/trc.png);	/*set top right hand corner image*/
	background-position: 100% 0%;			/*set the position to start at max width,0, top right*/
	}

#blc
	{
	background-image:url(images/blc.png);	/*set bottom left hand corner image*/
	background-position: 0% 100%;			/*set the position to start at 0,max height, bottom left*/
	}

#brc
	{
	background-image:url(images/brc.png);	/*set bottom right hand corner image*/
	background-position: 100% 100%;			/*set the position to start at max width,max height, bottom right*/
	}

#t
	{
	background-image:url(images/t.png); /*set the top border image*/
	background-position: 0% 0%;			/*set the position to start at 0,0, top left*/
	background-repeat: repeat-x;		/*repeat the top border image in x-axis*/
	}

#b
	{
	background-image:url(images/b.png); /*set the bottom border image*/
	background-position: 0% 100%;		/*set the position to start at 0,max height, bottom left*/
	background-repeat: repeat-x;		/*repeat the bottom border image in x-axis*/
	}

#r
	{
	background-image:url(images/r.png); /*set the right border image*/
	background-position: 100% 0%;		/*set the position to start at max width,0, top right*/
	background-repeat: repeat-y;		/*repeat the right border image in y-axis*/
	}

#l
	{

	background-image:url(images/l.png); /*set the left border image*/
	background-position: 0% 100%;		/*set the position to start at 0,max height, top left*/
	background-repeat: repeat-y;		/*repeat the left border image in y-axis*/
	}

HTML code:

<html>
<head>
<style>
<!--- CSS HERE -->
<!-- -->
</style>
</head>
</body>
		<div id="box">				

		<!--- box border -->
		<div id="l">
		<div id="r">
		<div id="b"><div id="blc"><div id="brc">
		<div id="t"><div id="tlc"><div id="trc">
		<!--  -->

		<div id="content">
			<h1>Viola~</h1>

			<p>
				This is really simply to do. But there is limitation for this method.
			</p>

		</div>

		<!--- end of box border -->
		</div></div></div></div>
		</div></div></div></div>
		<!-- -->

	</div>

</body>
</html>

untitled90

The HTML code and CSS are all explained on the code itself, the method use here is to pin 4 image on each edge and repeatingly putting image on the top and bottom in the direction of y-axis and x-axis. Lastly, we place an image which we took from the center of the image we created and place it as background of our content. You guys can download the above example code from curved-border. Please take note that the image you create on photoshop MUST be image with background color or else it will screw up the layout.

This technique is good with image that contain style such as shadow, glow etc. on its border. It allows user to text as long as they wish to provided they do not type 200 words continuously without space which will cause the word to go over the border.

This method should work for IE5.5 above, Firefox 1.0 above and other browser shouldn't have any issue with this.

Pure CSS

untitled88

body{padding: 20px;background-color: #FFF;font: 100.01% "Trebuchet MS",Verdana,Arial,sans-serif}

h1,p{margin: 0 10px}

h1{font-size: 250%;color: #FFF}

p{padding-bottom:1em}

div#box{ margin: 0 10%;background: #9BD1FA} /* set the margin to top-bottom, 0, left-right, 10%*/

/*key to curve is display:block*/
b.rtop, b.rbottom{display:block;background: #FFF}
b.rtop b, b.rbottom b{display:block;height: 1px;/* anything over the x-axis border will be hidden*/
    overflow: hidden; background: #9BD1FA}

/*form 3 layer which creates the curve*/
b.r1{margin: 0 5px}/*set the margin to left right with 5px of space*/
b.r2{margin: 0 3px}/*set the margin to left right with 3px of space*/
b.r3{margin: 0 2px}/*set the margin to left right with 2px of space*/

/*define a larger area can be remove but the curve won't look nice*/
b.rtop b.r4, b.rbottom b.r4{margin: 0 1px;height: 2px}

The explanation has been embarked in the code itself. The key to make this work is display:block without this, the border will not work regardless of the method used above. The theory in creating this is to form a pyramid like structure on both top and bottom and highlight the other background as the color used to structure the pyramid. Of course, the pyramid i'm talking here isn't the triangle shaped pyramid but the base line of the shaped structure(the first few layer). You can grab the code in pure-css.

CSS3

untitled89

The new implementation of CSS3 and above have many enhancement, one of it is a way to curve your corner without using any images with few simple code. However, this will only work for certain browser and not all browser support this method yet. However, the below code will work fine with Firefox and Safari for now.(means no no IE for ya)

CSS code:

body {
	font: 100%/1.5 "Myriad Pro","Myriad Web","Lucida Grande","Trebuchet MS","Tahoma","Helvetica","Arial",sans-serif;
	color: #fff;
	margin: 2em; 0 0 2em;

}

.box {
	width: 30em;
	background: #f9ac11  left top no-repeat;
	padding: 1em 2em;
	-moz-border-radius: 1.6em;
	-webkit-border-radius: 1.6em;
	border-radius: 1.6em;
}

h1 {
	margin-top: 0;
}

Others are basically the same other than the one highlighted in red. You can have a look on the code in roundercurve. This do work for IE with a little extension needed but require constant upgrade of the extension when new browser emerge to the market.

Limitation

All of the above technique mention above all have one similar weakness, they are all unable to automatically wrap the text within the div itself if the text typed do not have space and is very lengthy. The solution to this problem is to use server scripting such as PHP which has a built-in method 'wordwrap' or just try to contain your text while writing a post. Some uses javaScript to automatically breaks to the next line when the text goes over a certain define width but this degraded the performance of the overall site itself.

However, this situation happen rarely as user usually do not type a sequences of 200 words without a space in between? Cases such as this is rare and usually won't be a problem after overflow:hidden is written in the css. ( we hid it )

Tutorial: How to create columns using the div tag

First, I created a container div that would hold everything in my layout. My CSS container rule was

#container
{
width: 800px;
text-align: left;
margin: 0 0 0 150px;

}

Next, within the HTML container div, I inserted a div for the content column and a div for the navigation column


content

Within the CSS rules, my content column was

#content
{    width: 65%;
float: left;
}

whereas my navigation column was

#navigation
{
width: 30%;
margin-left: 70%;


}

This makes my file look like this

html