[英]How do I create 2 responsive column divs that stack on top of each at different screen sizes?
Updated question and provided more clarity:更新了问题并提供了更清晰的信息:
How do I create two responsive divs that sit next to each other and stack on top of each other at different screen sizes?如何创建两个彼此相邻并以不同屏幕尺寸堆叠在一起的响应式 div? The dimensions are specific (each div is 350 x 217 px).
尺寸是特定的(每个 div 为 350 x 217 像素)。
One div will have a text centred horizontally within the div but also left aligned and the other will be an image.一个 div 将有一个文本在 div 内水平居中但也左对齐,另一个将是图像。
Below is the ideal end result.下面是理想的最终结果。 I'm a newbie to dev and this is for an assignment that's overdue.
我是开发新手,这是一个过期的任务。 I've been fiddling around for the past couple of days and I keep going round in circles.
在过去的几天里,我一直在摆弄,我一直在转圈。
Responsive view on smaller screens:小屏幕上的响应式视图:
* { box-sizing: border-box; }.column { flex: 1; }.left { background-color: #e0e620; }.center { margin: auto; border: 3px solid green; padding: 50px 50px; }.right { background-color: #E5E5E5; }.row { display: flex; } img { width: 100%; display: block; } @media screen and (max-width: 600px) {.row { width: 100vw; display: block; }
<div class="row"> <div class="column left"> <div class="center">The Info.<br /> <a class="link-button-green" href="" title="Info guide">Download now</a> </div> </div> <div class="column right"> <img src="https://via.placeholder.com/350x217.jpg"> </div> </div>
The image is 350 x 217. Here is the ideal look below:图像为 350 x 217。下面是理想的外观:
The below screen is what I would like to achieve without changing the existing width and height of yellow div?下面的屏幕是我想在不改变黄色 div 的现有宽度和高度的情况下实现的? How do I go about achieving that?
我该如何实现这一目标?
display: grid
to the parent element .row
;display: grid
添加到父元素.row
;grid-template-columns: 1fr 1fr
to the parent element ( .row
) to have a 2-column layout.grid-template-columns: 1fr 1fr
到父元素 ( .row
) 以具有 2 列布局。grid-template-columns
to 1fr
for the element .row
to get a 1-column layout..row
的grid-template-columns
更改为1fr
以获得 1 列布局。 To have both elements have the same height, you can use grid-auto-rows: 1fr
on the parent element.grid-auto-rows: 1fr
。display: flex; flex-direction: column
display: flex; flex-direction: column
display: flex; flex-direction: column
to the .left
column. display: flex; flex-direction: column
到.left
列。 Flexbox will allow you do vertical center the text. flex-direction: column
) with justify-content: center
justify-content: center
将其与主轴( flex-direction: column
)对齐 * { box-sizing: border-box; }.row { display: grid; grid-template-columns: 1fr 1fr; } @media only screen and (max-width: 600px) {.row { grid-template-columns: 1fr; grid-auto-rows: 1fr; } }.left { background-color: yellow; display: flex; flex-direction: column; justify-content: center; } img { width: 100%; display: block; object-fit: contain; }
<div class="row"> <div class="column left"> <div class="center">The Info.<br /> <a class="link-button-green" href="" title="Info guide">Download now</a> </div> </div> <div class="column right"> <img src="https://via.placeholder.com/350x217.jpg"> </div> </div>
Your question needs work.你的问题需要解决。
You want the specific div widths to be 350px x 217px, but when you get to a min-width of 600px, that width of 350px isn't going to work anymore.您希望特定的 div 宽度为 350px x 217px,但是当您达到 600px 的最小宽度时,350px 的宽度将不再起作用。 You will struggle to have a responsive page if you set explicit heights and widths.
如果您设置明确的高度和宽度,您将很难拥有一个响应式页面。 You need to let elements fill their spaces naturally.
你需要让元素自然地填充它们的空间。
That said, I've created a solution that I think would work best based on the images you've provided.也就是说,我已经创建了一个我认为基于您提供的图像效果最好的解决方案。
* { box-sizing: border-box; margin: 0; padding: 0; } img { max-width: 100%; height: auto; }.container { display: flex; flex-direction: column; } @media screen and (min-width: 600px) {.container { flex-direction: row; min-height: 217px; } }.inner { background-color: #E0E61F; display: flex; justify-content: center; align-items: flex-start; flex-direction: column; width: 100%; min-height: 217px; }.inner span { padding-left: 1rem; min-height: unset; }.inner img { min-height: 217px; object-fit: cover; }
<div class="container"> <div class="inner"> <span><a href="#">The Info</a></span> <span>Download Now</span> </div> <div class="inner"> <img src="https://placekitten.com/1600/900" alt=""> </div> </div>
Like mentioned above i would start with mobile view.如上所述,我将从移动视图开始。 You can create a parent div around your two boxes called
container
with the following css properties您可以使用以下 css 属性在称为
container
的两个框周围创建父 div
display:flex
flex-direction: column
display:flex
flex-direction: column
Then add a media query set at the width you would like this divs to be side by side and change the flex direction on your container div to row然后在您希望此 div 并排的宽度处添加一个媒体查询集,并将容器 div 上的 flex 方向更改为 row
flex-direction: row
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.