Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Positioning elements, how do I do it ?! 2

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
0
0
IL
Hi everyone,
I try to work with css's position but it seems like I cannot handle it.
My code is consisted of a table inside a div.
The div's postion style is "relative" so that its' attributes will refer to the html page.
The table's position attribute is "absolute" so it should relate to its' ancestor: the above described div.
When I run the following code, the table disappears !
Why ?!
Code:
<html !doctype>
<head>
<style>
html, body, div, span, h1, h2, h3, h4, h5, h6, p, blockquote,a, abbr, acronym, address, big, cite, del, 
dfn, em, img, ins, kbd, small, strike, strong, sub, sup, tt, var,b, u, i, center,dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td,article, aside, canvas, 
details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary,time, 
mark, audio, video 	{margin: 0;	padding: 0;	border: 0;	font-size: 100%;font: inherit;vertical-align: baseline;}
.wrap {margin-top: 10%; margin-right: auto; margin-bottom: 10% margin-left:auto; position: relative; border-style: dashed; overflow:hidden;}
.myTable {direction:rtl; width:100% ; border-collapse: collapse; border: 1px solid black;position: absolute}
</style>
</head>
<body>
  <div class="wrap">
  <table class = "myTable">
    <tr>
      <th>a</th>
      <th>b</th> 
      <th>c/th>
    </tr>
    <tr>
      <td>1</td> 
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <td>1</td> 
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <td>4</td> 
      <td>5</td>
      <td>6</td>
    </tr>
    </table>
  </div>
</body>
</html>
 
The div's postion style is "relative" so that its' attributes will refer to the html page.

Nope. that is the most common misconception about positioning.

position: relative; is 'relative' to where the the element would be when positioned statically (the default)

position: absolute; is 'relative' to the 'page' layout, unless constrained by a 'non-static' parent.

Self promo link drop.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
Absolutely positioning an element takes it out of the document flow, so it has no bearing on its parent's height.

Your table is being hidden in the non-visible area of the "wrap" div, since your div has an overflow set to hidden.

You also have some errors in your CSS, so some rules aren't being applied. And an error in your HTML.

You have two choices depending on what exactly you want the result to be. Either you increase the "wrap" div's height so the table is visible, or remove the absolute positioning on the table so it affects the div's height.


As a rule of thumb, absolute positioning should be mostly avoided unless you absolutely need to have the element positioned somewhere specifically. Your code does not reflect this, so can't say whether you really need it or not. (Pun was intended)



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top