Showing the contents within a for loop in HTML inline mode

I'm struggling to showcase the elements in thumbnail format side by side using Bootstrap, but every time I try to do so within a for loop, they end up getting displayed on separate lines. Here's my code:

{% for book in all_books %}
<div class="row">
  <div class="clearfix">
    <div class="col-md-2">
      <div class="thumbnail">
        <a href="https://en.wikipedia.org/wiki/Inferno_(Dan_Brown_novel)" 
         target="_blank"><img src="{{book.book_cover}}" alt="Book1" 
        style="width:60%">
          <div class="caption">
            <p>{{book.title}} - {{book.author}}</p>
          </div>
        </a>
      </div>
    </div>
  </div>
</div>
{% endfor %}

Answer №1

Just a couple of things to note:

1 - If you're already using Bootstrap rows and columns, there's no need for a separate clearfix div. You can remove that first.

2 - It seems like the loop is wrapping around the wrong div. After addressing point 1, make sure to move the row div outside the loop like this:

<div class="row">
{% for book in all_books %}
    <div class="col-md-2">
      <div class="thumbnail">
        <a href="https://en.wikipedia.org/wiki/Inferno_(Dan_Brown_novel)" 
         target="_blank"><img src="{{book.book_cover}}" alt="Book1" 
        style="width:60%">
          <div class="caption">
            <p>{{book.title}} - {{book.author}}</p>
          </div>
        </a>
      </div>
    </div>
{% endfor %}
</div>

Answer №2

One important thing to note is that you are repeating <div class="row"> unnecessarily. You should consider moving your for loop within the <div class="col-md-2"> element as shown below:

`

<div class="row">
  <div class="clearfix">
  {% for book in all_books %}
    <div class="col-md-2">
      <div class="thumbnail">
        <a href="https://en.wikipedia.org/wiki/Inferno_(Dan_Brown_novel)" 
         target="_blank"><img src="{{book.book_cover}}" alt="Book1" 
        style="width:60%">
          <div class="caption">
            <p>{{book.title}} - {{book.author}}</p>
          </div>
        </a>
      </div>
    </div>
    {% endfor %}
  </div>
</div>`

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

Hiding and showing div elements using CSS, JavaScript, and PHP

Here is the current code snippet: <? while ($row1 = mysql_fetch_object($result1)) { echo '<a href="#" onclick="showhide("'.$row1->id.'");">Name</a>'; while ($row2 = mysql_fetch_object($result2)) { ...

Remove the ID, class, and other attributes from an HTML string using JavaScript for sanitization purposes

Can someone help me with sanitizing HTML text provided by a user? The HTML code in question is as follows: var htmlStr = `<p id="test" class="mydemo">TEhis is test</p> <pre class="css"> &lt;html> &lt;body cl ...

Issues arise when attempting to display Sphinx documentation containing SVG images on Internet Explorer 11

I have been working on building Sphinx documentation and incorporating a UML diagram generated as an SVG image using graphviz. The SVG file is embedded in the auto-generated HTML template from Sphinx. While the SVG displays perfectly fine on Chrome and Fi ...

Transfer responsibilities of events to the canvas and then fetch the Element in the handler

Currently, I am utilizing the Raphaël library to create a network graph, where nodes are depicted as circles. Users have the ability to dynamically add nodes by clicking on the canvas. When a new node is added, an Element object is pushed into both a Set ...

There seems to be an issue with sending the $_POST data

My straightforward form was functioning well, but now I am facing an issue where the post data is not being sent, and I am unable to identify the problem. <form role="form" name="challengeform" action="scripts/arena_setup.php" method="POST" onsubmit="r ...

Create a variety of tables populated with numerous hyperlinks within each table on a webpage

I have a website where I need to display multiple tables with different data, along with appropriate links within the table cells. Plan: Each table on the webpage represents a different school subject, showcasing performance metrics such as SAT scores, fi ...

Utilize Google Analytics for Tracking Website Performance in Django

I want to incorporate Google Analytics into my Django website either on an HTML page or in the Django admin. I want to display the data in a graphical format on a separate HTML page, based on the number of visitors per month. My website is built entirely ...

Unable to generate webpage source code

Having some trouble with my Next.js project as I'm using getStaticProps and getStaticPath to build a page, but unfortunately, the HTML is not being generated in the page source. This issue is causing problems with SEO, and I haven't implemented r ...

The CSS3 transition is not functioning correctly when transitioning element dimensions from a percentage or auto value to a specific pixel

As part of my project, I am developing a single-page website that features a vector graphic occupying 80% of the screen width on the initial 'start screen'. Once the user scrolls down, the graphic smoothly transitions into a navigation bar positi ...

An adaptive image-based menu design that adjusts based on screen size

Although I can get it to function properly, the trouble arises when I attempt to resize it for responsiveness, resulting in a strange scaling issue. Essentially, I want the images to remain at their natural size and align next to each other seamlessly with ...

Ways to emphasize search outcomes in Flask/HTML?

I am currently working on creating a search box using HTML and the Flask framework in Python. Below is the layout I am working with: Layout My goal is to input text into the search box and have it highlighted within the text area on the same HTML page. F ...

Varied elevations dependent on specific screen dimensions

There seems to be a minor issue with the height of the portfolio container divs at specific window widths. The problematic widths range from 1025 to 1041 and from 768 to 784. To visualize this, try resizing your browser window to these dimensions on the fo ...

Ways to modify the alignment of specific inline-block elements inside a DIV

Is there a way to align two inline-block elements to the left and one to the right without changing their display property? I am looking for a CSS solution, but would also consider using jQuery if necessary. In simple terms, I want the first two buttons ...

What are the steps to troubleshoot CSS issues in NextJS?

Currently, I am encountering challenges while trying to integrate markdown with CSS. The problem I am facing has been replicated in the link provided: https://codesandbox.io/s/react-quilljsbasic-forked-3rcxw?file=/src/App.js ...

The navigation bar is positioned with white space above it

Currently working on a website design and facing an issue with adding a clickable button to the Nav-bar. Noticed some white-space above the Nav-bar which is not desired. I had previously posted a similar question but unable to identify the CSS error causi ...

Looking to shrink the image dimensions for optimal mobile display using bootstrap

In one section, I have two columns - one for an image and one for text. https://i.sstatic.net/QMLNF.png Here is the HTML code: <div class="row aboutRow"> <div class="col-lg-3 col-sm-2 col-xs-3 col-md-3 text-center"> ...

Turn the process of transforming a .createElement("image") into a clickable link

I have successfully organized the images the way I wanted, but now I would like each image to be a clickable link that leads to its own URL Current Image Code: <img src="01.jpg" /> Desired Image Link Code: <a href="01.jpg" target="_new">< ...

Use PHP to open a text file and display its contents in a textarea

I have a textarea on my website and I'm trying to figure out how to populate it with the content of a text file. I followed a tutorial that guided me through writing some code, but when I implement it, I'm facing an issue where the words from the ...

Troubleshooting issue: Bootstrap 4 input group not properly showing invalid feedback text

I've been exploring the features of Bootstrap 4 - beta and I've noticed that when using .is-invalid with .input-group, it doesn't seem to be visible. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.cs ...

Using CSS3 to clear floats without altering the HTML structure

Despite searching for multiple solutions to this issue, I have yet to find one that works without requiring me to modify my HTML code. Here is the current HTML in question: <div class="name">Daiel</div> <div class="amount">30€</div ...