Display picture next to content with the help of CSS/Bootstrap and Django's Jinja 2

I'm having trouble displaying an image next to text, it keeps showing up below. Here's the code snippet:

<div class="container">  
  <div class="row">
        <div class="col-md-8">
            <h1>This is my first post</h1>
            <div> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard …   
        </div>
        <div class="col-md-4">
            <img src="/media/post/Screenshot_from_2022-07-17_23-55-13.png" class="img-thumbnail" >
        </div>
        <div><a href="/blog/first-post" >Read More..</a></div>
        
        <div class="col-md-8">
            <h1>this is second post</h1>
            <div> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard …   
        </div>
        <div class="col-md-4">
            <img src="/media/post/0a61bddab956.png" class="img-thumbnail" >
        </div>
        <div><a href="/blog/2nd-post" >Read More..</a></div>
  </div> 
</div> 

Here is how it appears: https://i.sstatic.net/YHgtV.png

EDIT. I copied this from view-source and some text got cut off. I am actually using Django jina 2 template code

{% for post in context %}
<div class="col-md-8">
    <h1>{{ post.title}}</h1>
    {{ post.content |truncatewords:20 | safe}}   
</div>
<div class="col-md-4">
    <img src="/media/{{post.image}}" class="img-thumbnail" >
</div>
<div><a href="/blog/{{post.slug}}" >Read More..</a></div>
{% endfor %}

And here is the base.html file code:

<div class="container">  
  <div class="row">
{%block content %}

{%endblock%}
  </div> 
</div> 

I suspect that truncatewords:20 is causing the issue.

By replacing it with truncatewords_html:20, the issue has been resolved

Answer №1

To display the image and text side by side, apply the d-inline class to both elements.

Answer №2

It seems like there is an issue in your HTML code - you forgot to close the div with class col-md-8 which is causing the text and image to be displayed next to each other on larger screens.

Please review the code snippet below for more details:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1a3aeaeb5b2b5b3a0b181f4eff1eff3">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a6865657e797e786b7a4a3f243a2438">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<div class="container">
  <div class="row">
    <div class="col-md-8">
      <h1>This is my first post</h1>
      <div>
        <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard …
      </div>
    </div>
      <div class="col-md-4">
        <img src="/media/post/Screenshot_from_2022-07-17_23-55-13.png" class="img-thumbnail">
      </div>
      <div><a href="/blog/first-post">Read More..</a></div>

      <div class="col-md-8">
        <h1>this is second post</h1>
        <div>
          <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard …
        </div>
    </div>
        <div class="col-md-4">
          <img src="/media/post/0a61bddab956.png" class="img-thumbnail">
        </div>
        <div><a href="/blog/2nd-post">Read More..</a></div>
      </div>
    </div>

Answer №3

I believe that a class="row" should not have a col total exceeding 12. Please update your code as shown below.

<div class="container">
    <div class="row">
        <div class="col-md-8">
            <h1>Here is my initial post</h1>
            <div><p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard …
        </div>
        <div class="col-md-4">
            <img src="/media/post/Screenshot_from_2022-07-17_23-55-13.png" class="img-thumbnail" >
        </div>
    </div>
    <div><a href="/blog/first-post" >Read More..</a></div>
    <div class="row">
        <div class="col-md-8">
            <h1>this is second post</h1>
            <div> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard …
        </div>
        <div class="col-md-4">
            <img src="/media/post/0a61bddab956.png" class="img-thumbnail" >
        </div>
    </div>
    <div><a href="/blog/2nd-post" >Read More..</a></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

Django displays the <head> within the <body> section of the webpage

I encountered a strange bug in my main project, so I created a small test scenario to troubleshoot. The issue lies in rendering content from the tag within the body tag, but I have been unable to identify the root cause. layout.html: {% load static %} ...

Managing spaces in django tooltips when utilizing translations

I'm trying to translate a tooltip on a link with the bootstrap class btn in a django template. The code looks like this: <a href="" class="btn btn-large btn-success" name="phone" data-toggle="tooltip" data-placement="right" title="Edited via phon ...

Guidelines for implementing a logout feature in Django using templates

Here is my views.py file: from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt import os # Create your views here. @csrf_exempt def index(request): if('loggedIn' in request.session): id = request. ...

The position of the carousel items changes unpredictably

My jQuery carousel consists of 6 individual items scrolling horizontally. Although the scroll function is working correctly, I have noticed that 2 of the items are randomly changing their vertical position by approximately 15 pixels. Upon reviewing the HT ...

Dynamic water filling effect with SVG

I'm trying to create a wipe animation that looks like water filling up inside of a drop shape. Currently, it is a square with a wave animation on top of the drop logo. The wave animation works correctly, but I am struggling to contain it within the dr ...

Insert a new, unique div onto an HTML webpage using JavaScript, ensuring no duplicates are created

Having an issue with this code snippet where nothing is being added. It seems like the variable result may not be taking a number or something else, but I'm not entirely sure why. $(document).ready(function () //document.getElementById(1).style. ...

Exploring the functionality of Next.js with Bootstrap 5 modals

Greetings, I have been trying to implement Bootstrap 5 without using react-bootstrap or reactstrap for my modal creation. However, I keep encountering this error: TypeError: undefined is not an object (evaluating 'this._element.classList') I ha ...

The curious conduct of wild safari creatures

I have been using JavaScript to split my ePub chapter into pages by wrapping the code with new divs that represent separate pages. I have also created CSS styles for these new divs. Everything works perfectly when the file has a filename extension of &apos ...

I'm wondering why my typography components display correctly on my local host, but not on my aws server. Any insights on

I've encountered an issue with the typography component I'm using for my headings. When I publish the website, the headings do not render properly and the styles are not applied correctly. Strangely, everything looks fine during npm run dev, but ...

Attempting to align content in the middle of the page across all web browsers

I need assistance with centering all the content on my website across different screen sizes and browsers. Currently, everything appears off-center and aligned to the left on various screens I have tested. You can view the HTML and CSS code in this link. H ...

Using Django Admin login form in a custom LoginRequired middleware

Solution sought with Django In my Django 1.6.2 (on Python 3.3) project, I aim to automatically apply the login_required decorator to all views and utilize the default admin login form. Avoiding the method described in Redirect to admin for login, I explor ...

Tips for repairing a horizontal line at the bottom of the <TD> tag and placing text on top

I am working with an HTML table that contains a single TR tag and two TD tags. Each TD has two HR tags creating horizontal lines, along with some text. I am looking for a way to keep the text aligned at the top of the TD and the horizontal line at the bott ...

Applying a dynamic translate3d transformation on the ::after element using CSS3

My current challenge involves manipulating a pseudo ::after element using translate3d If I hardcode the values, it's straightforward: div::after { ... transform: translate3d(40px, 40px, 0); } Now, I want to dynamically set the value for the ...

Expand the image background to fit within a div container

Below is the code snippet I am working on: <div id="intro_section_upper" > <%= image_tag("video_background.jpg", :id=>"video_bg") %> <div id="video_table_main" > <table class=" ...

Flexbox or Bootstrap 4 Grid: Which is the Better Choice?

My form is structured as follows: <div class="row"> <div class="col"> <textarea name="comment" class='form-control' placeholder='Type new comment here..'></textarea> </div> <div clas ...

Tips on modifying webpage content based on page size (zoom) in responsive web design using CSS

I have a good understanding of responsive design and how to set elements with relative width. My specific question is regarding the transition of the page from thisto that. Any insight on this would be greatly appreciated. Thank you in advance. ...

Testing lettuce with django and selenium is not running on Windows

Currently, my lettuce test suite using Selenium performs perfectly on Linux. However, upon installing Django and all necessary components on Windows in order to test the suite on IE8 and IE9, I encountered an issue. When attempting to run the test on Wind ...

Is there a way to trigger a modal popup when hovering over a Bootstrap 5 card?

After coming across a handy template online, I decided to implement a modal pop-up feature when hovering over cards using Bootstrap 5. Here's what I have so far: class SavedEpisodes extends Component { $(function() { $('[data-toggle=&qu ...

The issue with grunt-contrib-compass not functioning properly within npm

I recently installed npm and grunt, followed by installing grunt-contrib-compass. However, when I try to run grunt, I encounter the following error: Running “compass:dist” (compass) task Warning: not found: compass Use –force to continue. Aborted du ...

What possible reason could there be for the empty space on the left side?

<ul style="background: blue; list-style: none; width: 40px; height: 40px; color: white"> <li style="float: left; width: 20px; background: red">a</li> <li style="float: left; width: 20px; background: green; height: 40px">b</li ...