Home » Html » CSS3 Flex: Pull child to the right

CSS3 Flex: Pull child to the right

Posted by: admin November 30, 2017 Leave a comment

Questions:

here’s what I have Fiddle

ul {
  display: flex;
  justify-content: flex-start;
  flex-direction: row;
  align-items: center;
  width: 100%;
  height: 100px;
  background: #333;
  padding: 15px;
}

ul li {
  padding: 15px;
  margin: 5px;
  background: #efefef;
  border: 1px solid #ccc;
  display: inline-block;
  list-style: none;
}

#item-1 {
  height: 50px;
}

#item-2 {
  height: 70px;
}
<ul>
  <li id="item-1">Home</li>
  <li id="item-2">Menu</li>
  <li>More</li>
  <li>Stuff</li>
  <li>Settings</li>
</ul>
Answers:

Simple fix, use an auto-adjusting margin:

ul li:last-child {
    margin-left: auto;
}

You may also want to not use width: 100% so that the element stays inside the visible area:

ul {
    display: flex;
    justify-content: flex-start;
    flex-direction: row;
    align-items: center;
    /* width: 100%; */
    height: 100px;
    background: #333;
    padding: 15px;
}

http://jsfiddle.net/dwLHE/

See also http://www.w3.org/TR/2012/CR-css3-flexbox-20120918/#auto-margins