"Want to learn how to create divisions that start on a new line in your HTML/CSS

Is it possible to limit the number of divs displayed per row? I'm working on a Django project and would like to have a maximum of 3 divs per row. Ideally, this would automatically create a new line after every third div without using JavaScript. Any suggestions or solutions would be greatly appreciated. Thank you in advance.

.main-section{
    background: white;
    text-align: center;
    border: solid 1px #D1BEA8;
}

.book-list-main{
    display: table;
    table-layout: fixed;
    border-spacing: 30px;
}

.single-book{
    display: table-cell;
    width: 310px;
    height: 400px;
    margin: 0 20px 20px 10px;
    grid-column-start: 1;
    border: solid 1px #D1BEA8;
    height: fit-content;
}

.img-description-container{
  display: flex;
}

.single-book h1{
    font-size: 20px;
}

.img-description-container img{
    max-width: 180px;
    max-height: 240px;
    float: left;
}

.img-description-container p{
    float: right;
    font-size: 12px;
    overflow-y: scroll;
    margin-top: 50px;
}

.author-display{
    display: flex;
    flex-direction: column;
}
...
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="tryy.css">
</head>
<body>
    <div class="book-list-main">
                    
        <div class="single-book">
            <h1>BOOK TITLE</h1>
            <hr>
            <div class="img-description-container">
                <img src="images/cover.png" alt="Book Cover">
                <div class="author-display">
                <p>info about it</p>
                <span><strong>Author:</strong> ...</span>
                </div>
            </div>
            <div class="single-book-footer">
                <p><span  class="votes-total" data-hover="(214 Votes)">&#9734 <strong>4.6/5</strong> </span></p>
                <button>Add to Shelf</button>
                <button>Add Review</button>
            </div>
        </div>
        ...
        </div>


    </div>
</body>
</html>

Answer №1

If you have a collection of books like:

books_collection = ["Book A", "Book B", "Book C", "Book D", "Book E", "Book F", "Book G", "Book H", "Book I"]

The first step is to transform it into a structured format as follows:

books_collection = [("Book A", "Book B", "Book C"), ("Book D", "Book E", "Book F"), ("Book G", "Book H", "Book I")]

This is how the conversion process can be achieved:

structured_books_list = []
chunk_size = 3

for i in range(0, len(books_collection), chunk_size):
    structured_books_list.append(books_collection[i:i+chunk_size])

Then, when rendering the template, the following structure can be adopted:

{% for row in structured_books_list %}
    {% for book_title in row %}
        {{ book_title }}
    {% endfor %}
    <br>
{% endfor %}

Remember to customize the {{ book }} as per your HTML requirements.

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

The integration of single page applications and <form> elements is a powerful combination in web development

Is there still value in using a <form> element over a <div> element when developing single page applications? I understand the purpose of the <form> element for traditional form submissions with a full page reload, but in the context of ...

How can we convert unpredictable-length JSON user input into well-structured HTML code?

Welcome to the world of web development! I am currently embarking on a project where I aim to transform JSON data into HTML structures. Specifically, I am working on creating a dynamic menu for a restaurant that can be easily updated using a JSON file. The ...

Replace the facebook plugin using JQuery libraries

Does anyone know how to remove the like button on top of the 'Like box' Facebook plugin using JQuery? I have imported the like box from Facebook and I want to eliminate this like button, but Facebook does not allow me to do so. Therefore, I am t ...

Tips on inline password field placement for a better user interface experience?

While creating a registration form, I ran into a user interface problem. There are two specific changes I'd like to make: The label for Confirm Password should be inline on the same line. The password input should have an eye icon embedded within it ...

Learn the process of discarding an incoming email and responding with a message using Google App Engine

I recently reviewed the documentation: from google.appengine.api import mail mail.send_mail(sender="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d7e787d7d627f794794d65756c607d6168236e6260">[email protected]</a> ...

What is the best way to align text and images for list items on the same line?

HTML:- ul { list-style-image: url(https://i.sstatic.net/yGSp1.png); } <ul> <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, consequatur?</li> <li>Lorem ipsum dolor sit amet, consectetur adipisici ...

Ensuring a full height div using CSS

I have encountered an issue and created a fiddle to help illustrate it: http://jsfiddle.net/XJpGT/ The challenge I am facing is ensuring that the height of the green box always remains at 100%, while also making sure that the green and orange boxes do not ...

Slippery carousel: Showcase all items in center alignment mode

Currently, I am implementing Slick Carousel with centerMode. Is there a method to ensure that all items are displayed without being cut off on the screen? The setup I have is quite simple: $('.your-class').slick({ centerMode: true, slidesTo ...

It appears that the React MUI Grid is causing the page or its container to experience overflow issues

I'm utilizing Grid to maintain the organization of the entire page, but I keep encountering an overflow issue when adding multiple Grid items. This results in the scrollbar appearing even though the container size remains the same. Dashboard.tsx is pa ...

What is the process for adding elements to an object in Angular JS?

My goal is to send data to an ng-object after filling out and submitting an HTML form with user-defined values. However, after pressing submit, the values are being duplicated. I have attempted to clear the data by using $scope.user > $scope.user=&apos ...

AngularJS using the ng-controller directive

I am presenting the following HTML code excerpt... <!DOCTYPE html> <html lang="en-US" ng-app> <head> <title>priklad00007</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angula ...

Learn how to request a unique identifier from a URL to display the help contents. Discover the methods of

I am seeking guidance on advancing in this project. How can I achieve a specific result using PHP and MySQL? The original link is samplesite.com/welecome.php. When a user clicks on a link within the page, the URL transforms to samplesite.com/welecome.php?h ...

Django: design a model with a collection of values for every day in the month

Imagine if I need to design a form that enables a user to input 2 numeric values for each day of the month (e.g. count and rate). As the number of days in each month varies, what is the most effective way to accomplish this? Would it be advisable to set u ...

The sub-navigation element goes beyond the traditional navigation bar and causes misalignment

I'm currently in the process of developing sub navigation bars that expand upon hover. However, I've encountered an issue where hovering over "Manage" triggers the regular box to expand. This is happening because of the position: relative within ...

Is it true that eliminating white spaces can enhance a website's loading speed?

I'm curious about something. Can removing white space actually make a website load faster? For instance, take a look at the following CSS snippet - body{ overflow-wrap:break-word; word-break:break-word; word-wrap:break-word } .hidden{ display:none } . ...

Using AngularJS, I can bind the ng-model directive to asynchronously update and retrieve data from

I am currently working with Angular to develop a preferences page. Essentially, I have a field in a mysql table on my server which I want to connect my textbox to using ng-model through an asynchronous xhr request for setting and fetching data. I attempt ...

Position the center div within the parent div and ensure it overlays another child div as well

For my chess game, I want to display a message in the middle of the screen when a player wins, such as "White Wins," etc. The current setup is as follows: <div> <p> White Wins <p/> <div/> This message share the parent div with a ...

Added the .active state to the menu navigation successfully, however the active color was not implemented

My primary navigation menu is managed by WordPress and can be seen below. I aimed to modify the active state when a user clicks on a link in the menu by adding background-color and color for styling purposes. Although changing the background-color upon cl ...

Implementing Mouse Scroll Click Functionality in ASP.NET with C# (Without JavaScript)

How can I add a Mouse Scroll Click (Middle Button) event in ASP.NET and C#? I have already looked into the MouseWheel Event, but it did not provide the solution I was looking for. This is the order in which mouse events occur: MouseEnter MouseMo ...

The width of a div element seems to mimic the width of the entire page inexplic

There is a page containing various div elements. HTML: <div class="utilitiesHeader"> <div id="utilitiesLogo"></div> <div id="utilitiesFIO">Name of user</div> </div> <div class="utilitiesContent"> <d ...