如何使 div 在不同大小下保持相同的格式
How to make div remain formatted the same at different sizes
我们有一个 Bootstrap 带有背景图像、一些文本(h1 和 p)和号召性用语按钮的超大屏幕,并且希望能够针对不同的视口缩放图像,但是保持格式不变。因此 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
的一个怪癖。如果使用基于百分比的值,它将与 width 相关,而不是您可能期望的元素高度。阅读更多: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
属性 通过 div 将高度乘以宽度然后将该比率乘以 100% 来计算纵横比以获得正确的高度 相对于宽度(因为它是padding-top)。现在外容器将始终保持固定的 height/width 纵横比。将图像用作背景 div。您的内部内容可以像这样排列以覆盖该区域:
.heroContent {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
要偏移内容的位置,请利用 padding-top
的相同怪癖:
.heroContent {
padding-top: 8%;
padding-left: 8%;
}
请记住 padding-top
是基于宽度的,因为该值是百分比。现在您只需要内容随 div 缩放。为此,分配 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;
}
运行 全屏视图中的代码段并更改视口宽度以查看效果。希望有帮助!
.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>
我们有一个 Bootstrap 带有背景图像、一些文本(h1 和 p)和号召性用语按钮的超大屏幕,并且希望能够针对不同的视口缩放图像,但是保持格式不变。因此 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
的一个怪癖。如果使用基于百分比的值,它将与 width 相关,而不是您可能期望的元素高度。阅读更多: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
属性 通过 div 将高度乘以宽度然后将该比率乘以 100% 来计算纵横比以获得正确的高度 相对于宽度(因为它是padding-top)。现在外容器将始终保持固定的 height/width 纵横比。将图像用作背景 div。您的内部内容可以像这样排列以覆盖该区域:
.heroContent {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
要偏移内容的位置,请利用 padding-top
的相同怪癖:
.heroContent {
padding-top: 8%;
padding-left: 8%;
}
请记住 padding-top
是基于宽度的,因为该值是百分比。现在您只需要内容随 div 缩放。为此,分配 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;
}
运行 全屏视图中的代码段并更改视口宽度以查看效果。希望有帮助!
.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>