tags:

views:

257

answers:

3

Can this be done?

It seems like this should be possible. In general, I want the whole panel (div) to flow as you would usually expect within the body. However, I'd like to absolutely position the controls within the panel.

My experience is that when I try to absolutely position a control within a flow panel, the controls are not treated as being contained by the panel with relative coordinates of it container.

You can do this with a flow panel in WinForms and I like the mix of approaches. In my mind, it would be the best of both worlds, though I expect responses saying it would be a bad thing.

Comments?

+1  A: 

Given this HTML:

<div class="panel">
  <h2>Panel title</h2>
  <div class="close">x</div>
  <div class="content">Panel content</div>
</div>

You can use this CSS:

div.panel{
  position:relative;
}
div.panel h2,
div.panel div.close,
div.panel div.content{
  position:absolute;
}

<div class="panel"> will remain in the usual document flow, while each of its children will be absolutely positioned within that panel.

Ron DeVera
+1  A: 

Change the CSS 'position' property of the <div> to 'relative'. If your HTML looks like:

<body>
    <div id="container">
        <a id="control">start</a>
    </div>
</body>

then update the CSS to:

#container {
    position: relative;
}
#control {
    position: absolute;
    top: 0;
    left: 0;
}

The #control element will now be relative to the #container.

johnvey
I don't know if this is the ONLY correct answer, but I understood it quickly and I tested it and it works. Thanks so much! I love this site!
Velika
A: 

If you set the div to have position: relative, any child elements of the div can be absolutely positioned within the div by setting them to have position: absolute.

Example:

HTML:

<div id='panel'>
    <div id='control'>I'm positioned absolutely!</div>
</div>

CSS:

#panel { position: relative; width: 300px; height: 100px;}
#control { position: absolute;  top: 10px; left: 20px;
mishac