Home Resources Forum Advertise Contact

Go Back   Webmaster Forums > Web Designing and Development > Web Design and Development

Web Design and Development Designing professional looking and fast loading websites is a real challenge. Let's talk about it here, and share some high quality resources perhaps.

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 20th July 2006, 09:45 AM
arpan911 arpan911 is offline
WD Addict Poster
 
Join Date: 22nd June 2006
Posts: 299
Default Css center layers & elements

CSS Center Elements

There is a way to center elements by browser size using CSS only. In order to achieve this effect, let us being by taking a look at the position attribute and understanding what each attribute value pertinent to positioning means and how it can be best deployed in a to center a layer across browser sizes. In the following examples we are going to be working with a CSS Layer, however it is important to recognize that this technique can be used on any element that is absolutely positioned.

Position: Absolute

The position: absolute attribute signifies to the browser that this element will be positioned independently of other elements that are used in the page. Consider the following code snippet.


<div id="Layer1" style="position:absolute; width:346px; height:64px; z-index:1; left: 200px; top: 150px; background-color: #666666" class="maintext">
This is an absolutely positioned &lt;div&gt; element, that is 200px from left and 150px from top
</div>



In the above code snippet, we are interested in three attributes; the position, left and top attributes. These three attributes tell the browser to place the <div> element 200px from the left of the viewing area and 150px from the top of the browser viewing area, regardless of the page flow or the screen size of the browser

If you resize the browser screen to be less than 200px wide and hit the refresh button then no matter what you do the element remains at 200px from left and 150px from top. If the viewing area of the browser is less than 200px wide and 150px in height then the element goes off screen. It is important to understand that your positioning point starts from the top left corner of an element.

If we changed the code so that the element is placed 50% from the top of the browser and 50% from the browsers left edge we still find some problems. The placement of the element is dependent upon the browser size and consequently won't appear to be centered in most browser sizes, simply because the positioning of the element begins at the top left corner of the element.

We can overcome this limitation by shifting the top left corner of the element to some other co-ordinate. How? By using margin attributes.

If you recall, the top and left corner of the element begins exactly at the top left corner. By setting margins for the element, the top and left coordinates are shifted to accommodate the margin settings. Consequently the browser interprets the element top and left corner as beginning wherever we specify the margin attributes to be and will display content from that starting point. Armed with this knowledge, we can then go about creating an element that will always center by using a little math.

To obtain the new left edge value, divide the width attribute value by two; 364px / 2 = 173. Conceptually what we are about to do is subtract the value 173 from the width. We do this by setting a CSS definition for the element consisting of margin-left: -173px;.

We do the same for the new top edge value and end with margin-top: -32px for that element. The following is the CSS relevant for the margin settings of Layer1;

#Layer1 {

margin-left: -173px;

margin-top: -32px

}

The inline styles would look like this;


<div id="Layer1" style="position:absolute; width:346px; height:64px; z-index:1; left: 50%; top: 50%; background-color: #666666" class="maintext">
This is an absolutely positioned &lt;div&gt; element, that is 200px from left and 150px from top
</div>

__________________
arps
Reply With Quote
Reply



Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Elements for ppc marketing campaign?? varun1182 Pay Per Click / PPC Engines 5 18th January 2008 01:24 AM
Javascript: drag & drop for images and layers arpan911 Web Design and Development 1 26th February 2007 11:06 PM
Five critical (and overlooked) design elements hassen1 Web Design and Development 0 10th November 2006 11:27 PM
New free resource center for webmasters hilliebillie Web Resource Centre 0 20th August 2006 10:01 PM


All times are GMT -4. The time now is 09:00 PM.


Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC6
vB Ad Management by =RedTyger=