YouTube2Text

CSS Grid Course - The Only Grid Tutorial You'll Ever Need! — Transcript

by Coding2GO · 4,512 words · 641 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Have you ever struggled with CSS grid or
  2. 0:02the perfect solution you found online
  3. 0:03just seemed impossible to understand?
  4. 0:05Well, today that is going to change
  5. 0:07forever because in this video you will
  6. 0:09learn everything about CSS grid in only
  7. 0:1120 minutes.
  8. 0:13[music]
  9. 0:15When I first learned about grid, it
  10. 0:17seemed very complicated, but now I'm
  11. 0:18using it all the time to build modern
  12. 0:20and responsive layouts that look great
  13. 0:22on any device. No matter if I'm building
  14. 0:24an online shop or a creative portfolio,
  15. 0:26it always comes down to CSS grid. And in
  16. 0:28this video, I will explain all of it
  17. 0:29starting from the absolute basics all
  18. 0:31the way to advanced real world examples.
  19. 0:33So after watching this course, you will
  20. 0:35have the confidence to build any layout
  21. 0:36you can imagine. It all starts in HTML
  22. 0:39because for any type of layout, you will
  23. 0:40be working with a parent element that
  24. 0:42contains a few child elements. These
  25. 0:43make up your grid. We usually call the
  26. 0:46parent element grid container and all
  27. 0:48the child elements grid items. That is
  28. 0:50really all you need in HTML to start
  29. 0:52working with grid. In CSS, you start by
  30. 0:54addressing the grid container where you
  31. 0:56declare display grid. A grid needs
  32. 0:59columns and rows. If you don't declare
  33. 1:01the size or the number of columns and
  34. 1:02rows, the browser will just do it
  35. 1:04automatically. In this case, we have
  36. 1:05just one column and nine rows. But we
  37. 1:08can also change that by declaring how
  38. 1:10many columns and rows our grid should
  39. 1:11have. For the columns, you use the
  40. 1:13property grid template columns. Here,
  41. 1:15every value represents one column. For
  42. 1:18example, I write 200 pixels three times.
  43. 1:20That way I end up with three columns
  44. 1:22each being 200 pixels wide. Pretty
  45. 1:24simple. To see the grid layout better,
  46. 1:26we can go to the developer tools in the
  47. 1:28browser and click on this little icon
  48. 1:30over here. This is a Firefox feature
  49. 1:32that will show us the columns and rows
  50. 1:33with purple lines. That way it is easier
  51. 1:35to see what's going on. A lot of times
  52. 1:37you don't need to specify the rows of a
  53. 1:39grid as this adjusts automatically to
  54. 1:41the columns you specified. But if you
  55. 1:43want to, you can use grid template rows
  56. 1:46to do the same thing for the rows. Let's
  57. 1:48say 200 pixels three times again. Now we
  58. 1:51have this tic-tac-toe kind of layout.
  59. 1:53Another cool property you can use is the
  60. 1:54gap property. This one will create a gap
  61. 1:57between the columns and rows. So as you
  62. 1:59can see the grid template columns
  63. 2:00property determines not only how many
  64. 2:02columns the grid should have but also
  65. 2:04how big each individual column should
  66. 2:06be. That means we can easily change the
  67. 2:08size of one column by modifying these
  68. 2:10values. For example, 400 pixels. Then
  69. 2:13this column is a lot bigger than the
  70. 2:14others. And when you set up any grid
  71. 2:17layout, even a complicated one,
  72. 2:18adjusting the number and size of the
  73. 2:20columns is really all you need to do.
  74. 2:21But it's not always that easy because
  75. 2:23often times when we design a grid
  76. 2:25layout, these layouts can get very large
  77. 2:26and they will only fit properly on a
  78. 2:28desktop screen. And this is of course
  79. 2:30not very responsive. Which is why it is
  80. 2:32super important that you sketch out
  81. 2:34different versions of the same grid for
  82. 2:35all the different screen sizes. And a
  83. 2:37cool thing about grid is that it offers
  84. 2:38many tools that make everything
  85. 2:40responsive using only a few lines of
  86. 2:42code. But before I explain how to do
  87. 2:44that, let's quickly talk about this
  88. 2:45amazing tool I'm using right now. Miro
  89. 2:47is a great innovation workspace where
  90. 2:49you can brainstorm ideas with your team
  91. 2:51using a massive collection of templates
  92. 2:53which allow you to do basically anything
  93. 2:54you would need as a developer. That
  94. 2:56includes designing prototypes, planning
  95. 2:58processes using all sorts of diagrams
  96. 3:00that you might know from your computer
  97. 3:01science studies and collaborating with
  98. 3:03your team members. But there's more.
  99. 3:05What I personally love is how Miro
  100. 3:06brings all your tools together into a
  101. 3:08single workspace. That means you can
  102. 3:10import YouTube videos, Excel or Google
  103. 3:11Sheets, or even an entire AWS
  104. 3:14infrastructure. And if you ever work
  105. 3:15with someone that doesn't know anything
  106. 3:17about tech, like a client, investor, or
  107. 3:19business partner, then Miro is
  108. 3:21definitely the best option to visualize
  109. 3:23your idea and explain how your software
  110. 3:25works. That way, everyone can understand
  111. 3:27the process, no matter their background.
  112. 3:28There's so much more Miro can do, so I
  113. 3:30highly recommend that you try it out
  114. 3:32yourself by clicking the first link in
  115. 3:33the video description. Let's talk about
  116. 3:36how to improve the responsiveness of
  117. 3:37your grid. When setting up the columns
  118. 3:39and rows of your layout, you don't have
  119. 3:41to use these fixed pixel values. You
  120. 3:43could also make it a bit more responsive
  121. 3:45by using a unit called fraction. For
  122. 3:48example, if I want the grid to fill out
  123. 3:49the entire width of the website, and
  124. 3:52each column should have the same size.
  125. 3:54Then we apply 1 fr 1 fr 1f fr. Each
  126. 3:57column is one fraction wide. You can see
  127. 4:00how the grid stretches over the entire
  128. 4:01screen horizontally. And if I resize the
  129. 4:04window, the grid will resize
  130. 4:05automatically. So what do you think will
  131. 4:07happen if I also use fractions for the
  132. 4:09rows? So I say 1 fr 1f fr 1f fr on grid
  133. 4:13template rows. Here it is important to
  134. 4:15know that it will not cover the entire
  135. 4:16screen. Because fractions behave
  136. 4:18relative to the grid container, not to
  137. 4:20the window. So only when I assign a
  138. 4:23height to it, we can see the effect. A
  139. 4:25cool thing about fractions is that you
  140. 4:27can also use them relative to one
  141. 4:28another. So this column over here could
  142. 4:31also be two fractions wide. Then it will
  143. 4:33be two times the size of the others. And
  144. 4:35you can also mix different units within
  145. 4:37a grid. For example, let's say our
  146. 4:39website only has two elements, a sidebar
  147. 4:42and a main content. I will comment out
  148. 4:44the other elements for now. Then you
  149. 4:46could create a grid with only two
  150. 4:47columns using a fixed pixel value for
  151. 4:49the sidebar and one fraction for the
  152. 4:51main content. So the sidebar will always
  153. 4:54stay in place and keep its fixed size.
  154. 4:56But the main content will be flexible
  155. 4:58and responsive depending on the screen
  156. 5:00size. So fractions behave very similar
  157. 5:02to flex grow and flex rank that you
  158. 5:04might know from flexbox. Now let's get
  159. 5:05back to our previous example and learn
  160. 5:07more about alignment. Alignment can get
  161. 5:09a bit confusing if you don't listen
  162. 5:11closely to what comes next. Because in
  163. 5:13grid we have the alignment of the
  164. 5:14columns and rows, but we also have the
  165. 5:16alignment of each item in the cells. To
  166. 5:19show you this better, I made all columns
  167. 5:21and rows 300 pixels big, but I also
  168. 5:23assigned a height and width of 100
  169. 5:25pixels to the items. So now the items
  170. 5:28have a different size than their grid
  171. 5:29cell. You can see that if you take a
  172. 5:31look at the purple lines in Firefox. Now
  173. 5:33to handle the alignment within the
  174. 5:35cells, you can use justify items and
  175. 5:37align items. Here you have the values
  176. 5:39start, end, and center. Justify items
  177. 5:42handles the horizontal alignment, and
  178. 5:44align items handles the vertical
  179. 5:46alignment. If you set both to center,
  180. 5:48they will be centered perfectly. But
  181. 5:50then we also have justify content and
  182. 5:52align content. These are different
  183. 5:54properties. Let's see what they do.
  184. 5:56Justify content handles the alignment of
  185. 5:58the columns. So if I set this to center,
  186. 6:01it will center the grid horizontally
  187. 6:02within the grid container. Align content
  188. 6:05handles the alignment of the rows. If I
  189. 6:07set this one to center, it will center
  190. 6:08the grid vertically. But again, don't
  191. 6:10confuse the grid container with the
  192. 6:12viewport. If I change the size of the
  193. 6:14grid container, that will affect these
  194. 6:15properties too. The grid is only
  195. 6:17centered within the grid container, not
  196. 6:19on the entire website. All right, now
  197. 6:21you know the basics of creating columns
  198. 6:23and rows and how to align everything to
  199. 6:24your liking. But now it's time to
  200. 6:26explore a concept called implicit grids.
  201. 6:28So far, we have created a fixed amount
  202. 6:30of columns and rows because we knew how
  203. 6:32many columns and rows we needed. But
  204. 6:34what if you don't know the number of
  205. 6:35elements that make up your grid? What if
  206. 6:37you get your content from a database
  207. 6:39like in a search function for example?
  208. 6:41Then the number of search results can be
  209. 6:43different every time. How can you make a
  210. 6:45grid for that? Let's get back to our
  211. 6:47previous example and learn more about
  212. 6:48implicit grids in HTML. We can see how
  213. 6:51we have nine boxes. Let's see what
  214. 6:53happens if I add more elements. Then
  215. 6:55these two boxes will be down here. They
  216. 6:57are very small and the layout is
  217. 6:59inconsistent. To fix that, we need to
  218. 7:01prepare our grid by managing its
  219. 7:02automatic behavior. What should the grid
  220. 7:04do automatically? For that, we use the
  221. 7:06grid auto rows property. Grid auto rows
  222. 7:09controls the size of every automatically
  223. 7:11generated row like this one down here.
  224. 7:13We only accounted for three rows, but we
  225. 7:16need four rows to handle our unexpected
  226. 7:17elements. Here I would just apply the
  227. 7:19same value 300 pixels again. And now it
  228. 7:22looks very consistent again. And by the
  229. 7:24way, this property is actually the
  230. 7:25reason why I don't use grid template
  231. 7:27rows that much. We can completely remove
  232. 7:29it and just use grid auto rows to have
  233. 7:31the same layout. Now every row is
  234. 7:33automatic and only the columns are
  235. 7:35specified. Now in grid layouts, new
  236. 7:37elements are normally added in a new
  237. 7:39row. This has to do with the grid
  238. 7:40autoflow property. Its default value is
  239. 7:43row. But if you set this property to
  240. 7:45column, then every new element will be
  241. 7:47added in a new column. In that case, you
  242. 7:49would remove the grid template columns
  243. 7:51and use grid auto columns instead. Here
  244. 7:53I used 300 pixels again to have
  245. 7:55automated columns each being 300 pixels
  246. 7:58wide. And of course, if you have too
  247. 7:59many elements, this will result in
  248. 8:01overflowing elements. But still, I think
  249. 8:03you should know about this concept
  250. 8:04because you can use that if you want to
  251. 8:06create a website with horizontal
  252. 8:07scrolling like in a Netflix letter for
  253. 8:09example. Now after understanding the
  254. 8:11basic grid properties, it is time to
  255. 8:13learn something more complicated.
  256. 8:15Something that is very unique in CSS
  257. 8:16grid because technically everything we
  258. 8:18have covered so far can be achieved with
  259. 8:20flexbox as well. It just works a bit
  260. 8:22better with these responsive grid
  261. 8:23settings. But there's also a concept we
  262. 8:26call bento grids. These are grids where
  263. 8:28elements stretch across multiple columns
  264. 8:29or rows. They look like Japanese food
  265. 8:31boxes, which is where the name bento
  266. 8:33comes from. This is nearly impossible to
  267. 8:35achieve with flexbox, but it's very
  268. 8:37simple in CSS grid and it can lead to
  269. 8:39very awesome layouts. Here's how it
  270. 8:40works. Let's say we wanted to create
  271. 8:42this layout. Then you can probably see
  272. 8:44how we need a CSS grid for this. We use
  273. 8:46grid template columns and grid template
  274. 8:48rows to create four columns and two
  275. 8:50rows. For simplicity, they are all 200
  276. 8:52pixels big, but you could also use a
  277. 8:55more responsive unit like fractions, for
  278. 8:56example. I also use a gap of 1m to see
  279. 8:59the individual boxes better. Right now
  280. 9:01they are all displayed next to each
  281. 9:03other but we can define their exact
  282. 9:04position in the grid if we name them
  283. 9:06somehow. Each box needs a name so that
  284. 9:09we can identify it later. For that we
  285. 9:11use the grid area property. You can do
  286. 9:13this in CSS but then you would have to
  287. 9:15address every box individually. So to
  288. 9:17save a few lines of code you could also
  289. 9:19do this in HTML using the style
  290. 9:21attribute. This is usually done much
  291. 9:23faster and since I don't plan on
  292. 9:24overwriting these values we don't have
  293. 9:26to worry about specificity. The names
  294. 9:28for the grid area is just box one, box
  295. 9:30two, box three, and so on. Now that
  296. 9:33every box has a name for its grid area,
  297. 9:35we can now decide how big these grid
  298. 9:37areas should be. In other words, across
  299. 9:39how many columns or rows they should
  300. 9:40stretch. And for that, we use the
  301. 9:42property grid template areas. And this
  302. 9:45is really where all the magic happens.
  303. 9:47On that property, we can define strings.
  304. 9:49One string is one row in the grid.
  305. 9:52Remember this grid design over here is
  306. 9:53the goal. So in the first row we need
  307. 9:56box one, box two, box two again, and box
  308. 10:00three. Now we write another string. I'm
  309. 10:03going to do this in a new line, but we
  310. 10:04could also write everything in one line
  311. 10:05of code. In the second row of the grid,
  312. 10:07we need box one again, box 4, box 5, and
  313. 10:12box 5 again. And in the browser, that
  314. 10:14should already give us the first grid of
  315. 10:16the video. So for a beginner that might
  316. 10:18be a bit tricky to see, but the grid
  317. 10:19template areas property contains the
  318. 10:21information where exactly each box
  319. 10:23should go. Maybe you'll see it with a
  320. 10:25bit of video editing. Every string is
  321. 10:28one row and the spaces inside the
  322. 10:30strings separate the columns and every
  323. 10:32box has a grid area which serves as
  324. 10:33their identifier. The cool thing about
  325. 10:35this property is that we can now change
  326. 10:37the layout very easily by just changing
  327. 10:39these strings. For example, I can make
  328. 10:42box one very big by putting it here and
  329. 10:44here and make all the other boxes a bit
  330. 10:46smaller over here. And this will now
  331. 10:48give me a completely different layout by
  332. 10:50just changing one tiny thing. And you
  333. 10:53can probably see how powerful this can
  334. 10:54be when thinking about responsive web
  335. 10:56design. For a smaller display, we just
  336. 10:58have to come up with a design that is a
  337. 11:00bit thinner. That means we have to
  338. 11:02rearrange the grid areas so that all the
  339. 11:04boxes fit properly. Inside a media
  340. 11:06query, I changed the values for the grid
  341. 11:08template columns and grid template rows.
  342. 11:10We could use a 3x3 grid for example.
  343. 11:12Later I will show you how to make this
  344. 11:14completely automatic. And now in grid
  345. 11:16template areas I create a design that
  346. 11:18looks somewhat like this. We just
  347. 11:20rearranging the five grid areas for the
  348. 11:22five boxes so that we have a 3x3 layout.
  349. 11:25This is now a bit thinner and perfectly
  350. 11:27suited for tablet devices. And of course
  351. 11:30the same thing goes for mobile screens.
  352. 11:31I can do the same thing again but this
  353. 11:33time with only two columns and four
  354. 11:35rows. So depending on the screen size,
  355. 11:37we can always change the grid template
  356. 11:39areas property to create a grid layout
  357. 11:41that fits properly. But you might have
  358. 11:43noticed one annoying thing. We always
  359. 11:45need to change grid template columns and
  360. 11:47grid template rows. But I found a way
  361. 11:49where we don't need to change these
  362. 11:51properties. There's a solution where you
  363. 11:53only adjust the grid areas and the
  364. 11:54columns and rows will follow
  365. 11:56automatically because technically that
  366. 11:58information is already contained in grid
  367. 12:00template areas. So I will remove grid
  368. 12:02template columns and grid template rows
  369. 12:04everywhere and only at the beginning up
  370. 12:06here in my grid container. I will use
  371. 12:08different properties called grid autoc
  372. 12:10columns and grid auto rows. These
  373. 12:13properties will now create an implicit
  374. 12:15grid. That means here we define how big
  375. 12:17an automatically created column or row
  376. 12:19should be. They're all 200 pixels. By
  377. 12:22using these properties, we don't need to
  378. 12:23specify the columns and rows anymore
  379. 12:25since they will be created
  380. 12:26automatically. I'm not saying that this
  381. 12:28is the better way. If you really want to
  382. 12:30be 100% sure and you want more control
  383. 12:32over the individual columns and rows,
  384. 12:34then you will be better off with grid
  385. 12:35template columns and grid template rows
  386. 12:37in the media query. But if you prefer a
  387. 12:38shorter and more automatic way, then do
  388. 12:40it like this with an implicit grid. Now,
  389. 12:42there are a few things to consider going
  390. 12:44forward. Right now, I'm using a fixed
  391. 12:46unit. You could also use the unit em
  392. 12:48which is a bit better, but with both of
  393. 12:50these units, you're limiting the size of
  394. 12:51the boxes. If that is the goal, then
  395. 12:53that's of course okay. This could be
  396. 12:55useful for an image gallery, for
  397. 12:56example. But the moment you start
  398. 12:58filling these boxes with content, then
  399. 13:00things can get messy if you limit the
  400. 13:02size. This is why we have to make a
  401. 13:03decision. Do you want to hide
  402. 13:05overflowing content with overflow hidden
  403. 13:07and make sure the grid stays in place
  404. 13:08perfectly or do you want to have an
  405. 13:10adaptive grid that resizes itself based
  406. 13:12on the text content? For that, you can
  407. 13:14use the value auto or one fraction for
  408. 13:17the automatic rows. But what is the
  409. 13:19difference? The value auto is dependent
  410. 13:21on the text content. Here, if there's no
  411. 13:23content or padding, then there's no row.
  412. 13:26If there's more text content, the height
  413. 13:28of the row will adjust automatically.
  414. 13:30But I think this will make it really
  415. 13:31difficult to balance the layout because
  416. 13:33now the content has to be balanced
  417. 13:35perfectly to make this grid look good.
  418. 13:37The other option for an adaptive grid is
  419. 13:39the value one fraction. If you use one
  420. 13:41fraction, then all the rows, they're all
  421. 13:44the same size. And for me, that works a
  422. 13:45lot better. That means if I start
  423. 13:47filling the first row with a bit of
  424. 13:49content and it grows bigger, then the
  425. 13:51other rows will also grow accordingly.
  426. 13:53But just a general recommendation here,
  427. 13:55don't fill these bento grids with too
  428. 13:57much text. I believe these grids work
  429. 13:58way better with images and cool graphs
  430. 14:01because most of the time the goal is to
  431. 14:02make a product look awesome and not to
  432. 14:04present a big amount of information.
  433. 14:06Now, before we move on to my favorite
  434. 14:07way of using grid layouts, where we
  435. 14:09create a responsive layout for an online
  436. 14:11shop. Let's talk about a topic that
  437. 14:13shows how you can use grid in a
  438. 14:15completely unexpected but powerful way.
  439. 14:17Because so far, we've only used grid to
  440. 14:19create responsive layouts. But grid is
  441. 14:21actually more powerful than that because
  442. 14:23we can use the same properties to solve
  443. 14:25completely different problems too. For
  444. 14:27example, if you want to stack certain
  445. 14:28elements, like when you want to place
  446. 14:30text on top of an image or put a video
  447. 14:33behind a website section, then you would
  448. 14:35use a concept called grid stacking. It
  449. 14:37is entirely possible with the grid
  450. 14:39properties you already know. You just
  451. 14:40have to get a bit creative. Here's how
  452. 14:42it works. When you have a grid layout
  453. 14:44defined, you can target the child
  454. 14:45elements and use the properties grid row
  455. 14:48and grid column. These properties
  456. 14:50control how many columns or rows an
  457. 14:52element should span. For example, if I
  458. 14:53set grid column to 1/3, the element
  459. 14:57stretches across two columns because
  460. 14:59grid lines are count as 1 2 3. Now, why
  461. 15:02am I telling you this? Because we can
  462. 15:04use this exact concept to make chart
  463. 15:06elements overlap. Let's assign the same
  464. 15:08cell in the middle to our second chart
  465. 15:10by making it stretch from line two to
  466. 15:12line four. By default, that will push it
  467. 15:15into another row. So we also have to set
  468. 15:17the same grid row 1/2 and do that for
  469. 15:21both chart elements. Now you can see how
  470. 15:23they overlap. It is even clearer if I
  471. 15:25lower the opacity on one of them. So
  472. 15:27when two elements share the same grid
  473. 15:29cell, we essentially get the same
  474. 15:30behavior we want from absolute
  475. 15:32positioning. overlapping elements that
  476. 15:34are positioned relative to their parent.
  477. 15:36A common example for this would be to
  478. 15:38place text on top of an image. With
  479. 15:40absolute positioning, you would have a
  480. 15:41wrapper with position relative and a
  481. 15:43text container with position absolute
  482. 15:45and then use the top left, bottom right
  483. 15:47values to position it exactly where you
  484. 15:48want it. This works totally fine. But
  485. 15:51here's the same thing with grid
  486. 15:52stacking. Remove all of this and instead
  487. 15:54set the wrapper to display grid. This
  488. 15:57will automatically create a one by one
  489. 15:59grid without defining rows or columns.
  490. 16:01We don't need them. Now on the text
  491. 16:02container, set both grid column and grid
  492. 16:04row to start at line one and end at line
  493. 16:07two. meaning it covers the entire grid
  494. 16:09since this is only one cell. On the
  495. 16:12image, we do exactly the same grid row
  496. 16:14and grid column one two and that will
  497. 16:17make them overlap perfectly just like
  498. 16:19with absolute positioning but with the
  499. 16:21advantage that we now have access to all
  500. 16:23those fancy grid properties. For
  501. 16:25example, we can use place items to align
  502. 16:27the elements at the start, at the end,
  503. 16:29or even in the center without having to
  504. 16:31calculate top 50% and translating it
  505. 16:34back as you would have to do it in
  506. 16:35position absolute. Right now, we're
  507. 16:37stacking the CSS grid using grid row and
  508. 16:39grid column. But the cool thing is
  509. 16:41there's actually a shorter and more
  510. 16:43readable way which I prefer. Let's use
  511. 16:45that in the next example. Imagine you
  512. 16:46want to play a large video in the
  513. 16:48background of your header section.
  514. 16:49Here's my video element which has all
  515. 16:51the important attributes we need to make
  516. 16:53it play in the background. With absolute
  517. 16:55positioning, you would often have to set
  518. 16:56either the video or the text content to
  519. 16:58position absolute. That will work fine,
  520. 17:01but if the content size changes
  521. 17:02dynamically, you could run into some
  522. 17:04ugly overflow issues. But with grid
  523. 17:06stacking, you don't have to worry about
  524. 17:08that. Let's remove the absolute
  525. 17:09positioning and set the header to
  526. 17:11display grid. This time I will use grid
  527. 17:14template areas. I simply define one area
  528. 17:17and call it stack. Now on both the video
  529. 17:20and the text container, I assign that
  530. 17:22grid area stack. It is very readable and
  531. 17:25you can see how both elements stack in
  532. 17:27the browser. Now to control which
  533. 17:29element is on top, I just use the Z
  534. 17:31index property. The higher the value,
  535. 17:33the closer the element is to the viewer.
  536. 17:35And again, we can center everything with
  537. 17:37place items center. Very intuitive. All
  538. 17:40right. So far, you should already have a
  539. 17:42good understanding about the properties
  540. 17:43we have in CSS grid. But the most
  541. 17:45important grid concept is still missing.
  542. 17:48The one concept that is used in most
  543. 17:50real life applications. A concept I call
  544. 17:52grid wrapping, where the grid is able to
  545. 17:54adjust the amount of columns that fit on
  546. 17:56the screen at once. If there's not
  547. 17:57enough space, it will simply eliminate a
  548. 17:59column and put the items into the
  549. 18:01remaining columns. It is doing all of
  550. 18:03this automatically, responsively,
  551. 18:04without any media queries. And to learn
  552. 18:06about this concept more practically,
  553. 18:08let's implement it into this basic
  554. 18:10online shop layout. We want to create a
  555. 18:12wrapping grid that shows these products
  556. 18:14in a responsive way. Depending on the
  557. 18:16screen size, the grid will automatically
  558. 18:18add or remove columns so that the
  559. 18:20website looks great on any device. Let's
  560. 18:22take a look at what we have got so far.
  561. 18:24We have a basic website with a products
  562. 18:26list. These products are simple div
  563. 18:28containers with an image, a name, and a
  564. 18:30price. That's it. They have very basic
  565. 18:32starts. Display flex and flex direction
  566. 18:34column to have a vertical layout within
  567. 18:36the containers. The image has a height
  568. 18:38auto applied so that the height can
  569. 18:40resize properly if we change the size of
  570. 18:42the image using grid which we will do.
  571. 18:44So let's take a look at the grid layout
  572. 18:46in the product list. We have of course
  573. 18:48display grid. The columns are pretty
  574. 18:50basic just three columns each 300 pixels
  575. 18:53in the browser. That means this grid is
  576. 18:55not responsive. If I resize the window
  577. 18:58these columns will overflow. To
  578. 19:00calculate how many columns can fit
  579. 19:02automatically, we have to use a function
  580. 19:04called repeat. This function is pretty
  581. 19:06powerful. It takes two arguments. The
  582. 19:08first one determines how many times you
  583. 19:10want to repeat and the second one what
  584. 19:12value you want to repeat. For example, I
  585. 19:15can say repeat three times 300 pixels.
  586. 19:18Then that would be exactly the same as
  587. 19:20before just using the repeat function.
  588. 19:22If I say repeat four times, it will
  589. 19:24create four columns of 300 pixels. So
  590. 19:27far pretty simple. But we don't have to
  591. 19:29use a fixed number value here. We can
  592. 19:31also use the keyword auto fit. Then it
  593. 19:34will check automatically how many times
  594. 19:36we can repeat the value 300 pixels
  595. 19:38depending on how much space there is. So
  596. 19:40if I resize the browser window, you can
  597. 19:42already see how it will create and
  598. 19:44delete columns depending on the screen
  599. 19:45size. Pretty simple and yet very
  600. 19:48responsive. But we can also make it even
  601. 19:50more responsive by introducing fractions
  602. 19:52into this layout. You see, sometimes we
  603. 19:54have a screen size where there's not
  604. 19:56enough space for another column. But we
  605. 19:58do have some wide space that we could
  606. 19:59fill out. So at this screen size, we
  607. 20:01could make the remaining columns
  608. 20:03slightly bigger until we have enough
  609. 20:05space to create another column. For
  610. 20:06that, we have to adjust the second
  611. 20:08parameter. Here we provide a size range
  612. 20:11instead using the minmax function. The
  613. 20:13minimum value should be 300 pixels, but
  614. 20:16the max value should be one fraction. So
  615. 20:19essentially we're telling the browser
  616. 20:21try to auto fit as many columns next to
  617. 20:23each other as possible and each column
  618. 20:25should be between 300 pixels and one
  619. 20:27fraction wide. That way there is no wide
  620. 20:30space and we end up with a responsively
  621. 20:32wrapping grid where the columns can
  622. 20:34resize if needed. Pretty awesome. And
  623. 20:36with that you have just completed an
  624. 20:38entire deep dive into CSS grid. But this
  625. 20:41is only the beginning. If you're ready
  626. 20:43to move beyond layout and actually start
  627. 20:45building interactive websites, then it's
  628. 20:47time to learn JavaScript. I've created a
  629. 20:49full 8 hours JavaScript course, beginner
  630. 20:51friendly but fast-paced, so you can see
  631. 20:53results quickly by building hands-on
  632. 20:55projects. You will learn everything
  633. 20:57about programming in JavaScript, which
  634. 20:59will open the doors to all those
  635. 21:00libraries and frameworks that you plan
  636. 21:01to learn. So, if you want to learn React
  637. 21:03in the future or Angular or Vue.js, then
  638. 21:06you will have to learn JavaScript first.
  639. 21:08You can get my course on Udemy using the
  640. 21:10link in the video description. My name
  641. 21:12is Fabian and this was coding to go.

About this transcript

This page contains the full transcript of CSS Grid Course - The Only Grid Tutorial You'll Ever Need! by Coding2GO, generated from the public captions YouTube serves with the video. The transcript has 4,512 words across 641 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.