Bootstrap 4如何重叠两个Divs进行快速叠加

时间:2019-08-29 14:19:21

标签: css bootstrap-4 responsive-design

我是一个后端人员,正在尝试为我们使用Bootstrap 4的项目弄清楚一些细节。

简而言之,我们要创建在此处执行的布局: https://codepen.io/mediumandmessage/pen/xVeXop

(此示例和下面的代码来自我发现的原始Bootstrap 3示例,而不是Bootstrap 4)

HTML:

.somesection {margin-top:50px!important;}
body {
  font-size:17px;
  font-family:Helvetica Neue;
}
img {max-width:100%;}

.overlay-text {
  font-size:3.5vw;
  float:right;
  width:65%; /*important*/
  bottom:21vw; /*important*/
  padding: 25px;
  background:#f7f7f7;
}
<div class="container somesection">
  <div class="row col-xs-6">  
     <img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80">
  </div>
  <div class="col-xs-3 col-sm-offset-4 overlay-text">
      This is some text that should partially overlay.
  </div>
</div>

但是,该示例使用Bootstrap 3并中断了Bootstrap 4(文本在图像下方水平显示),并且也没有响应地堆叠div。

我尝试过修改absoluterelative的位置,等等。它成为了很多代码,可以清晰地执行并做出响应,我希望外面有人可以对实现有所了解在纯Bootstrap4中...

如果任何人都可以在这里分享任何专业知识,我将不胜感激!

2 个答案:

答案 0 :(得分:1)

您可以将转换添加到覆盖列(对于较小的屏幕,可能需要通过媒体查询来取消此转换)。

请注意下面的html,我已修复您的代码以与boostrap 4配合使用-列必须在一行内(它们不能在一行内),而且我不认为有-xs再上课

.overlay-text {
  /* these two are needed - the align self makes the column not stretch the whole height of the image column and the translate moves the column over the image */
  align-self: flex-start;
  transform: translateX(-20%);

  /* the following are for example only */
  background-color: #ffffff;
  padding:20px;
}
<div class="container somesection">
  <div class="row">
    <div class="col col-sm-6">
      <img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&amp;auto=compress&amp;dpr=1&amp;crop=entropy&amp;fit=crop&amp;w=1087&amp;h=725&amp;q=80" class="w-100">
    </div>
    <div class="col col-sm-3 col-sm-offset-4 overlay-text">
      This is some text that should partially overlay.
    </div>
  </div>
</div>

Example bootply

答案 1 :(得分:0)

只需将position:relative;添加到.overlay-text 您还可以调整bottom

的值

.somesection {margin-top:50px!important;}
body {
  font-size:17px;
  font-family:Helvetica Neue;
}
img {max-width:100%;}

.overlay-text {
  font-size:3.5vw;
  float:right;
  width:65%; /*important*/
  bottom:21vw; /*important*/
  padding: 25px;
  background:#f7f7f7;
  position:relative;
}
<div class="container somesection">
  <div class="row col-xs-6">  
     <img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80">
  </div>
  <div class="col-xs-3 col-sm-offset-4 overlay-text">
      This is some text that should partially overlay.
  </div>
</div>