html - 如何在 html 中创建可变大小的 tile 布局

标签 html css layout sass

我正在尝试看看是否可以创建一个像下面这样的可变大小的图 block 布局,而无需任何中间包装器 divs

Xbox 350 movies and tv home screen

我首先尝试了一种 flexbox 方法,然后发现 flexbox 并不是为了让子元素跨行移动而设计的,然后是一个普通的 inline-blockfloat: left 方法,但存在完全相同的问题。我无法让 block 移动到不同的行并进入它们之间的空间。

我也在寻找一种无 javascript 的方法。所以不使用 Masonry 之类的东西。

Codepen

对我来说很重要的一点是,我正在尝试一般解决这个问题,这意味着虽然我们当然可以找到一种方法来创建这个特定布局,我更担心弄清楚如何创建这种通用类型的布局,我可能无法提前知道图 block 的确切方向、顺序或大小。

这是所需的 html(jade):

.xbox-tiles
    .tile 1 x 1
    .tile 1 x 1
    .big-tile 2 x 2
    .tile 1 x 1
    .tile 1 x 1
    .tile 1 x 1
    .tile 1 x 1
    .tile 1 x 1
    .tile 1 x 1

sass:

@import "bourbon"

%tile
    @include flex(0)
    @include flex-basis(auto)

    // display: inline-block
    // float: left

    box-sizing: border-box


.xbox-tiles
    @include display(flex)
    @include flex-direction(column)
    @include flex-flow(wrap)

    height: 90vh
    width: 90vw

    .tile
        height: 33.33%
        width: 25%
        @extend %tile

    .big-tile
        height: 66.66%
        width: 50%
        @extend %tile

最佳答案

在对 this question 发表评论之后我能够制作这个 codepen它使用简单的 float ,不需要 flexbox,换句话说,将所有小方 block 向左浮动,将大方 block 向右浮动:

@import "bourbon"

%tile    
    box-sizing: border-box

.xbox-tiles

    height: 90vh
    width: 90vw

    .tile
        float: left
        height: 33.33%
        width: 25%
        @extend %tile

    .big-tile
        float: right
        height: 66.66%
        width: 50%
        @extend %tile

body {
  background-color: grey;
}

.xbox-tiles .tile, .xbox-tiles .big-tile {
  box-sizing: border-box;
  border: 2px solid white;
  margin: 0;
}

.xbox-tiles {
  height: 90vh;
  width: 90vw;
  background-color: cyan;
}
.xbox-tiles .tile {
  float: left;
  height: 33.33%;
  width: 25%;
}
.xbox-tiles .big-tile {
  float: right;
  height: 66.66%;
  width: 50%;
}
<div class="xbox-tiles">
  <div class="tile">1 x 1 - 1</div>
  <div class="tile">1 x 1 - 2</div>
  <div class="big-tile">2 x 2 - 3</div>
  <div class="tile">1 x 1 - 4</div>
  <div class="tile">1 x 1 - 5</div>
  <div class="tile">1 x 1 - 6</div>
  <div class="tile">1 x 1 - 7</div>
  <div class="tile">1 x 1 - 8</div>
  <div class="tile">1 x 1 - 9</div>
</div>

关于html - 如何在 html 中创建可变大小的 tile 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966237/

相关文章:

javascript - 单击按钮将背景视频(环境视频)变成可播放的视频

jquery - 使用 Jquery 过滤表行

html - 使外部div的宽度自动适合内部div

php - 如何在 php 中允许特殊标签?

css - 试图理解 <div> 元素

php - Joomla 自定义模板覆盖不起作用

layout - 将一个 div 剪切为 2 个背景图像,然后使用 CSS3 在顶部分层渐变

android - 如何使具有多个子布局的 RelativeLayout 的 ScrollView 充满屏幕?

jquery - 使用 nth-child 的问题

javascript - 如何在单击 JavaScript/Angular 时切换字体大小