【问题标题】:Jump from horizontal layout to vertical layout responsively without fixed breakpoint响应式从水平布局跳转到垂直布局,无需固定断点
【发布时间】:2016-11-23 21:39:53
【问题描述】:

有很多方法可以用 CSS(inline-block、flex 等)实现这种水平布局:

[item 1] [item 2] [item 3] [item 4]

如果项目不能全部放在一行中,这些方法通常会换行到多行,每行有多个项目。

我希望布局跳到所有屏幕尺寸的每行一个项目,其中项目不适合单行:

[item 1]
[item 2]
[item 3]
[item 4]

这是否可以通过 CSS 实现,无需:

  • javascript
  • 添加一个固定宽度的断点(这依赖于知道项目的确切宽度)。

【问题讨论】:

  • 但是你(或者更确切地说,CSS)肯定会知道它们什么时候不合适?您要么使用 % 宽度,在这种情况下,它们将始终适合,或者您正在使用 fixed with,在这种情况下,计算响应断点应该很容易?
  • 应该总是至少有一个绝对最大宽度,因为你不希望你的外部容器太宽 - 如果你有一些宽度设置,在某个地方,你通常可以计算出您需要基于此。
  • 项目是基于文本的。不仅文本的宽度因浏览器而异,而且文本是动态的,并且会因页面而异。

标签: html css


【解决方案1】:

像这样?

/*  SECTIONS  */
.section {
	clear: both;
	padding: 0px;
	margin: 0px;
}

/*  COLUMN SETUP  */
.col {
	display: block;
	float:left;
	margin: 1% 0 1% 1.6%;
}
.col:first-child { margin-left: 0; }

/*  GROUPING  */
.group:before,
.group:after { content:""; display:table; }
.group:after { clear:both;}
.group { zoom:1; /* For IE 6/7 */ }
/*  GRID OF FOUR  */
.span_4_of_4 {
	width: 100%;
}
.span_3_of_4 {
	width: 74.6%;
}
.span_2_of_4 {
	width: 49.2%;
}
.span_1_of_4 {
	width: 23.8%;
}

/*  GO FULL WIDTH BELOW 480 PIXELS */
@media only screen and (max-width: 480px) {
	.col {  margin: 1% 0 1% 0%; }
	.span_1_of_4, .span_2_of_4, .span_3_of_4, .span_4_of_4 { width: 100%; }
}
<div class="section group">
	<div class="col span_1_of_4">
	1 of 4
	</div>
	<div class="col span_1_of_4">
	1 of 4
	</div>
	<div class="col span_1_of_4">
	1 of 4
	</div>
	<div class="col span_1_of_4">
	1 of 4
	</div>
</div>

【讨论】:

  • 这增加了一个固定宽度的断点,我特别提到要避免。它适应的点应该基于内容的(未知)长度。
  • .. 你现在已经设法忽略了both我的问题中特别提到的条件。
猜你喜欢
  • 2017-03-02
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 2015-07-27
相关资源
最近更新 更多