0

So I have three images contained in a flexbox. It is centered perfectly but once I shrink the viewport down the image seems to stick to the left side and not automatically center itself. Examples of flexbox show images resizing to fit the screen and I don't quite know where I went wrong.

https://codepen.io/sblocks/pen/MXaRwd

<div class="services">
  <div class="three">
    <ul class="hover">
      <li>
        <img src="img/pictures/landing/1.jpg" alt="landscape">
        <h2>Landscaping</h2>
        <span class="h1"></span>
        <span class="h2"></span>
        <span class="h3"></span>
      </li>
      <li>
        <img src="img/pictures/landing/3.jpg" alt="tree">
        <h2>Tree Services</h2>
        <span class="h1"></span>
        <span class="h2"></span>
        <span class="h3"></span>
      </li>
      <li>
        <img src="img/pictures/landing/2.jpg" alt="plow">
        <h2>Storm Cleanup</h2>
        <span class="h1"></span>
        <span class="h2"></span>
        <span class="h3"></span>
      </li>
    </ul>
  </div>

.services {
  width: 100%;
  height: 100vh;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
  background-color: #2e2e2e;
}
.three {
  max-width: 950px;
  margin: auto;
  width: 100%;
  display: table;
  margin-top: 25vh;
}
.three:after {
  content: "";
  clear: both;
}
.hover {
  margin: 0;
  padding: 0;
  list-style-type: none;
}
.hover li {
  position: relative;
  max-width: 293px;
  background-color: #4f4f4f;
  width: 100%;
  height: 400px;
  margin: 10px;
  float: left;
  overflow: hidden;
  cursor: pointer;
  background-position: center;
}
.h1,
.h2,
.h3 {
  position: absolute;
  top: 0;
  left: 0;
  width: 0%;
  height: 135px;
  background-color: #c5ddb5;
  transition-duration: 0.8s;
}
.h2 {
  top: 133px;
  width: 100%;
  left: 293px;
  background-color: #68d793;
  transition-duration: 0.8s;
}
.h3 {
  top: 266px;
  transition-duration: 0.8s;
}
.hover li:hover .h1,
.hover li:hover .h3 {
  width: 100%;
}
.hover li:hover .h2 {
  left: 0;
}
.hover li h2 {
  position: absolute;
  top: 187px;
  color: #fff;
  text-transform: uppercase;
  font-size: 25px;
  z-index: 1;
  left: -110px;
  transform: translate(-50%);
  width: 134px;
  letter-spacing: 1px;
  transition-duration: 1.5s;
}
.hover li:hover h2 {
  left: 50%;
}

1 Answers1

0

How do I center list items inside a UL element?

You need to use display:inline-block instead of float:left (and obviously, you must add text-align:center to the container)

Neil
  • 390
  • 2
  • 14