masonry-auto-flow - CSS: Cascading Style Sheets 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The masonry-auto-flow CSS property modifies how items are placed when using masonry in CSS Grid Layout.

Syntax

/* Keyword values */
masonry-auto-flow: pack;
masonry-auto-flow: next;
masonry-auto-flow: ordered;
masonry-auto-flow: definite-first;
masonry-auto-flow: next ordered;

/* Global values */
masonry-auto-flow: inherit;
masonry-auto-flow: initial;
masonry-auto-flow: unset;

This property may take one of two forms:

  • A single keyword: one of pack, next, definite-first, or ordered
  • Two keywords, for example next ordered.

Values

pack
As per the default masonry algorithm, items will be placed into the track with the most room.
next
Items will be placed one after the other in the grid axis.
definite-first
Display as in the default masonry algorithm, placing items with a definite place first before placing other masonry items.
ordered
Ignore any items with a definite placement, and place everything according to order-modified document order.

Formal definition

Initial valuepack
Applies toGrid containers with masonry layout
Inheritedno
Computed valueas specified
Animation typediscrete

Formal syntax

[ pack | next ] || [ definite-first | ordered ]

Examples

Using the next keyword

HTML

<div id="grid">
  <div id="item1"></div>
  <div id="item2"></div>
  <div id="item3"></div>
  <div id="item4"></div>
  <div id="item5"></div>
</div>
<select id="flow">
  <option value="pack">pack</option>
  <option value="next">next</option>
</select>

CSS

#grid {
  height: 200px;
  width: 200px;
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: masonry;
  masonry-auto-flow: pack;
}

#item1 {
  background-color: lime;
  height: 2em
}

#item2 {
  background-color: yellow;
}

#item3 {
  background-color: blue;
  height: 3em;
}

#item4 {
  background-color: red;
  height: 2.5em;
}

#item5 {
  background-color: aqua;
  height: 4em;
}
const selectElem = document.querySelector('select');

function changeMasonryFlow() {
  var grid = document.getElementById("grid");
  var direction = document.getElementById("flow");
  var masonryAutoFlow = direction.value === "pack" ? "pack" : "next";

  grid.style.masonryAutoFlow = masonryAutoFlow;
}

selectElem.addEventListener('change', changeMasonryFlow);

Result

Specifications

SpecificationStatusComment
CSS Grid Layout Module Level 3
The definition of 'masonry-auto-flow' in that specification.
Editor's DraftInitial definition

Browser compatibility

BCD tables only load in the browser

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:83 次

字数:7243

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文