Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Getting Creative with CSS Grid


๐Ÿ“š Getting Creative with CSS Grid


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Grids don't have to be boring! You can easily spice up a classic 12x12 grid with very few lines of CSS โ€” it's time to master grid-area!

First, let's create a <figure>-tag with a class:

<figure class="ui-bubble-grid">
 <!--IMAGES-->
</figure>

Within that tag, add images โ€” I'm using:

<img src="https://source.unsplash.com/random/300x300?sig=1" 
alt="TEXT" width="300" height="300">

Replace the sig=1 with sig=2 etc. for more random images.

Next, let's add a simple 12x12 grid in CSS:

:where(.ui-bubble-grid) {
  all: unset;
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(12, 1fr);
}

Let's also add some nested default styles for images:

img {
  background: #CCC;
  height: auto;
  object-fit: cover;
  width: 100%;
}

This will ensure the images take up all of the assigned "grid-space".

Let's see what we have:

Initial

Note: For the grid-view, select the <figure>-tag in Dev Tools, and click on the "grid"-button:

Dev Tools

OK, so not much going on yet. Each image automatically fills up one grid-cell.

Let's change that with grid-area:

&:nth-child(1) { grid-area: 1 / 1 / 7 / 7; }
&:nth-child(2) { grid-area: 5 / 5 / 11 / 11; }
&:nth-child(3) { grid-area: 9 / 3 / 13 / 7; }
&:nth-child(4) { grid-area: 3 / 9 / 7 / 13; }
&:nth-child(5) { grid-area: 8 / 1 / 11 / 4; }
&:nth-child(6) { grid-area: 2 / 8 / 4 / 10; }
&:nth-child(7) { grid-area: 10 / 10 / 13 / 13; }
&:nth-child(8) { grid-area: 1 / 11 / 3 / 13; }

โ€ฆ and we get:

Grid Area

Wait! What? Let's dive into that! grid-area is short for:

  • grid-row-start
  • grid-column-start
  • grid-row-end
  • grid-column-end

Note: If you find grid-area hard to read, you can use grid-column and grid-row instead.

Now, if we look at the first image, it has this code:

{ grid-area: 1 / 1 / 7 / 7; }

We start at the first row (1) and the first column (1).

We want the image to take up 6x6 cells, so the end row and end column are 7, as 7 - 1 = 6.

For the next image, we want to layer it on top of the first image (bottom right) with a slight offset. Looking at the grid, that'll be column 5 and row 5. As we also want the next image to be 6x6, the CSS is:

{ grid-area: 5 / 5 / 11 / 11; }

Now, I encourage you to study the other grid-area-parts I made. Play around with them, move stuff around: make your own grid!

When you're ready, let's add a border-radius: 50% to the img in CSS, and we get:

Border Radius

Nice! Now, let's de-select the grid-preview in Dev Tools and see what we've created:

Bubble Grid

I like it! But what happens if you add more images than you created grid-area-parts for?

CSS grid will just find the first empty grid-cell, then the next etc.:

More images

What about responsiveness? The grid is resizable, but some of the images might get too small on phones. If you want โ€” add a @media or @container-query, and then simply change the grid-area-parts for that breakpoint.

Demo

Here's a CodePen you can fork and play with:

...



๐Ÿ“Œ CSS Grid: Moving From CSS Frameworks To CSS Grid (2018 and beyond)


๐Ÿ“ˆ 48.47 Punkte

๐Ÿ“Œ CSS Grid Handbook โ€“ Complete Guide to Grid Containers and Grid Items


๐Ÿ“ˆ 42.67 Punkte

๐Ÿ“Œ Getting Creative with CSS Grid


๐Ÿ“ˆ 38.42 Punkte

๐Ÿ“Œ How to Build a Grid to List Toggle using CSS Grid and JavaScript


๐Ÿ“ˆ 31.31 Punkte

๐Ÿ“Œ Post-Modern CSS (In-Depth on CSS Grid, Flexbox and Other New Properties)


๐Ÿ“ˆ 28.52 Punkte

๐Ÿ“Œ Stylify CSS: Automagic CSS bundles splitting into CSS layers in Astro.build


๐Ÿ“ˆ 25.74 Punkte

๐Ÿ“Œ A BRIEF REVIEW OF CSS CASCADING, CSS SELECTORS and CSS SPECIFICITY.


๐Ÿ“ˆ 25.74 Punkte

๐Ÿ“Œ Choosing the Right CSS Approach: Tailwind CSS vs Bootstrap vs Vanillaย CSS


๐Ÿ“ˆ 25.74 Punkte

๐Ÿ“Œ Ukraine Power Grid Attack Exposes US Power Grid Security Gaps (July 19, 2016)


๐Ÿ“ˆ 22.73 Punkte

๐Ÿ“Œ Ukraine Power Grid Attack Exposes US Power Grid Security Gaps (July 19, 2016)


๐Ÿ“ˆ 22.73 Punkte

๐Ÿ“Œ Joomla Responsive Grid For Articles Grid 3.4.5 SQL Injection


๐Ÿ“ˆ 22.73 Punkte

๐Ÿ“Œ GRID 3 heiรŸt einfach nur GRID und hat ein Datum: Schaut jetzt den Reveal-Trailer


๐Ÿ“ˆ 22.73 Punkte

๐Ÿ“Œ How To Enhance AG Grid with Avatars: Building a Collaborative Grid with React and Ably


๐Ÿ“ˆ 22.73 Punkte

๐Ÿ“Œ Creative Article Grid Using Flex


๐Ÿ“ˆ 22.28 Punkte

๐Ÿ“Œ Creative Creative Software AutoUpdate Engine ActiveX Control ctsueng.ocx memory corruption


๐Ÿ“ˆ 21.84 Punkte

๐Ÿ“Œ Get creative with a year of Adobe Creative Cloud for $40 a month


๐Ÿ“ˆ 21.84 Punkte

๐Ÿ“Œ Creative Zen Air SXFI & Creative Zen Hybrid SXFI: Neue TWS- und Over-Ear-Kopfhรถrer


๐Ÿ“ˆ 21.84 Punkte

๐Ÿ“Œ Why CSS Grid Is A Game Changer For Web Design


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Unlocking the Power of CSS Grid Layout


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Solving Layout Problems With CSS Grid and Friends


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ CSS Grid for UI Layouts


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Inspired by CSS Grid


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ A Dev's Guide to CSS Grid


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Google Chrome 57 Launches with CSS Grid Layout, Improved Add to Home Screen


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Chrome 57 Arrives With CSS Grid Layout and API Improvements


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Tour the latest features of the CSS Grid Inspector, July 2017


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Powerful New Additions to the CSS Grid Inspector in Firefox Nightly


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Building a Pinterest Clone Using CSS Grid


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ How To Build A Magazine Layout With CSS Grid Areas


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Learn CSS Grid โ€œThe Easy Wayโ€!


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ How to Adjust Columns with CSS Grid for Responsiveness


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ DevTools Tips: How to inspect CSS grid


๐Ÿ“ˆ 19.94 Punkte

๐Ÿ“Œ Bento Grid layout responsivo em 40 linhas de CSS๐Ÿฅข ๐Ÿฑ


๐Ÿ“ˆ 19.94 Punkte











matomo