RogerFedFan
RogerFedFan

Reputation: 566

Align List Items Center with Flexbox Column

I'm trying to align a nav menu to the right on a picture and I've managed to place the text where I want it but I can't figure out how to center the list items like I would if I was to use justify content: center;. They line up and match to the left.

.landing {
  background-image: url(http://adwallpapers.xyz/uploads/posts/72363-blonde-girl-blur-photo-4k-ultra-hd-wallpaper__celebrity.jpg);
  margin-left: 40px;
  margin-right: 40px;
  height: calc(100vh - 0px);
  background-size: cover;
  background-position: center center;
}

ul {
  display: flex;
  flex-direction: column;
  padding: 0;
  align-items: flex-end;
}

li {
  list-style: none;
  color: white;
  font-size: 25px;
  position: relative;
  top: 400px;
  right: 50px;
}
<div class="landing">
  <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
  </ul>
</div>

https://jsfiddle.net/oanja0xL/

Upvotes: 1

Views: 4311

Answers (2)

Gabe Hiemstra
Gabe Hiemstra

Reputation: 269

I think you'd be better off without flexbox. I have modified your CSS and indicated the changes made.

.landing {
    background-image: url(http://adwallpapers.xyz/uploads/posts/72363-blonde-girl-blur-photo-4k-ultra-hd-wallpaper__celebrity.jpg);
    margin-left: 40px;
    margin-right: 40px;
    height: calc(100vh - 0px);
    background-size: cover;
    background-position: center center;

    /* new */
    text-align: right;
}

ul {
    /*display: flex;*/
    /*flex-direction: column;*/
    padding: 0;
    /*align-items: flex-end;*/

    /* new */
    text-align: center;
    display: inline-block;
    margin-right: 10px;
    margin-top: 30px;
}


li {
    list-style: none;
    color: white;
    font-size: 25px;
    /*position: relative;*/
    /*top: 400px;*/
    /*right: 50px;*/
}
<div class="landing">
  <ul>
	<li>One</li>
	<li>Two</li>
	<li>Three</li>
  </ul>
</div>

Upvotes: 1

blecaf
blecaf

Reputation: 1645

You can make the container with the background image a flex element too. Which you can use to move it's child element. See sample below.

.landing {
	    background-image: url(http://adwallpapers.xyz/uploads/posts/72363-blonde-girl-blur-photo-4k-ultra-hd-wallpaper__celebrity.jpg);
    margin-left: 40px;
    margin-right: 40px;
    height: calc(100vh - 0px);
    background-size: cover;
    background-position: center center;
    display: flex;/**Added this**/
    align-items: center; /**Added this**/
    justify-content: flex-end; /**Added this**/
}

ul {
    display: flex;
    flex-direction: column;
    padding: 0;
	  /**align-items: flex-end; Removed this **/
}


li {
    list-style: none;
    color: white;
    font-size: 25px;
    position: relative;
    /* top: 400px;  Removed this*/
    right: 50px;
}
<div class="landing">
  <ul>
	<li>One</li>
	<li>Two</li>
	<li>Three</li>
  </ul>
</div>

Upvotes: 0

Related Questions