将列表项目中心与Flexbox列对齐

时间:2017-05-15 02:59:09

标签: html css css3 flexbox

我正在尝试将导航菜单对齐到图片上的右侧,我已设法将文本放在我想要的位置但是我无法弄清楚如何将列表项目居中,如果我是使用justify content: center;。它们排成一行并与左侧匹配。

.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/

2 个答案:

答案 0 :(得分:1)

我认为没有flexbox你会更好。我修改了你的CSS并指出了所做的更改。

.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>

答案 1 :(得分:0)

您也可以将带有背景图像的容器设为flex元素。您可以使用它来移动它的子元素。见下面的示例。

&#13;
&#13;
.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;
}
&#13;
<div class="landing">
  <ul>
	<li>One</li>
	<li>Two</li>
	<li>Three</li>
  </ul>
</div>
&#13;
&#13;
&#13;