Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Horizontal Scrolling using CSS Grid
Jennifer Bland
Jennifer Bland

Posted on • Originally published atjenniferbland.com

     

Horizontal Scrolling using CSS Grid

In this previous article, I showedhow to do horizontal scrolling using flexbox. I was recently asked how to do the same scrolling but using CSS Grid instead. This article shows you how to do horizontal scrolling using CSS Grid.

Setting up my project

This project will contain the following files:

  • index.html

  • style.css

It will also include one folder calledimages. Inside this folder will be five images. The images in the folder are:

  • image1.jpg

  • image2.jpg

  • image3.jpg

  • image4.jpg

  • image5.jpg

Our basic website

Theindex.html file contains the code for our basic website. It will display our five images in a horizontal scroll.

Here is the contents of myindex.html file:

<!doctype html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><metahttp-equiv="X-UA-Compatible"content="ie=edge"><title>CSS Grid Horizontal Scroll</title><linkrel="stylesheet"href="style.css"></head><body><divclass="container"><imgsrc="images/image1.jpg"alt="image1"><imgsrc="images/image2.jpg"alt="image2"><imgsrc="images/image3.jpg"alt="image3"><imgsrc="images/image4.jpg"alt="image4"><imgsrc="images/image5.jpg"alt="image5"><!-- more images --></div></body></html>
Enter fullscreen modeExit fullscreen mode

Let's walk through this code. In thehead I have a link to mystyle.css file. I will cover this file later.

The body of the website contains adiv with a.class ofcontainer.

Inside the div are fiveimg elements that point to the five images located in theimages folder.

That is it for ourindex.html file.

Adding horizontal scrolling of images

The secret sauce of making the images scroll horizontally is our CSS code.

Here is the code in thestyle.css file:

.container{display:grid;grid-auto-columns:calc(100%-4rem);grid-auto-flow:column;grid-gap:16px;overflow-x:auto;}.containerimg{width:100%;height:auto;}
Enter fullscreen modeExit fullscreen mode

Let's walk through this code.

display: grid; - This sets the container element to be a grid container, which means that its child elements will be placed onto a grid of rows and columns.

grid-auto-columns: calc(100% - 4rem); - This sets the width of any additional columns that are created in the grid to be the full width of the container minus 4rem (which is the same as 64px if the root font size is 16px).

grid-auto-flow: column; - This specifies that new rows should be created as new columns, rather than as new rows.

grid-gap: 16px; - This sets the gap between the grid cells to be 16px.

overflow-x: auto; - This causes a horizontal scrollbar to be displayed if the content of the grid overflows the container element horizontally.

.container img { width: 100%; height: auto; } - This sets the width of all img elements that are children of the .container element to be 100% of their parent element's width, and sets their height to be automatically calculated based on their aspect ratio.

Working Code

Here is an animated gif showing our horizontal scroll.

Horizontal Scrolling using CSS Grid

Conclusion

Wanted to take the time to show how to do horizontal scrolling using CSS Grid. If you want to seehow to do this using Flexbox check out my other article.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a Senior Software Engineer. Google Developers Expert. Entrepreneur. Mountain Climber. Neil Diamond fanatic. MBA grad. World traveler.
  • Location
    Atlanta, GA
  • Work
    Senior Software Engineer
  • Joined

More fromJennifer Bland

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp