How to create a responsive product display layout using HTML and CSS

WBOY
Release: 2023-10-16 08:24:35
Original
877 people have browsed it

How to create a responsive product display layout using HTML and CSS

How to create a responsive product display layout using HTML and CSS

Overview:
In modern society, more and more people shop through the Internet. In order to attract more consumers, website developers need to create beautiful and responsive product display pages. This article will introduce you to how to use HTML and CSS to implement a simple and effective product display layout.

Step 1: Set HTML structure
First, we need to set up a basic HTML structure. In this structure, we will use a div element to create a container for the product and insert the product's image, title, and price into it. The following is a basic HTML structure example:

<!DOCTYPE html>
<html>
<head>
    <title>商品展示</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="container">
        <div class="product">
            <img  src="product1.jpg" alt="How to create a responsive product display layout using HTML and CSS" >
            <h3>商品标题</h3>
            <p>商品价格</p>
        </div>
        <div class="product">
            <img  src="product2.jpg" alt="How to create a responsive product display layout using HTML and CSS" >
            <h3>商品标题</h3>
            <p>商品价格</p>
        </div>
        <!-- 添加更多的商品 -->
    </div>
</body>
</html>
Copy after login
Copy after login

In the above example, we have set up a container div that will contain all the products. Each product is defined as a div with the class name "product". The product image, title and price are inserted into the product div respectively.

Step 2: Set CSS style
Next, we need to use CSS to set the style of the product display layout. We will use CSS media queries to implement responsive layout. Here is an example of setting CSS styles:

.container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}

.product {
    width: 300px;
    margin: 20px;
    padding: 10px;
    text-align: center;
    background-color: lightgray;
}

.product img {
    width: 100%;
    height: auto;
}

@media (max-width: 768px) {
    .product {
        width: 400px;
    }
}

@media (max-width: 480px) {
    .container {
        flex-direction: column;
    }
    .product {
        width: 100%;
    }
}
Copy after login
Copy after login

In the above example, we first set the container to flex layout and use the flex-wrap property to make the items wrap when the container exceeds the width. We then set the product width, margins, and padding, and center-aligned the text. Finally, we set the background color for the product container.

In the @media query, we set different styles for different screen sizes. When the maximum width is 768px, we set the width of the product to 400px. When the maximum width is 480px, we use the flex-direction attribute to arrange the products vertically and set the width of the products to 100%.

Step 3: Add more products
If you want to display more products, just add more products div in the HTML. You can copy existing product divs to quickly create new products. The product's image, title, and price can be customized as needed.

Summary:
It is not difficult to create a responsive product display layout using HTML and CSS. By setting the HTML structure and CSS styles, we can easily implement a beautiful product display page. Remember to use media queries to adapt to different screen sizes to ensure your products look best on a variety of devices. I hope this article helped you create an impressive responsive product display layout.

Appendix: Sample file
style.css:

.container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}

.product {
    width: 300px;
    margin: 20px;
    padding: 10px;
    text-align: center;
    background-color: lightgray;
}

.product img {
    width: 100%;
    height: auto;
}

@media (max-width: 768px) {
    .product {
        width: 400px;
    }
}

@media (max-width: 480px) {
    .container {
        flex-direction: column;
    }
    .product {
        width: 100%;
    }
}
Copy after login
Copy after login

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>商品展示</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="container">
        <div class="product">
            <img  src="product1.jpg" alt="How to create a responsive product display layout using HTML and CSS" >
            <h3>商品标题</h3>
            <p>商品价格</p>
        </div>
        <div class="product">
            <img  src="product2.jpg" alt="How to create a responsive product display layout using HTML and CSS" >
            <h3>商品标题</h3>
            <p>商品价格</p>
        </div>
        <!-- 添加更多的商品 -->
    </div>
</body>
</html>
Copy after login
Copy after login

The above sample file uses two schematic product images. You You can replace it with your own product image. Remember to change the file name and path to the corresponding ones in index.html.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!