【问题标题】:How to place a layer on top of another layer using flexbox?如何使用 flexbox 将一个图层放置在另一个图层之上?
【发布时间】:2021-09-10 12:48:29
【问题描述】:

在下面的sn-p中,我有一张卡片。在手机/平板电脑中,视频位于文本内容之上。在桌面上,内容向右移动,视频向右移动。

视频应位于内容 div 的垂直中心。我试过align-items: center;,但它不起作用,我认为这可能与视频有关,但我不确定。

我正在寻找最简洁的方法,将视频容器与左侧的内容 div 垂直对齐,然后将视频容器覆盖在内容 div 的顶部。

这是当前设置:

// Video Thumbnail Script
$('.vid-thumb').on('click', function () {
  $(this).remove();
  $('.vid-ct').prepend(
    '<script src="https://fast.wistia.com/embed/medias/2uzup7d1l3.jsonp" async><\/script><script src="https://fast.wistia.com/assets/external/E-v1.js" async><\/script><div class="wistia_embed wistia_async_2uzup7d1l3 videoFoam=true" style="width:100%;">&nbsp;</div>'
  );
  window._wq = window._wq || [];
  _wq.push({
    id: '2uzup7d1l3',
    onReady: function (video) {
      video.play();
    }
  });
});
/* Duru Sans */
@import url("https://fonts.googleapis.com/css2?family=Duru+Sans&display=swap");
/*resets*/
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  font-family: "Duru Sans", sans-serif;
  font-size: 16px;
  color: #000a70;
}

a {
  text-decoration: none;
}

.img-fluid {
  width: 100%;
  height: auto;
}

.container {
  padding-right: 24px;
  padding-left: 24px;
  width: 100%;
}

.people-love-nextiva h2 {
  font-size: 2.25rem;
  line-height: 2.75rem;
  font-weight: 900;
  text-align: center;
  margin-bottom: 2rem;
}

.video-slider {
  display: flex;
  flex-direction: column;
}
@media (min-width: 1200px) {
  .video-slider {
    flex-flow: row;
    align-items: center;
  }
}
.video-slider__video {
  order: 1;
}
.video-slider__video .vid-ct {
  width: 100%;
}
@media (min-width: 768px) {
  .video-slider__video .vid-ct {
    min-height: 388px;
  }
}
@media (min-width: 992px) {
  .video-slider__video .vid-ct {
    min-height: 524px;
  }
}
@media (min-width: 1024px) {
  .video-slider__video .vid-ct {
    min-height: 432px;
  }
}
@media (min-width: 1200px) {
  .video-slider__video {
    max-width: 50%;
    order: 2;
  }
}
.video-slider__content {
  padding: 2rem;
  background: #F2F5F9;
  order: 1;
  height: -webkit-fit-content;
  height: -moz-fit-content;
  height: fit-content;
  display: flex;
  flex-direction: column;
}
@media (min-width: 1200px) {
  .video-slider__content {
    order: 1;
    justify-content: center;
    height: 600px;
    padding: 10rem 18.5rem 10rem 4rem;
  }
}
.video-slider__content h6.kicker.kicker--light {
  font-size: 0.75rem;
  line-height: 0.75rem;
  letter-spacing: 1px;
  text-transform: uppercase;
}
.video-slider__content h4 {
  margin-top: 1rem;
  font-size: 1.75rem;
  line-height: 2.25rem;
  font-weight: 900;
}
.video-slider__content p {
  margin-top: 1.25rem;
  font-size: 1rem;
  line-height: 1.5rem;
}
.video-slider__content .txt-link {
  margin-top: 2rem;
}

.txt-link {
  display: inline-flex;
  align-items: center;
}
.txt-link a {
  color: #005fec;
  font-weight: 700;
  font-size: 1.125rem;
  position: relative;
}
.txt-link a:hover::after {
  visibility: visible;
  width: 100%;
}
.txt-link a::after {
  content: "";
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 0;
  height: 1px;
  background-color: #005fec;
  visibility: hidden;
  transition: all 0.2s ease;
}
.txt-link img {
  height: 0.75rem;
  margin-left: 0.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="people-love-nextiva">
    <h2>People love Animals.</h2>
    <div class="video-slider">
      <div class="video-slider__video">
        <div class="vid-ct">
          <div class="vid-thumb">
            <img class="img-fluid vid-thumb-img lazy" loading="lazy" srcset="https://nextivaweb.imgix.net/jpg/phat-scooters-video-overlay.jpg?auto=format&w=576&h=416 576w,
                                                                             https://nextivaweb.imgix.net/jpg/phat-scooters-video-overlay.jpg?auto=format&w=768w&h= 768w"
                 src="https://nextivaweb.imgix.net/jpg/phat-scooters-video-overlay.jpg&w=576"
                 sizes="(min-width: 320px) 100vw" alt="Phat Scooters - A Nextiva Customer Success Story">
          </div>
        </div>
      </div>
      <div class="video-slider__content">
        <h6 class="kicker kicker--light">
          Customer Story
        </h6>
        <h4>Company Name</h4>
        <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Consequatur, porro.</p>
        <span class="txt-link arrow-link">
          <a href="#">Read more</a>
          <img alt="arrow right icon" class="learn-more-arrow" src="https://nextivaweb.imgix.net/icons/Arrow-Right-Hover-Animation.svg" loading="lazy">
        </span>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: css flexbox


    【解决方案1】:

    我不相信有一个“最佳”解决方案,因为有多种解决方案,根据您的 HTML 结构,一个可能会比另一个匹配更好:

    .my-element {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        // align vertically and horizontally:
        // left: 50%;
        // transform: translate(-50%, -50%);
    }
    

    另一种方法是通过 flexbox 属性来实现:

    .parent {
        display: flex;
    }
    
    .parent .child {
        margin: auto;
    }
    

    另一个通过table-cell:

    .parent p {
        display: table-cell;
        vertical-align: middle;
    }
    

    要叠加,我可能会使用第一个解决方案,因为它可以让您轻松地使用位置,然后添加 z-index 以确保视频是第一层。

    否则(个人不喜欢)我会在您的视频容器上以负边距播放。

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 2023-01-09
      相关资源
      最近更新 更多