【问题标题】:Flex Box Items responsively but aligning to the rightFlex Box Items 响应式但向右对齐
【发布时间】:2020-07-18 21:57:44
【问题描述】:

我正在尝试在响应式布局中使用弹性框。对于大屏幕尺寸,我已经能够使用 flex。但是对于小屏幕尺寸,在不改变布局的情况下,我想要这样。 1) 第 4 块在最左边 2) Block 2 在 Block 4 的右边 3) Block 3 和 Block 4 在 Block 2 下方对齐。这样 Block 4 下方没有任何东西。

我的标记是这样的:

<div id="main">
    <div class ="greenbox" style="background-color:lightgreen;">Green BLOCK 1 with more content.</div>
  <div class ="bluebox" style="background-color:lightblue;">BLUE BLOCK 2</div>  
<div class ="yellowbox" style="background-color:yellow;">yellow BLOCK 3</div> 
  <div class ="redbox" style="background-color:coral;">BLOCK 4 RED</div>
</div>

我的初学者 CSS 是:

#main {
  border: 1px solid black; 
  display: flex;
  align-content: flex-start

  align-items: center;
}

#main div {
padding: 10px;
}

.redbox {
order: -1;
}

【问题讨论】:

标签: css flexbox alignment


【解决方案1】:

您可以将CSS-Grid 用于狭窄的视口。我用line-based placement 来定位你的盒子。

我不确定你想把盒子放在哪里。所以这里是一个任意定位的代码示例:

.box {
  box-shadow: inset 0 0 1em rgba(0, 0, 0, 0.4);
  padding: 1em;
  color: #FFF;
  font-family: sans-serif;
}

.box--green {
  background-color: #3D9970;
}

.box--blue {
  background-color: #0074D9;
}

.box--yellow {
  background-color: #FFDC00;
}

.box--red {
  background-color: #FF4136;
}

/* 
 * position your boxes wherever you want in your grid
 */

#main {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* 4 columns */
  grid-template-rows: repeat(2, 1fr); /* 2 rows */
}

.box--1 {
  grid-area: 1 / 1 / 2 / 3;
}

.box--2 {
  grid-area: 1 / 3 / 2 / 5;
}

.box--3 {
  grid-area: 2 / 3 / 3 / 4;
}

.box--4 {
  grid-area: 2 / 4 / 3 / 5;
}

/* 
 * use flex positioning (and get rid of the grid) on bigger screens 
 */

@media screen and (min-width: 960px) {
  #main {
    border: 1px solid black;
    display: flex;
    align-content: flex-start;
    align-items: center;
  }
  #main div {
    padding: 10px;
  }
  .redbox {
    order: -1;
  }
}
<main id="main">

  <div class="box box--1 box--green">BLOCK 1 - with more content</div>
  <div class="box box--2 box--blue">BLOCK 2</div>
  <div class="box box--3 box--yellow">BLOCK 3</div>
  <div class="box box--4 box--red">BLOCK 4</div>

</main>

【讨论】:

  • 这看起来相当不错。我不确定 Grid,因为它的浏览器兼容性不如 Flexbox。但我真的很喜欢你的布局方式和易于阅读的方式。
  • 兼容性很好 (caniuse.com/#feat=css-grid)。这都是关于 IE11 的,但这不应该阻止我们。谷歌备用。使用您习惯于旧浏览器的旧技术提供体面的布局很容易 - 然后为较新的浏览器使用网格。有一些很好的关于网格后备的教程。 Rachel Andrew 就该主题写了一些不错的文章。
  • 如何扩展 Box 1,使绿色延伸到第 2 行,并占据第 1 列和第 2 列。现在那里没有颜色?有可能吗?
  • 是的,当然有可能:只需像这样更改行尾的值:.box--1 {grid-area: 1 / 1 / 3 / 3;} 在网格中,您可以随意放置框。请参阅我添加的链接,关于基于行的放置。您只需为列和行添加从哪里开始到哪里结束。此链接可能对您有所帮助:css-tricks.com/snippets/css/complete-guide-grid
  • 您好,谢谢。这个网格的东西很有趣。但现在我有一个技巧后续问题。如何使用“grid-auto-columns: min-content”。我只希望将该设置应用于块 3。我希望块 1 和块 2 不适合最小内容。当我在父级上设置它时,一切都设置为最小内容。
【解决方案2】:

首先,从 html 中删除您的样式代码并将它们放入 css 文件中。这是出于使用 css 文件目的和更清洁的 html 文件的原因。

回到你的问题

在内部 CSS 中严格使用 @media query 进行布局,以平滑地从大屏幕调整到小屏幕,反之亦然。

通过移动优先哲学设计开始您的代码,然后为@media screens 选择一个断点,最后为每个“您的屏幕需求”应用不同的设计布局其中之一。

对于移动优先的设计,请使用:

.block1 {
     display: flex;
     flex-direction: flex-left;
     }

 .boxright2 {
      display: flex;
      flex-direction: flex-right;
    }
  .block3 .block4 {
    display: flex;
    flex-direction: row;
    Float: clear;
     }

然后使用以下代码作为断点,并根据此处提供的视口添加或减小每个项目的大小:

@media screen and (min-width:768px) {
   //your code goes here for tablet
   }

@media screen and (min-width: 1024px) {

//your code goes here for larger screen like desktop and laptop
}

P.S. play with them you will figure it out later. 

【讨论】:

  • 让我纠正自己使用 display: flex-start;显示:弯曲端;用上面的替换那些 display: flex-left 和 right 属性然后休息是你的任务
猜你喜欢
  • 2017-10-06
  • 2021-05-16
  • 2019-01-16
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
相关资源
最近更新 更多