overflow auto 和 white-space 的问题:nowrap 和 flexbox

Issues with overflow auto and white-space: nowrap and flexbox

我正在尝试创建一个具有固定宽度的可滚动水平时间轴。我在父容器上有 overflow-x: auto ,每当我添加 white-space: nowrap 到同一个元素时,子列表元素开始被切断。我认为这与 flex-box 的行为有关。我试过将 min-width: 0 添加到 flex 元素和 flex-grow: 1 但没有成功。

body {
  font-family: Arial, Helvetica, sans-serif;
}

.container {
  height: 200px;
  width: 500px;
  margin: 0 auto;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
}

.timeline {
  display: flex;
  justify-content: center;
  min-width: 0;
}

.timeline ol {
  list-style: none;
  display: flex;
  justify-content: center;
}

.timeline .horizontal-line {
  position: relative;
  background-color: #0d6a3d;
  height: 3px;
  border-radius: 4px;
  margin: 5em 0;
}

.event {
  margin: 0px 10px;
  position: relative;
}

.date {
  position: relative;
  text-align: center;
  top: -70px;
  width: 100%;
}

.date::after {
  content: "";
  position: absolute;
  bottom: -28px;
  left: 50%;
  right: auto;
  height: 20px;
  width: 20px;
  border-radius: 50%;
  border: 3px solid #00a950;
  background-color: #fff;
  transition: 0.3s ease;
  transform: translateX(-50%);
}

.content {
  width: 100%;
  text-align: center;
  position: relative;
  top: -45px;
}
<section class="container">
  <div class="timeline">
    <div class="horizontal-line">
      <ol>
        <li class="event">
          <h3 class="date">07/02/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
      </ol>
    </div>
  </div>
</section>

width: fit-content; 添加到您的 timeline。这让所有元素都适合水平滚动。然后您将在 ol 上保留默认值 padding-inline-start。您可以通过设置 padding: 0;padding-inline-start: 0;.

来消除该差距

body {
  font-family: Arial, Helvetica, sans-serif;
}

.container {
  height: 200px;
  width: 500px;
  margin: 0 auto;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
}

.timeline {
  display: flex;
  justify-content: center;
  min-width: 0;
  width: fit-content;
}

.timeline ol {
  list-style: none;
  display: flex;
  justify-content: center;
  padding: 0;
}

.timeline .horizontal-line {
  position: relative;
  background-color: #0d6a3d;
  height: 3px;
  border-radius: 4px;
  margin: 5em 0;
}

.event {
  margin: 0px 10px;
  position: relative;
}

.date {
  position: relative;
  text-align: center;
  top: -70px;
  width: 100%;
}

.date::after {
  content: "";
  position: absolute;
  bottom: -28px;
  left: 50%;
  right: auto;
  height: 20px;
  width: 20px;
  border-radius: 50%;
  border: 3px solid #00a950;
  background-color: #fff;
  transition: 0.3s ease;
  transform: translateX(-50%);
}

.content {
  width: 100%;
  text-align: center;
  position: relative;
  top: -45px;
}
<section class="container">
  <div class="timeline">
    <div class="horizontal-line">
      <ol>
        <li class="event">
          <h3 class="date">07/02/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
        <li class="event">
          <h3 class="date">08/25/2020</h3>
          <p class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </li>
      </ol>
    </div>
  </div>
</section>