如何使用Grid在CSS中创建时间表?

huangapple go评论68阅读模式
英文:

How to create timetable in CSS using Grid?

问题

        <div class="Grid">
            <div class="Grid-Column">
                <div class="column">1</div>
                <div class="column">2</div>
                <div class="column">3</div>
                <div class="column">4</div>
                <div class="column">5</div>
                <div class="column">6</div>
            </div>

            <div class="Grid-Rows">
                <div class="row">星期一</div>
                <div class="row">星期二</div>
                <div class="row">星期三</div>
                <div class="row">星期四</div>
                <div class="row">星期五</div>
            </div>

            <div class="Grid-Element">
                <div>A</div>
                <div>B</div>
                <div>C</div>
                <div>E</div>
                <div>F</div>
            </div>
        </div>
英文:

I am trying to recreate this picture using CSS grid.
However I do not know how to properly create elements that span across multiple colums.

如何使用Grid在CSS中创建时间表?

This is the code I have right now.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    &lt;div class=&quot;Grid&quot;&gt;
        &lt;div class=&quot;Grid-Column&quot;&gt;
            &lt;div class=&quot;column&quot;&gt;1&lt;/div&gt;
            &lt;div class=&quot;column&quot;&gt;2&lt;/div&gt;
            &lt;div class=&quot;column&quot;&gt;3&lt;/div&gt;
            &lt;div class=&quot;column&quot;&gt;4&lt;/div&gt;
            &lt;div class=&quot;column&quot;&gt;5&lt;/div&gt;
            &lt;div class=&quot;column&quot;&gt;6&lt;/div&gt;
        &lt;/div&gt;

        &lt;div class=&quot;Grid-Rows&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;Monday&lt;/div&gt;
            &lt;div class=&quot;row&quot;&gt;Tuesday&lt;/div&gt;
            &lt;div class=&quot;row&quot;&gt;Wednesday&lt;/div&gt;
            &lt;div class=&quot;row&quot;&gt;Thursday&lt;/div&gt;
            &lt;div class=&quot;row&quot;&gt;Friday&lt;/div&gt;
        &lt;/div&gt;

        &lt;div class=&quot;Grid-Element&quot;&gt;
            &lt;div&gt;A&lt;/div&gt;
            &lt;div&gt;B&lt;/div&gt;
            &lt;div&gt;C&lt;/div&gt;
            &lt;div&gt;E&lt;/div&gt;
            &lt;div&gt;F&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

我不确定这是否是最佳方法。无论如何,请尝试:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    .Grid {
      width: 700px;
      height: 500px;
      border: 2px solid red;
      display: grid;
      grid-template-columns: repeat(7, 1fr);
      grid-template-rows: repeat(6, 1fr);
    }

    .pondeli {
      grid-row-start: 2;
      grid-row-end: 3;
    }

    .Utery {
      grid-row-start: 3;
      grid-row-end: 4;
    }

    .Streda {
      grid-row-start: 4;
      grid-row-end: 5;
    }

    .Ctvrtek {
      grid-row-start: 5;
      grid-row-end: 6;
    }

    .Patek {
      grid-row-start: 6;
      grid-row-end: 7;
    }

    .a {
      grid-row-start: 3;
      grid-row-end: 4;
      grid-column-start: 3;
      grid-column-end: 5;
    }

    .b {
      grid-row-start: 3;
      grid-row-end: 4;
      grid-column-start: 7;
      grid-column-end: 8;
    }

    .c {
      grid-row-start: 4;
      grid-row-end: 5;
      grid-column-start: 4;
      grid-column-end: 5;
    }

    .d {
      grid-row-start: 4;
      grid-row-end: 5;
      grid-column-start: 5;
      grid-column-end: 6;
    }

    .e {
      grid-row-start: 5;
      grid-row-end: 6;
      grid-column-start: 2;
      grid-column-end: 3;
    }

    .f {
      grid-row-start: 5;
      grid-row-end: 6;
      grid-column-start: 5;
      grid-column-end: 7;
    }

&lt;!-- language: lang-html --&gt;

    &lt;div class=&quot;Grid&quot;&gt;
      &lt;div&gt;&lt;/div&gt;
      &lt;div&gt;1&lt;/div&gt;
      &lt;div&gt;2&lt;/div&gt;
      &lt;div&gt;3&lt;/div&gt;
      &lt;div&gt;4&lt;/div&gt;
      &lt;div&gt;5&lt;/div&gt;
      &lt;div&gt;6&lt;/div&gt;

      &lt;div class=&quot;pondeli&quot;&gt;Pondeli&lt;/div&gt;
      &lt;div class=&quot;Utery&quot;&gt;Utery&lt;/div&gt;
      &lt;div class=&quot;Streda&quot;&gt;Streda&lt;/div&gt;
      &lt;div class=&quot;Ctvrtek&quot;&gt;Ctvrtek&lt;/div&gt;
      &lt;div class=&quot;Patek&quot;&gt;&lt;/div&gt;

      &lt;div class=&quot;a&quot;&gt;A&lt;/div&gt;
      &lt;div class=&quot;b&quot;&gt;B&lt;/div&gt;
      &lt;div class=&quot;c&quot;&gt;C&lt;/div&gt;
      &lt;div class=&quot;d&quot;&gt;D&lt;/div&gt;
      &lt;div class=&quot;e&quot;&gt;E&lt;/div&gt;
      &lt;div class=&quot;f&quot;&gt;F&lt;/div&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;
英文:

I'm not sure if this is the best method. Anyway, try it:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.Grid {
  width: 700px;
  height: 500px;
  border: 2px solid red;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(6, 1fr);
}

.pondeli {
  grid-row-start: 2;
  grid-row-end: 3;
}

.Utery {
  grid-row-start: 3;
  grid-row-end: 4;
}

.Streda {
  grid-row-start: 4;
  grid-row-end: 5;
}

.Ctvrtek {
  grid-row-start: 5;
  grid-row-end: 6;
}

.Patek {
  grid-row-start: 6;
  grid-row-end: 7;
}

.a {
  grid-row-start: 3;
  grid-row-end: 4;
  grid-column-start: 3;
  grid-column-end: 5;
}

.b {
  grid-row-start: 3;
  grid-row-end: 4;
  grid-column-start: 7;
  grid-column-end: 8;
}

.c {
  grid-row-start: 4;
  grid-row-end: 5;
  grid-column-start: 4;
  grid-column-end: 5;
}

.d {
  grid-row-start: 4;
  grid-row-end: 5;
  grid-column-start: 5;
  grid-column-end: 6;
}

.e {
  grid-row-start: 5;
  grid-row-end: 6;
  grid-column-start: 2;
  grid-column-end: 3;
}

.f {
  grid-row-start: 5;
  grid-row-end: 6;
  grid-column-start: 5;
  grid-column-end: 7;
}

<!-- language: lang-html -->

&lt;div class=&quot;Grid&quot;&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
  &lt;div&gt;6&lt;/div&gt;

  &lt;div class=&quot;pondeli&quot;&gt;Pondeli&lt;/div&gt;
  &lt;div class=&quot;Utery&quot;&gt;Utery&lt;/div&gt;
  &lt;div class=&quot;Streda&quot;&gt;Streda&lt;/div&gt;
  &lt;div class=&quot;Ctvrtek&quot;&gt;Ctvrtek&lt;/div&gt;
  &lt;div class=&quot;Patek&quot;&gt;&lt;/div&gt;

  &lt;div class=&quot;a&quot;&gt;A&lt;/div&gt;
  &lt;div class=&quot;b&quot;&gt;B&lt;/div&gt;
  &lt;div class=&quot;c&quot;&gt;C&lt;/div&gt;
  &lt;div class=&quot;d&quot;&gt;D&lt;/div&gt;
  &lt;div class=&quot;e&quot;&gt;E&lt;/div&gt;
  &lt;div class=&quot;f&quot;&gt;F&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 22:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定