2

I’m trying to implement something like a web page navbar. I have code like this:

nav {
  position: relative;
  display: inline-block;
}

.nav-right {
  display: inline-block;
  position: absolute;
  right: 0px;
  list-style: none;
}
<nav>
  <h2>My First Website</h2>
  <ul class="nav-right">
    <li>Home</li>
    <li>About</li>
    <li>Contact</li>
  </ul>
</nav>

I have known that an absolute element is positioned to its first positioned (not static) ancestor element. I have set position property of element nav as absolute, but it looks like the <ul> run out of the <nav> range. In other word i'm confused about why nav'height is less than ul's Thanks a lot.

enter image description here

cpprookie
  • 122
  • 2
  • 11

4 Answers4

2

Absolute position elements are removed from the document flow. So the parent's height will hold only the other elements. Not the absolute position elements.

If you want your parent to cover your absolute position element then you have to set a fixed height to the parent element. You can set top: 0; like other's have answered. But still your parent's height wont be determined by the absolute positioned element, that's the reason the ul is outside the nav.

karthick
  • 11,998
  • 6
  • 56
  • 88
  • Thanks a lot, this is the answer I'm looking for. By the way, my english is too terrible to describe my confusion clear but you truely understand what i mean. – cpprookie Jun 09 '17 at 07:09
1

try top : 0px; also with that.If the top is auto then it defaults to their position: static values.Check this for more details:position: absolute without setting top/left/bottom/right?

Also display:block for the nav for the nav to take full sapce

nav {
  position: relative;
  display: block;
}

.nav-right {
  display: inline-block;
  position: absolute;
  top:0px;
  right:0px;
  list-style: none;
}
<nav>
      <h2>My First Website</h2>
      <ul class="nav-right">
        <li>Home</li>
        <li>About</li>
        <li>Contact</li>
      </ul>
    </nav>
XYZ
  • 4,450
  • 2
  • 15
  • 31
1

Use display: block; in parent and add top:0; in child to align in right side of parent

nav {
  position: relative;
  display: block;
}

.nav-right {
  display: inline-block;
  position: absolute;
  right: 0px;
  top:0;
  list-style: none;
}
<nav>
  <h2>My First Website</h2>
  <ul class="nav-right">
    <li>Home</li>
    <li>About</li>
    <li>Contact</li>
  </ul>
</nav>
jafarbtech
  • 6,842
  • 1
  • 36
  • 55
  • 1
    Thanks jafarbtech, your code works but @karthick truely point out the core problem so i have to vote him. Thanks again :) – cpprookie Jun 09 '17 at 07:13
1

Try This :

nav {
  position: relative;
}

.nav-right {
  display: inline-block;
  position: absolute;
  top: 0px;
  right: 0px;
  list-style: none;
} 
 <nav>
  <h2>My First Website</h2>
  <ul class="nav-right">
    <li>Home</li>
    <li>About</li>
    <li>Contact</li>
  </ul>
</nav> 
Ehsan
  • 12,655
  • 3
  • 25
  • 44
  • Thanks ehsan, your code works but @karthick truely point out the core problem so i have to vote him. Thanks again :) – cpprookie Jun 09 '17 at 07:12