search
HomeWeb Front-endHTML TutorialHow to create a responsive image grid layout using HTML and CSS

How to create a responsive image grid layout using HTML and CSS

Oct 27, 2023 am 10:26 AM
csshtmlResponsivepicturegrid layout

How to create a responsive image grid layout using HTML and CSS

How to create a responsive image grid layout using HTML and CSS

In today’s Internet era, images occupy an important part of web content. In order to display various types of images, we need an effective and beautiful grid layout. In this article, we will learn how to create a responsive image grid layout using HTML and CSS.

First, we will create a basic structure using HTML. The following is the sample code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>响应式图片网格布局</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="grid-container">
        <div class="grid-item">
            <img src="/static/imghwm/default1.png"  data-src="image1.jpg"  class="lazy" alt="图片1">
        </div>
        <div class="grid-item">
            <img src="/static/imghwm/default1.png"  data-src="image2.jpg"  class="lazy" alt="图片2">
        </div>
        <div class="grid-item">
            <img src="/static/imghwm/default1.png"  data-src="image3.jpg"  class="lazy" alt="图片3">
        </div>
        <!-- ...  -->
    </div>
</body>
</html>

In the above sample code, we create a <div> element with class <code>grid-container, which contains Several child elements with class grid-item, each child element contains a <img alt="How to create a responsive image grid layout using HTML and CSS" >## with src and alt attributes. #element. Next, we need to use CSS to define and layout the grid. The following is the sample code:

.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    grid-gap: 10px;
}

.grid-item {
    width: 100%;
    padding-top: 100%;
    position: relative;
    overflow: hidden;
}

.grid-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    position: absolute;
    top: 0;
    left: 0;
}

In the above sample code, we first use

display: grid to convert the grid-container element into a grid layout container. Then, we used grid-template-columns to define the column layout of the grid, and achieved adaptive responsiveness through repeat(auto-fit, minmax(200px, 1fr)) Layout, setting the minimum width of each column to 200 pixels and filling the available space as much as possible.

Next, we use

grid-gap to define the gap between grid items as 10 pixels.

In the

.grid-item class, we use some styles to ensure that the grid items occupy equal space and to make the image adaptable. By setting padding-top to a percentage value, we make the height of each grid item consistent with its width.

Finally, we applied some styling to

.grid-item img, including setting the width and height to 100%, using object-fit: cover to make the image Fill the entire container as much as possible and position the image on the top and left of the container via position: absolute.

In this way, we have successfully created a responsive image grid layout. No matter how the screen size changes, the layout of the grid items will automatically adapt and the pictures will keep looking good.

To summarize, creating a responsive image grid layout using HTML and CSS is fairly simple. By using grid layout and some basic CSS styles, we can easily implement a beautiful image display page that adapts to different screens.

The above is the detailed content of How to create a responsive image grid layout using HTML and CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version