What is the best way to align product cards side by side instead of stacking them on separate lines?

I am currently working on a mock-up website for a school project. For this project, I am developing a products page where I intend to showcase the items available for sale. Although I managed to successfully copy a template from w3schools, my challenge lies in creating multiple cards that can be positioned side by side. Whenever I paste the same template again, it results in the card being displayed on a separate line rather than next to the first one.

My attempts involving float:left and additional copying and pasting of the template haven't yielded the desired outcome. The second card consistently ends up beneath the first one instead of beside it. I'm unsure of how to proceed with placing them adjacently. While I understand the concept of floating elements next to each other, I seem unable to apply it to this particular situation. Any advice or guidance would be greatly appreciated.

The Code I Copied:

<!DOCTYPE html>
<html>
... (template code)
</body>

My Personalized Version:

<!doctype html>
<html>
<head>
... (customizations)
</body>

Thank you for your help!

Answer №1

To make your cards display in a row, you'll need to nest them inside a div with the property set to display: flex;

In your HTML:

<div class="wrapper">
    <div class="card">
        <img src="collar.png" alt="Denim Jeans" style="width:100%">
        <p class="productName">Leather Collar</p>
        <p class="price">$<strike>9.99</strike></p>
        <p class="newPrice">2.99</p>
        <p class="productDes">A comfortable collar made with natural materials.</p>
        <p><button>Add to Cart</button></p>
    </div>
        
  
       <div class="card">
        <img src="collar.png" alt="Denim Jeans" style="width:100%">
        <p class="productName">Leather Collar</p>
        <p class="price">$<strike>9.99</strike></p>
        <p class="newPrice">2.99</p>
        <p class="productDes">A comfortable collar made with natural materials.</p>
        <p><button>Add to Cart</button></p>
      </div>
</div>

In your CSS:

 .wrapper{
    /* width: 100%; */
    display:flex;
    flex-direction:row;
    justify-content:center; //there are several options to position
    align-items:center;
    flex-wrap: wrap; //if the display gets too small, the cards will automatically reposition on a different line

}
.card {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  max-width: 300px;
  /* margin: auto; */ //no more need because parent controls position.
  text-align: center;
  font-family: arial;
}

Final Product:

<html>
<head>
<meta charset="utf-8">
<title>Pet Store</title>
<style>
/* styling for various elements... */

.productName {
    font-size:68px;
    font-weight:lighter;
    padding:10px;
}

.productDes {
    padding:10px;
}

.newPrice {
 font-size: 32px; 
 color:#272727;
 font-weight:bold;
 padding:10px;
}

</style>
</head>

<body>

<!-- Rest of the content -->

</b

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Switching classes in real time with JavaScript

I'm struggling to understand how to toggle a class based on the anchor text that is clicked. <div> <a id="Menu1" href="#">Menu</a> <div id="subMenu1" class="subLevel"> <p>stuff</p> </div> <div> <a i ...

Enhancing the appearance of unordered lists with CSS. Creating numbered lists

Here is a list that I am currently working with: <ul> <li>question1</li> <li>answer1</li> <li>question2</li> <li>answer2</li> </ul> I have applied the following CSS styling: ul { ...

Images are not being shown on the desktop carousel display

I am encountering an issue with my carousel. When I attempt to decrease the height of the carousel, the entire image is not being displayed properly (on desktop), although it works fine on mobile devices. My goal is for the carousel not to take up the ent ...

Customize the appearance of the date input box in MUI KeyboardDatePicker

Currently, I am attempting to customize the appearance of the KeyboardDatePicker component including board color, size, font, and padding. However, none of the methods I have tried seem to be effective. Here is what I have attempted so far: 1 . Utilizing ...

Execute JavaScript code following the completion of loading and running an external script

My goal is to dynamically load and run a third-party JavaScript file (from a different domain) and then execute some of my own code afterwards. One option I'm considering is using jQuery's $.getScript: $.getScript('https://login.persona.org ...

Can the background color of HTML header text be altered using JavaScript in JQGRID?

Can the background color of HTML header text be modified using JavaScript? Updated: Oops, I forgot to mention that it is for the header text in jqGrid. My apologies for that oversight. ...

The text element does not line up with the icons

As I'm working on creating my first account page header, I've run into an issue with some of the icons not aligning perfectly with the text. While advanced developers may find this task simple, as a beginner, I could really use some advice on how ...

Issue with slideout menu hyperlinks malfunctioning

Currently developing a site at , everything seemed ready for deployment until testing in 320x480 mode on mobile revealed that the links on the slideout menu were not working on any mobile device I tried, regardless of resolution or page. I've tried u ...

AngularJS HTTP POST request encountering issue with success function not functioning as expected

I am currently working on implementing a basic HTTP post request to insert data into my database. The following pages are involved: register.php contains the registration form. maincons.js houses the application controllers. sqlregister.php include ...

error in URI (not a valid URI):

Encountering an issue with a URI that is triggering a bad URI error. http://localhost:3000/api/v1/company_donations.json?token=foo&donation={&amount=101}&comment=Ordered The goal is to have the URL carry 2 attributes Token Donation obje ...

Omitting an element from the CSS styling

How can I create a CSS selector that targets the text content within this snippet, excluding the <label> component? Essentially, I want to style the text in the div while leaving the label unchanged. <div id="edit-cc" class="form-item form-type ...

Guide to implementing CSS3 transitions with prefixes using JavaScript

Is there a way to apply CSS styles using JavaScript when I don't have access to the CSS file? #fade div { -webkit-transition: opacity 1s; -moz-transition: opacity 1s; -o-transition: opacity 1s; -ms-transition: opacity 1s; transition: ...

Artboard: Easily navigate through pictures

As part of a school assignment, I wanted to create an interactive story where users can click through images using the UP and DOWN buttons. However, I am facing an issue with my If function overriding the previous function. I envision this project to be a ...

Limit the size of images with CSS properties like max-height or max-width

What is the preferred method for restricting the size of an image and why? While max-width can be used to restrict the size of an element, this article recommends using it specifically for images within containers. If you have a web application with a ve ...

"Interactive elements like dropdown menus and clickable buttons that transform the content of an HTML

My webpage includes three buttons that, when clicked, reveal hidden div elements containing forms. Here is the HTML code for the buttons: <button id="edituser" type="submit" onclick="toggle_visibility('c');" style="border:0;width:100px;margin ...

The width of the Ion-slide is dynamically determined by the styling

After transitioning my Ionic 2 project to Ionic 3, I encountered issues with ion-slides which are affecting my app's functionality. Upon app loading, specific widths are being defined in the style tags on the slides, disrupting the intended styling. ...

Problem with traversing from parent to children elements using jQuery selectors

<form data-v-c4600f50="" novalidate="novalidate" class="v-form"> <div data-v-c4600f50="" class="pr-2" question="Top Secret4"> <div data-v-c4600f50="" f ...

Listen for the load event during an AJAX request without using jQuery's add

I have four HTML files and four corresponding JavaScript files. Each JavaScript file is externally loaded by its respective HTML file. Specifically, index.html loads javascript.js, 1.html loads javascript1.js, 2.html loads javascript2.js, and 3.html loads ...

jQuery "slide" animation without using <br>

I've been working on a website that incorporates the jQuery "Slide" effect. I have implemented this effect multiple times, using it on 3 different div tags. Each line consists of one "Dynamic" div tag (the moving one) and one "Static" div tag (the tri ...

Steps for successfully passing an object in a dynamically generated function invocation

In my jQuery script, I have a click event bound to thumbnail images with the class "thumbnail": $(".thumbnail").click(function(){ var obj = new Object(); obj.el = "imageEdit"; obj.id = $(this).attr("id").replace("img_",""); openDialogue(obj); }); ...