Achieving distinct CSS margins for mobile and desktop devices in a Django template without the need for multiple {% block content %} statements

Is there a way to dynamically change the margin-left of my main {% block content %} in the base.html template based on whether the viewer is using a mobile or desktop device?

This is how my current base.html looks like:

<div class="content container-fluid">
    <div class="row">
        <div class="col-md-8">
            {% block content %}
            {% endblock %}
        </div>
    </div>
</div>

And here is the corresponding css file snippet:

.content {
    margin-left: 40px;
}

.content_mobile {
    margin-left: 10px;
}

Since I have successfully implemented similar functionality in other parts of my application using Bootstrap classes, my initial approach was to utilize those classes as well:

<div class=".visible-xs-block, hidden-xs">
    <div class="content container-fluid">
        <div class="row">
            <div class="col-md-8">
                <!-- This content is not visible on mobile devices -->
                {% block content %}
                {% endblock %}
            </div>
        </div>
    </div>
</div>

<div class=".visible-lg-block, hidden-lg .visible-md-block, hidden-md .visible-sm-block, hidden-sm">
    <div class="content_mobile container-fluid">
        <div class="row">
            <div class="col-md-8">
                <!-- This content is hidden on all other views, excluding mobile devices -->
                {% block content %}
                {% endblock %}
            </div>
        </div>
    </div>
</div>

Unfortunately, Django throws an exception because it only allows one {% block content %} per template!

Do you have any suggestions on how I can achieve this without resorting to multiple blocks?

Answer №1

For instance:

<div class="content">Something else </div>

If you wish to adjust the margin based on screen size

follow these steps

In your main css file :

@media (max-width: 480px) {
    .content{
       margin-left: 20px;
    }
}

@media (max-width: 320px) {
    .content{
       margin-left: 10px;
    }
}

and

@media (min-width:320px)  { /* smartphones, portrait iPhone, portrait 480x320 phones (Android) */ }
@media (min-width:480px)  { /* smartphones, Android phones, landscape iPhone */ }
@media (min-width:600px)  { /* portrait tablets, portrait iPad, e-readers (Nook/Kindle), landscape 800x480 phones (Android) */ }
@media (min-width:801px)  { /* tablet, landscape iPad, lo-res laptops ands desktops */ }
@media (min-width:1025px) { /* big landscape tablets, laptops, and desktops */ }
@media (min-width:1281px) { /* hi-res laptops and desktops */ }

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

Parent div overflowing due to vertical flex container

In my current project, I am attempting to design a layout with a fixed header and footer along with a dynamic content area that utilizes the remaining vertical space. The content-wrapper contains a lot of data, which may require a scrollbar to navigate. H ...

What precautions should I take to safeguard my HTML/CSS/JS projects when sharing a link with my employer?

Picture this scenario: you need to share a link for your work, but you want to protect it from being easily copied or developed further by someone else. However, since it's a document, any browser can still view it. Can anyone recommend a tool that wi ...

Evaluating Django app on local server prior to deploying on Heroku platform

After completing the steps outlined in Heroku's "Getting Started with Django", I managed to successfully deploy my application on the Heroku server. However, I encountered a lack of guidance on testing my application locally before pushing any change ...

Tips for sorting Django objects to retrieve the top X items with the highest property values

Hey there, I'm facing a challenge with a class known as Hero, which contains 150 objects. Each object in this class has a property called Winrate. My goal is to extract the top 12 heroes based on their win rates. class Hero(models.Model): hero_n ...

A small space underneath the <a> element nested within a <li> tag within a navigation bar

As a new programmer, I am currently working on creating a navbar. However, I have encountered an issue where there are small gaps underneath the user when my cursor hovers over it. There is a white gap under the user element. I expect that the hover back ...

Is the default animation duration of Compass being ignored?

After recently updating to Compass version 1.0.16, I started setting up basic usage of the new animation features. However, I encountered an issue where setting default values for different animation settings did not take effect. This led me to hard-code t ...

Choosing different elements using identical classes in JQuery

Struggling with a coding problem that seems like it should be an easy fix, but can't quite figure it out. The HTML code I have is as follows: <section class="actualite"> <div class="actualite-text"> <h3 class="title"&g ...

Displaying a single item per click using jQuery

Hey there, I have a small issue with some HTML divs and show/hide functionality. I'm trying to make it so that only one div is shown per click, but my current jQuery code is showing all the divs at once. Any suggestions on how to fix this? Check ou ...

Pure CSS text slideshow

I'm currently working on developing a text content slideshow using only CSS. Here is the HTML/CSS code I have: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>CSS text slideshow examp ...

What happens when browsers encounter unfamiliar at-rules?

CSS at-rules have been part of CSS since the days of CSS2. As CSS evolves with new specifications like @supports, it's interesting to see how different browsers handle unsupported rules. Do major browsers simply ignore unrecognized rules, or do they f ...

I was able to efficiently reverse the transition using JavaScript, but I'm puzzled as to why my alternate approach is not functioning correctly

Check out my demo on jsfiddle I am trying to implement a reverse transition when clicking the <li> again. The commented code did not work, but the code below worked perfectly: let dbclickre = true; function flipped() { if (db ...

Showing the image as a backdrop while scrolling through text

How can I create an effect that continuously displays the image while scrolling text in Internet Explorer without using position: sticky or position: fixed? var sticky = document.querySelector('.sticky-container'); var img = document.querySele ...

Establishing a straightforward system that allows me to craft designs while viewing real-time updates and refreshes on my web browser

I have started my project with a simple npm setup: npm init node install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5c51514a4d4a4c5f4e7e0a100e100e135c5b4a5f100d">[email protected]</a> This is how my packag ...

Using a single jQuery script, implement multiple modals on a webpage that are responsive on both desktop and mobile devices

My Modal is functioning well on desktop, but I'm facing an issue on mobile where the screen doesn't close when clicking outside the modal area. Is there a way to solve this for mobile without adding the 'X' button to close it? I attem ...

Missing TextField in Django forms

from django import forms class UserForm(forms.ModelForm): first_name = forms.TextField(label=_(u'First name'), required=False) last_name = forms.TextField(label=_(u'Last name')) I encountered an issue when running the code abo ...

What is the best way to toggle content visibility using Bootstrap 5 buttons?

How can I achieve a toggle functionality for two buttons in Bootstrap 5, where clicking one button displays its content while hiding the content of the other button? For instance, when button 1 is clicked, its content should appear while hiding the content ...

Ensure that the remaining space on the page is filled by utilizing 2 divs

I am currently in the process of developing a website that needs to be responsive on all pages. The layout consists of 5 divs positioned horizontally. The three middle divs have fixed sizes - 200px, 400px, and 200px respectively. However, I am facing a cha ...

CSS - What's the source of this mysterious whitespace?

I'm currently making adjustments to a Wordpress website that is designed to be responsive. The site uses media queries to adapt its display based on the user's screen size, and everything looks great except for when the screen width is at its sma ...

Guide to adding an icon within an HTML `<input>` element

Is anyone able to help me successfully place my icon font inside the search input field? I've tried adjusting the margin and padding, but nothing seems to be working! If you have any insights or suggestions, please let me know. Your help is greatly a ...

What is the best way to include some white space around a paragraph?

Can anyone help me figure out how to add margins to my paragraphs with borders on a website? I've tried using margin in CSS, but it's not working. Here is the HTML code I am using: body{ margin:0px; font-family:Calibri; background ...