Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different css margins for mobile & desktop without multiple {% block content %} in Django template

I need to change the margin-left that is applied to my main {% block content %} in my base.html template (that contains my navbar and other common elements) based on if the viewer is using mobile or desktop.

My current base.html is like:

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

with a css file containing:

.content {
    margin-left: 40px;
}

.content_mobile {
    margin-left: 10px;
}

Given that in other parts of my application I've accomplished something similar by using the following dedicated Bootstrap classes, my first thought was to do the same using something like:

<div class=".visible-xs-block, hidden-xs">
    <div class="content container-fluid">
        <div class="row">
            <div class="col-md-8">
                <!-- This is hidden from mobile view -->
                {% 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 is hidden from all other views (including desktop) -->
                {% block content %}
                {% endblock %}
            </div>
        </div>
    </div>
</div>

But Django raises an exception because it can only render 1 {% block content %} per template!

Any ideas how I can do this without using multiple blocks?

like image 398
TimJ Avatar asked Nov 14 '17 12:11

TimJ


People also ask

How many margins are present in CSS?

The four margin properties for each side of the box and the margin shorthand were all defined in CSS1. The CSS2. 1 specification has an illustration to demonstrate the Box Model and also defines terms we still use to describe the various boxes.

Is it OK to use negative margins CSS?

The short answer is no, as long as you are only using negative CSS margins to bring elements closer together than their box model properties would allow.

What is margin 10px?

One way to do this is to add margins, which will create space around one or more sides of the element. A simple margin declaration might look like this: margin: 10px; That declaration means that the margin box should extend 10px in all four directions—left, right, up, and down—beyond the size of the border box.

How do you make a responsive margin?

Define some width on your container and set margin top & bottom to some desired value and left & right values to auto so that it will always split the remaining space on the both sides equally.


1 Answers

example :

<div class="content">Some thing </div>

you want to give different margin based on size of display

than do this

In your common css :

@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 */ }
like image 103
Vaibhav Avatar answered Oct 13 '22 03:10

Vaibhav