• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

CSS Floats

What is CSS Float?

With CSS float ,an element can be pushed to the left or right ,allowing other elements to wrap around it .

Float is very often used for images, but it is also useful when  working with layouts.

How Elements Float

Element are floated horizontally, this means that an element can only be floated left or right, not up or down.

A floated element will move as far to the left or right as it can.Usually this means all the way to the left or right of the containing element.

The elements after the floating element will flow around it.

The elements before the floating element will not be affected.

If an image is floated to the right ,a  following text flows around it to the left:

Example

img

{

float:right;

}

<html>

<head>

<style type=”text/css”>

img

{

float:right;

}

</style>

</head>

<body>

<p> In the paragraph below ,we have added an image with style<b> float:right </b>.The result is that the image will float to the right in the paragraph </p>

<p>

<img src=”logocss.gif” width =”95”height =”84”/>

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

</p>

</body>

</html>

 

Floating Elements Next to Each Other

If you pace several floating elements after each other, they will float next to each other if there is room.

Here we have made an image gallery using the float property:

 

Example

thumbnail

{

float:left;

width:110px;

height:90px;

margin:5px;

}

<html>

<head>

<style type=”text/ccs”>

Thumbnail

{

Float:left;

Width:110px;

Height:90px;

Margin:5px;

}

</style>

</head>

 

<body>

<h3>Image Gallery</h3/

<p>Try resizing the window to see what happens when the images does not have enough room</p>

<img class=”thumbnail”src=”klematis_small.jpg”width=”107”height=”90”>

<img class=”thumbnail”src=”klematis2_small.jpg”width=”107”height=”80”>

<img class=”thumbnail”src=”klematis3_small.jpg”width=”116”height=”90”>

<img class=”thumbnail”src=”klematis4_small.jpg”width=”120”height=”90”>

<img class=”thumbnail”src=”klematis_small.jpg”width=”107”height=”90”>

<img class=”thumbnail”src=”klematis2_small.jpg”width=”107”height=”80”>

<img class=”thumbnail”src=”klematis3_small.jpg”width=”116”height=”90”>

<img class=”thumbnail”src=”klematis4_small.jpg”width=”120”height=”90”>

<body>

<html>

Turning off float-Using Clear

Elements after the floating element will flow around it. To avoid this, use the clear property. The clear property specifies which sides of an element other floating elements are not allowed.

Add a text line into the image gallery, using the clear property

Example

.text_line

{

Cler:both;

}

<html>

<head>

<style type=text/css”>

.thumbnail

{

Flaot:left;

width:110px;

height:90px;

<td colspan=”4”>This is the foot of the table</td>

</tr>

</tfoot>

</tbody>

<tr>

<td>cell 1</td>

<td>cell 2</td>

<td>cell 3</td>

<td>cell 4</td>

</tr>

<tr>

…more rows here containing four cells>>>.

</tr>

</tbody>

<tbody>

<tr>

<td>cell 1</td>

<td>cell 2</td>

<td>cell 3</td>

<td>cell 4</td>

</tr>

<tr>

…more rows here containing four cells>>>.

</tr>

</tbody>

</table>

This will produce following result

Screenshot_30

 

Nested Tables

You can use one table inside another table.Not only tables you can use almost all the tags inside table data tag<td>.

Following is the example of using another table and other tags inside a table cell.

Screenshot_31

 

Using Table Caption

The caption tags will serve as a title or explanation and show up at the top of the table. This tag is deprecated in newer version of HTML/XHTML.

<table border=”1”>

<caption>This is the caption</caption>

<tr>

<td>row 1,column 1</td><td>row1, column  2</td>

<tr>

</table>

 

This is produce following result:

Screenshot_32

 

Using a Header, Body and Footer

Tables can be divided into three portions: aheader, abody,and a foot. The head and foot are rather similar to headers and footers in a word-processed document that remain the same for every page,while the body is the main content of the table.

The three elements for separating the head,body and foot of a table are:

<thead>-to create a separate table header.

<tbody>-to indicate the main body of the table.

<tfoot>-to create a separate table footer.

A table may contain several<tbody>elements to indicate different pages of groups of data.But is notable that <thead> and <tfoot> tags should appear before <tbody>

<table border=”1” width=”100%”>

<thead>

<tr>

<td colspan=”4”>This is the head of the table</td>

</tr>

</thead>

<tfoot>

<tr>

margin :5px;

}

.text-line

{

clear :both

margin-bottom:2px;}

</style>

</head>

<body>

<h3> Image Gallery</h3>

<p> Try resizing the window to see what happens when the images does not have enough room.</p>

<img class =”thumbnail” src=”klematis_small.jpg” width=”107” height=”90”>

<img class =”thumbnail” src=”klematis2_small.jpg” width=”107” height=”80”>

<img class =”thumbnail” src=”klematis3_small.jpg” width=”116” height=”90”>

<img class =”thumbnail” src=”klematis4_small.jpg” width=”120” height=”90”>

<h3 class =”text_line”>Second row</h3>

<img class =”thumbnail” src=”klematis_small.jpg” width=”107” height=”90”>

<img class =”thumbnail” src=”klematis2_small.jpg” width=”107” height=”80”>

<img class =”thumbnail” src=”klematis3_small.jpg” width=”116” height=”90”>

<img class =”thumbnail” src=”klematis4_small.jpg” width=”120” height=”90”>

</body>

</html>

 

CSS Horizontal Aligning Block Elements

A block element is an element that takes up the full width valaible ,and has a line break before and after it .

Examples of block elements:

<h1>

<p>

<div>

For aligning text, see the CSS Text chapter. In this chapter we will show you how to horizontally align block elements for layout purposes.

Centre Aligning Using the margin Property

Block elements can be aligned by setting the left and right margins to “auto”.

Note : Using margin:auto will not work in IE 8 and earlier,unless a !DOCTYPE is declared.

Setting the left and right margins to auto specifies that they should split the available margin equally .The result is cantered element:

Example

.center

{ margin –left:auto;

margin-right:auto;

width:70%;

background –colour: #b0e0e6;

}

 

Tip

Aligning has no effect if the width is 100%

Note

In IE5 there is a margin handling bug for block elements. To make the example above work in IE5, add some extra code.

Note

In IE 5 there  is a margin handling bug for block elements.To make the example above work in IE 5,add some extra code.

 

Left and Right Aligning Using the position Property

One method of aligning elements is to use absolute positioning:

Example

.right

{

position :absolute;

right:0px;

width :300px;

background-color:#b0e0e6;

}

Note

Absolute positioned elements are removed from the normal flow, and can overlap elements.

Summary
Review Date
Reviewed Item
CSS Floats
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on CSS Floats"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.