简体   繁体   English

如何创建 2 个响应式列 div,以不同的屏幕尺寸堆叠在每个列的顶部?

[英]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?我该如何实现这一目标?

  1. add display: grid to the parent element .row ;display: grid添加到父元素.row
  2. add grid-template-columns: 1fr 1fr to the parent element ( .row ) to have a 2-column layout.grid-template-columns: 1fr 1fr到父元素 ( .row ) 以具有 2 列布局。
  3. For screens at 600px or below you change the grid-template-columns to 1fr for the element .row to get a 1-column layout.对于 600px 或以下的屏幕,您将元素.rowgrid-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
  4. To maintain the normal block-level-behavior you add 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. Flexbox 将允许您将文本垂直居中。
  5. To vertical center the text you have to align it to the main-axis ( 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.

相关问题 如何让我的 2 列 div 在不同的屏幕尺寸下以相等的宽度和长度堆叠在一起? - How to get my 2 column divs to stack on top of each other at equal width and length at different screen sizes? 如何使我的块引用响应不同的屏幕尺寸? - How do i make my blockquote responsive to different screen sizes? 如何将这些div彼此堆叠? - How do I stack these divs on top of each other? 如何在div框内彼此堆叠div? - How do I stack divs on top of each other in a form box? 如何将不同的 div 堆叠在一起(应该是响应式的) - How to stack different divs on each other (should be responsive) 我如何垂直对齐和堆叠我的选项卡和网格 div 以整齐地堆叠在彼此之上? - How do I vertically align and stack both my tabs and grid divs to stack neatly on top of each other? 如何获得使用不同屏幕尺寸或缩放时不会移动的div - How do I get divs that won't move upon using different screen sizes or zooming 如何在页面中心将这些div堆叠在一起? - How do I get these divs stack on top of each other right in the center of the page? 如何将两个水平div堆叠在一起以便移动? - How do I get two horizontal divs to stack on top of each other for mobile? 如何创建可根据不同手机尺寸调整大小的响应式图像? - How do I create responsive images that resize with different mobile phone sizes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM