Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > CSS > CSS Cascading Style Sheets
|
CSS Cascading Style Sheets All issues relating to Cascading Style Sheets (CSS).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the CSS Cascading Style Sheets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 24th, 2010, 09:49 AM
Registered User
 
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default Show div at specific location.

Hi,

Which css should we user if we want to display a div tag at specific location on web page. The element (div) can also be in a container (or may not be), but irrespective to it's container's position the element should alowys at specific location from page's top.

Thank you,
 
Old November 24th, 2010, 07:35 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

This sounds like a navbar or login form. Regardless, you can do this relatively simply (depending on your other style rules).

Here's a quick demo

<html>
<head>
<title>demo page</title>
<style type="text/css">

body {
padding-top: 10em;
background-color: #00FFFF;
}

#child {
position: absolute;
top: 1em;
left: 1em;
background-color: #FF00FF;
}

#parent {
background-color: #FFFF00;
/* position: relative; */
}

#content {
background-color: #FF0000;
padding: 1em;
/* position: relative; */
}


</style>
</head>
<body>
<div id="content">
Placeholder
<div id="parent">
Parent Div
<div id="child">
Child Div
</div>
</div>
</div>
</body>
</html>

The general idea is that you add some padding-top to the body (or the wrapper div, id="content" which contains all the info on the page). This gives you some room at the top of the page to work with and you can adjust the size of the padding to your needs.

Next you position the child element absolutely. Try moving it around by changing the top and left properties. (There are also right and bottom properties, you can set all four, just one or any combination depending on what you need)

Note that this only works because there are no positioned ancestors. If either the content or the parent div have positioning, then the child div will still be absolutely positioned, but it will be placed relative to it's nearest positioned ancestor. Play around the positioning for the content and parent divs to see what happens.

This is called positioning context. If parent has a position declaration, then that will be the positioning context for the child div. If parent does not, but content does, then content will be the positioning context, etc. If no ancestors are positioned, then the body element is the last ditch positioning context which the browser uses and that's what you want in this case. Just remember that means you can't use positioning on any of the ancestors of the element you want to move.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Positioning Div over specific table cell pjmair HTML Code Clinic 4 May 8th, 2009 02:19 PM
Saving Picture file to specific Location sangeenobs VB How-To 0 December 13th, 2005 11:18 AM
Set cursor at specific location in text box pritz VB How-To 3 August 29th, 2005 10:27 AM
Set cursor at specific location in text box pritz Beginning VB 6 1 August 15th, 2005 12:22 PM
Window Size and DIV location TSEROOGY Javascript 6 August 30th, 2004 10:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.