html - 如何使div在不同尺寸下保持相同的格式

标签 html css twitter-bootstrap css-transitions transform

我们有一个带有背景图像、一些文本(h1 和 p)和号召性用语按钮的 Bootstrap Jumbotron,我们希望能够针对不同的视口(viewport)缩放图像,但要保持格式相同。所以 div 将被视为图像。

我在这里开始了这个实验:https://codepen.io/Codewalker/pen/xxKRLBd并尝试过过渡和变换,但我无法正确显示它。它一直表现得像一个响应式 div。

<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <img src="https://picsum.photos/id/1056/1424/562" alt="test" class="img-fluid">
      <div class="heroContent">
        <h1>Jumbotron H1 goes here</h1>
        <p class="pHero">
        This is the first line in the jumbotron.<br>
        The second line appears underneath the first line.
        </p>
        <button type="button" class="btn btn-primary">Chat With Us</button>
      </div>
    </div>
  </div>
</div>

.container {
  max-width:1424px;
}
.heroContent {
  position: absolute;
  top: 30%;
  left: 30%;
  transform: translate(-50%, -50%);
}

我们的目标是让整个 div 本质上像图像一样对待,它可以在不改变格式或布局的情况下进行缩放。

最佳答案

试试绝妙的 CSS 宽高比框技巧。基本思想利用了浏览器在计算 padding-top 时的一个怪癖。如果使用基于百分比的值,它将与宽度相关,而不是您可能期望的元素高度。阅读更多:https://css-tricks.com/aspect-ratio-boxes/

对于您的布局,我简化了标记,因此只有一个外部 div (.postcard) 和一个包含所有内容的内部 div (.heroContent)。

外部 div 得到 position: relative 因为内部 div 将被绝对定位。然后像这样应用纵横比技巧:

.postcard {
  width: 1424px;
  padding-top: calc(562 / 1424 * 100%);
}

padding-top 属性通过将高度除以宽度然后将该比率乘以 100% 来计算纵横比,以获得相对于宽度的正确高度(因为它是填充顶部)。现在外容器将始终保持固定的高度/宽度纵横比。将图像作为背景应用到该 div。您的内部内容可以像这样排列以覆盖该区域:

.heroContent {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

要偏移内容的位置,请利用 padding-top 的相同怪癖:

.heroContent {
  padding-top: 8%;
  padding-left: 8%;
}

请记住 padding-top 是基于宽度的,因为该值是百分比。现在您只需要内容随 div 缩放。为此,分配相对于视口(viewport)宽度的 font-size 值:

.heroContent h1 {
  font-size: 3vw;
  margin-top: 0; /* this line removes the extra space you would get from default margin top on a heading element */
}

.pHero {
  font-size: 1.8vw;
}

.heroContent .btn {
  font-size: 1.2vw;
}

在全屏 View 中运行代码片段并更改视口(viewport)宽度以查看效果。希望有帮助!

.postcard {
  position: relative;
  width: 1424px;
  max-width: 100%;
  padding-top: calc(562 / 1424 * 100%);
  background-image: url('https://picsum.photos/id/1056/1424/562');
  background-repeat: no-repeat;
  background-position: left top;
  background-size: cover;
}

.heroContent {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding-top: 8%;
  padding-left: 8%;
}

.heroContent h1 {
  font-size: 3vw;
  margin-top: 0;
}

.pHero {
  font-size: 1.8vw;
}

.heroContent .btn {
  font-size: 1.2vw;
}
<div class="postcard">
  <div class="heroContent">
    <h1>Jumbotron H1 goes here</h1>
    <p class="pHero">
      This is the first line in the jumbotron.<br> The second line appears underneath the first line.
    </p>
    <button type="button" class="btn btn-primary">Chat With Us</button>
  </div>
</div>

关于html - 如何使div在不同尺寸下保持相同的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598656/

相关文章:

javascript - 如何通过 Chrome 扩展突出显示 PDF 文件中的文本

javascript - 当我选中一个复选框(同一类)并将值插入文本框时,如何选中另一个复选框

html - 背景径向渐变在 Firefox 中被置换

css - 如何在 twitter bootstrap 中将默认背景颜色白色更改为其他颜色

javascript - 如何使用切换按钮而不是复选框/单选按钮?

asp.net - 调用 window.open 打开一个窗口,但随后它突然最小化

html - 使用属性选择器在 CSS 伪元素中换行

css - Div css 背景图像未显示 100% 高度

css - 转换 Bootstrap 2.3.2 和 jQuery-Spinner 以与 Bootstrap 3.* 一起使用

html - 对齐按钮中心小屏幕 Bootstrap