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

Is there a way to detect if JavaScript is disabled using a unique CSS selector?

Does anyone know of a CSS selector that can be used when JavaScript is disabled? I'm not referring to the noscript tag, but specifically in CSS. ...

Ways to eliminate header and footer data while printing a webpage

In my attempt to implement the code displayed below, I am encountering an issue where the header and footer continue to appear on the printed page. Despite numerous attempts with various CSS elements, I have been unsuccessful in removing the header and fo ...

How can I vertically align text in React-bootstrap Navbar Nav-items?

I have been struggling to find a solution to my problem without resorting to custom CSS classes. I'm wondering if it's possible to achieve what I need using only flex classes. Here is the code for a simple navbar component: <Navbar bg ...

Configuring the port for the live server while executing tests in Django

I am currently developing a web application using Django and deploying it with Docker. I want to conduct testing on the container using Selenium, specifically through Selenium Grid. However, I have encountered an issue where I need to port forward a specif ...

Rampant number of objects contained within box element

I am currently working on a box component that contains flex items. Unfortunately, I am facing an issue where the box width remains constant and causes overflow when I try to reduce its size in order to accommodate all the items. Can anyone provide guidanc ...

How can I design a versatile button in HTML/CSS3/jQuery/Javascript that utilizes images for borders and corners for maximum scalability?

Is there a way to create a completely scalable button in HTML/CSS3/jQuery+Plugins using images for borders and corners? I have attempted a messy method, but I am confident that there are more effective solutions available. The approach I considered invol ...

Issues with Yahoo social login functionality in Django-allauth

I'm currently working on integrating Yahoo Social Login into my website, using django-allauth. So far, I've successfully implemented Google login and believe I've also done the same with Facebook and Amazon logins. However, a major obstacle ...

Load user information for the current logged in user using Django

Greetings fellow Django beginners! I know this question has been asked before, but I'm having trouble finding a solution that fits my specific situation. Here's the issue: I have a Form with a choice field that gets its options from the databas ...

Images Centerfold Team

I am facing a challenge in creating a group of images for an album. There seems to be a significant gap on the right side, which cannot be resolved without using padding. However, adding padding only works for my current PC resolution and creates a larger ...

An HTML/CSS component that dynamically adjusts its height at random intervals

My goal is to have the footer of my page occupy just one line in height. On occasion, when I open my page in a new browser tab, the footer ends up being two lines tall. However, if I simply reload the page within the same tab, everything appears as intende ...

Step-by-step guide on redirecting a page from a Django form

I'm in the process of developing a dictionary application that allows users to input a word through a form. The issue I'm facing is that when a user enters a word and hits submit, the form disappears leaving only the submit button and the search ...

Incorporating a CSS Module into a conditional statement

Consider the following HTML structure <div className={ `${style.cell} ${cell === Player.Black ? "black" : cell === Player.White ? "white" : ""}`} key={colIndex}/> Along with the associated CSS styles .cell { ...

Is it possible to leverage the flex features of react-native in a manner similar to the row/column system of

Is it a good idea to utilize flex in react native by creating custom components that retrieve flex values? I previously used the bootstrap grid system and now I am exploring react native. Is there a way to implement this example using react-native bootstr ...

Crafting a dynamic inline search form with Bootstrap

My goal is to replicate the design in this image: This is my current progress: ...

"How to retrieve the height of an element within a flexslider component

I need some assistance with using JavaScript to determine the height of an element within a flexslider. There are two challenges I am facing. When I attempt to use a regular function getHeight(){ var h = document.getElementById("id-height").style.height; ...

Styles in print CSS are not effective in an Angular project

Currently, I am working on an Angular project where I need to generate a printable document using CSS. The challenge I am facing is ensuring that the date and title in the header do not print automatically when the document spans multiple pages. Additional ...

Utilizing Laravel 5.2 to showcase a video

I am currently developing in Laravel 5.2 and facing an issue with displaying a video on my view page. While I can successfully store the video in my database table, I am encountering difficulties when trying to fetch and display it. In my database table, I ...

After the initial submission, the Bootstrap placeholder for the Select Option is only displayed

I'm attempting to create a placeholder for my dropdown with multiple options using Angular and Bootstrap. However, it seems like I may be doing something incorrectly. I created an option for a placeholder such as please select and placed it at the top ...

Determine the body's height by adding the heights of the header and footer

In my design, there is a header with a height of 8rem; .header { top: 0; left: 0; height: 8rem; } Following that, there is a footer with a height of 5rem; footer { width:100%; height: 5rem; } I am attempting to calculate the heig ...

How can I make the arrows work on a secondary slider with EasySlider1.7?

I finally managed to get 2 sliders working on my page after reading through several other tutorials. However, I am facing an issue with the Prev & Next arrows on the second slider as they don't seem to work properly. I inherited this page from someon ...