How to properly align the content inside a div in a Django project

As I take on my first project in Django, I find myself grappling with how to center elements on my site. Specifically, I aim to create a div that encompasses all the content on the page, excluding the navigation bar. This div should be positioned perfectly in the middle of the screen and occupy 50% of the width. Additionally, all the content inside this div should also be centered. Despite trying various methods, I am still facing challenges in achieving this layout. Here is a glimpse of what I have implemented so far:

https://i.sstatic.net/WqLBu.png

Below is the template code:

<html>
<style>
    body {
        margin: 0;
        padding: 0;
    }

    img[src="/media/images/crate_bar.png"] {
        margin: 0;
        padding: 0;
        display: block;
        width: 100%;
        height: auto;
        box-sizing: border-box;
    }

    img[src="/media/images/logo.png"] {
        position: absolute;
        display: block;
        width: auto;
        height: auto;
        left: 50%;
        transform: translateX(-50%);
        top: 0;
    }

    #logo {

        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
    }

    #main_container {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    #second_container {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 50%;
    }
</style>
<div id="logo">
    <img src="/media/images/crate_bar.png">
    <a href="{% url 'mainapp:home_view' %}"><img src="/media/images/logo.png" alt="logo"></a>
</div>
<h1>
    {% if user.is_superuser %}
    <a href="{% url 'mainapp:new_article' %}"><button type=" button">New article</button></a>
    {% endif %}
    {% block content %}
    <div id="main_container">
        <div id="second_container">
            <ul>
                {% for article in list_of_articles %}
                {% if article.article_image %}
                <img src="{{article.article_image.url }}" height="200" width="450">
                {% endif %}
                <p>{{article.publication_date}}</p>
                <h1><a href="{% url 'mainapp:article_view' article.id %}">{{article.title}}</a></h1>
                {% endfor %}
            </ul>
        </div>
        {% endblock %}
    </div>

</html>

Answer №1

Make sure to include a body tag in your HTML code and apply the following styles to it. (It seems like your HTML is missing the body tag)

body {
    margin: 0;
    padding: 0;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

Next, for each container you want to create, use the following CSS styles:

#main_container {
    margin: 0 auto;
}

#second_container {
    width: 50%;
    margin: 0 auto;
}

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

Is it possible to center without specifying a fixed width?

Check out this demo link Is there a way to center all the divs and paragraphs under the main div without specifying a fixed width value? I want to remove the 300px width in the .center class and have all the elements align center. Specifically, how can I ...

I'm using SASS in my project, can you provide guidance on customizing the UI with @aws-amplify/ui

I've recently taken over a Next.js (React) project from another developer and they've used .scss files (SASS) for styling. The specific project I'm working on can be found here https://github.com/codebushi/nextjs-starter-dimension My task n ...

Failure to locate the Python kernel specs for the notebook in iPython with Django resulted in a CommandError

Exploring the world of jupyter/ipython and django, I find myself facing a challenge. Running Django 1.9.5 on Ubuntu 16.04 has been smooth, but now I seek to integrate iPython (version 5.1.0) with my Django application. The goal is to launch the iPython not ...

Modifying CSS class attributes with JavaScript

I am looking for a way to dynamically change the value of an attribute in a CSS class. Here is my situation: I have multiple elements that are all using the same class. Instead of individually selecting and applying styles to each one, I want to alter a ...

Is there a way to automatically recalculate the "Total Price" when the input values are adjusted?

Whenever I add an item to the cart, it gets appended to the row in the shopping cart, and the price adjusts accordingly. However, I'm having trouble getting the price to adjust whenever I change the input values (input type: "number"). I can't se ...

Error in finding Django page - general

Can you explain the distinction between Django's pair of choices: return HttpRequestNotFound() raise Http404 Please elaborate. ...

Internet Explorer 8 encounters issues with HTML5 shiv causing disruption to the webpage

As a novice in html5, I am facing an issue with rendering the page in IE8 when trying to call the html5 shiv. Only the background loads, and the rest of the content is missing. Here is what I have added: <header> <!--[if lt IE 9]><script s ...

Drop down menus fail to appear after the screen has been resized

Creating responsive menus involves using ordered and unordered lists, along with CSS for styling. I have added a script to dynamically generate dropdown menus, but encountered an issue where nothing appears on the screen upon resizing - only the backgrou ...

Is it possible to create a webpage where the header and footer remain static, while the content in the body can be dynamically changed

I am in the process of creating a webpage where main.html will load a page with a static header and footer that remains unchanged when navigation links are clicked. I'd like to achieve this using Javascript, with the body content being sourced from an ...

Unable to reset iframe style height in IE8 using JavaScript

I am encountering an issue with resetting the height of an iframe using JavaScript. Here is the code snippet I am working with: var urlpxExt = document.getElementById('urlPx'); urlpxExt.style.height = "200px"; While this approach works well in m ...

Displaying contents do not seem to work with the background color

I've been attempting to include a border and background-color to my table but for some reason it's not taking effect. This is the CSS code for my table: table { width: 100%; display: grid; overflow: auto; } table thead, table tbody, t ...

Having trouble moving forward after encountering the 'tuple' object without the 'startswith' attribute error while uploading a file

I encountered this issue AttributeError at /admin/user/usermodel/1/ 'tuple' object has no attribute 'startswith' whenever I attempt to upload a file using the admin panel. As a beginner in file uploading, I am struggling to solve this ...

Enhancing the django UserCreationForm

So, I have expanded the User model: class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) sheba = models.DecimalField(max_digits= SHEBA_LENGTH, decimal_places = 0) mellicode = models.DecimalField(max_digi ...

visuals in lieu of radio buttons

Currently, I am working on customizing my radio buttons and I want to find a way to add images for both the checked and unchecked events without any compatibility issues across different browsers. I have tried various methods, but each one comes with its o ...

Navigation bar's active area does not span the full height

I'm facing some issues with the navigation bar on my responsive website. One issue is that the clickable area for the links does not extend to the full height of the nav bar, and I would like it to cover the entire height. Another problem arises whe ...

What is the method for loading a subcategory based on the category by invoking a jQuery function within the <td> element of a JavaScript function that adds rows dynamically?

Whenever I click the add row button, the category dropdown list successfully loads. However, when I select an option from this category list, the subcategory does not load any list. The Javascript function responsible for adding rows dynamically is as fol ...

Error: Django couldn't find the template for the specified path at /home/

Seeking assistance to incorporate a template displaying a ModelForm on the home page of my Django application. To achieve this, I created a separate app named home within my project for the dynamic content of the home page. However, the current template I ...

Issue with Django: Unable to fetch data from server response in Ajax

Just starting out with Django and trying to figure out how I can dynamically add content from a python script without reloading the page. In my views.py file, I have two functions - one for uploading a file (home) and another for calling a python script t ...

Different ways to personalize marker designs and incorporate numbers into Folium markers

I came across a technique to include numbers within markers by utilizing plugins.BeautifyIcon on this site - Folium markers with numbers inside. However, I am searching for an alternative method to incorporate numbers without relying on plugins.BeautifyIc ...

I am experiencing issues with my drag and drop feature not functioning properly

I am looking to reposition my #timebase1 div within the draghere div. Currently, it only moves the start of the div, but I would like to be able to drop it anywhere inside the draghere div. function handleDrag(e) { var id = e.id; ...