1

Here's my plunker: http://plnkr.co/edit/0srpOqyv5o6V8NhSzhSA?p=preview

If you click on the word Location, the <ul> element is shown

I am struggling to align a <ul> element underneath its parent <p> tag.

The <ul> appears to always appear on the left (i know the position is absolute).

HTML:

 <div class="section-dropdown">
     <p>It is a
         <div class="dropdown-heading" ng-click="Location = !Location">
             Location
         </div>
         <ul ng-show="Location" class="dropdown-details">
             <li>Italy</li>
             <li>Spain</li>
             <li>Greece</li>
             <li>USA</li>
          </ul>
     </p>
 </div>
user3731438
  • 283
  • 8
  • 19
  • 1
    Note that you have block level elements inside of a `

    ` tag. This is illegal and will most likely break the HTML when rendered. `.dropdown-heading` and `.dropdown-details` will most likely appear outside of the `

    `.

    – pschueller Apr 04 '15 at 12:47
  • @pschueller - thanks for pointing this out, how should it be corrected? – user3731438 Apr 04 '15 at 12:52
  • 1
    I would end the `

    ` just before the `

      ` and change `.dropdown-heading` into a `` (inline).
    – pschueller Apr 04 '15 at 12:59

2 Answers2

1

Remove position:absolute; It's collapsing unto itself rather than expanding the full width when you do that so it can't center the text within itself.

Rob
  • 14,746
  • 28
  • 47
  • 65
  • this was my inital fix, however when more text is below, it forces it to drop down rather than appear above: http://plnkr.co/edit/d4dMaVCmk7fAD5JpsTaa?p=preview – user3731438 Apr 04 '15 at 12:45
  • I'm not sure what you mean by drop down rather than appear above. – Rob Apr 04 '15 at 12:47
  • @So, when you click on location in the second plunker i provided, the UL appears inline... i.e the "I dont like" "Places i have been" appear below the UL... Im trying to get the UL to appear on top of them, hence the abosulute positioning – user3731438 Apr 04 '15 at 12:51
  • It works that way for me. Which browser are you using? – Rob Apr 04 '15 at 12:54
  • Firefox and Chrome - can you send me your plunker? – user3731438 Apr 04 '15 at 12:56
1

As has already been established, when you position the <ul> absolutely it is removed from the flow of the document, meaning it is positioned outside of the containing element. You can force the <ul> to position itself in relation to one of the parent elements by giving a parent position: relative;. You may then also need to add overflow: visible; to see any part of the <ul> that might overflow the parent.

Now that the <ul> is relative to the container you can position it accordingly, for instance:

.dropdown-details {
  top: 155px;
  left: 50%;
}

If you want the absolutely positioned <ul> to then be horizontally centered like the rest of the content, there is a little trick you can use (see https://stackoverflow.com/a/1777282/2126792).


Here is your adjusted plunker.

I fixed the HTML and added a wrapper around the <ul>:

<div class="section-dropdown">
  <p>It is a
    <span class="dropdown-heading" ng-click="Location = !Location">Location</span>
  </p>
  <div class="dropdown-details">
    <ul ng-show="Location">
      <li>Italy</li>
      <li>Spain</li>
      <li>Greece</li>
      <li>USA</li>
    </ul>
  </div>
</div>

And then positioned it in relation to the .grid-wrap div:

.grid-wrap {
  position: relative;
  overflow: visible;
}
.dropdown-details {
  top: 155px;
  left: 50%;
}
.dropdown-details ul {
  position: relative; left: -50%;
  border: 1px dashed #ef8e80;
  padding: 9px;
  background: #ffffff;
}
Community
  • 1
  • 1
pschueller
  • 4,362
  • 2
  • 27
  • 50