236
<div class="flex flex-col h-screen justify-between">
<header class="h-10 bg-red-500">Header</header>
<main class="mb-auto h-10 bg-green-500">Content</main>
<footer class="h-10 bg-blue-500">Footer</footer>
</div>
Class justify-between
is not required, but I would leave him (for other case).
So, play with h-screen
and mb-auto
classes.
And you get this UI:
113
Another approach would be using flex-grow.
<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.0.2/tailwind.min.css" rel="stylesheet"/>
<div class="flex flex-col h-screen">
<div class="bg-red-500">header</div>
<div class="bg-green-500 flex-grow">content</div>
<div class="bg-blue-500">footer</div>
</div>
- [Django]-Why is logged_out.html not overriding in django registration?
- [Django]-In Django, how does one filter a QuerySet with dynamic field lookups?
- [Django]-How to pull a random record using Django's ORM?
44
New solution in 2022. Tailwindcss version 3.0.24 in codepen demo.
<div class="min-h-screen">
<div>Content</div>
<div class="sticky top-[100vh]">Footer</div>
</div>
- [Django]-How do I access the request object or any other variable in a form's clean() method?
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
- [Django]-Make the first letter uppercase inside a django template
- [Django]-Pass extra arguments to Serializer Class in Django Rest Framework
- [Django]-Django QuerySet order
- [Django]-Django β limiting query results
25
Another way that was recently discovered by SΓlvio Rosa uses position: sticky
+ top: 100vh
on the footer. The way to achieve this with TailWindCSS isβ¦
<html class="min-h-screen">
<body class="min-h-screen">
<header>Header</header>
<main>Content</main>
<footer class="sticky top-[100vh]">footer</footer>
</body>
</html>
You can read the full article on CSS Tricks here
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-Passing STATIC_URL to file javascript with django
- [Django]-How to customize user profile when using django-allauth
8
None of the solutions here worked for me since my component wasnβt a layout component. I wanted a sticky footer to appear on just a single page and it needed to ignore the margins and padding of parent containers. Hereβs the tailwind solution that worked for me:
<div className="fixed bottom-0 left-0 bg-red-500 w-screen h-12">
Sticks to bottom, covers width of screen
</div>
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-What is the difference between null=True and blank=True in Django?
- [Django]-How to set True as default value for BooleanField on Django?
8
Sticky Header and Footer
For a sticky header and footer regardless of content and screen size, do this:
<div class="flex flex-col h-screen">
<div class="sticky top-0 z-50">header</div>
<div class="grow">content</div>
<div class="sticky bottom-0 z-50">footer</div>
</div>
- [Django]-Django return file over HttpResponse β file is not served correctly
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-Django JSONField inside ArrayField
6
A solution without using margin:
.as-console-wrapper {
display: none !important; /* just to hide stackoverflow console warning */
}
<script src="https://cdn.tailwindcss.com"></script>
<div class="flex flex-col min-h-screen">
<header class="bg-yellow-600">content</header>
<div class="bg-blue-600">content</div>
<div class="flex-1"></div> <!-- here -->
<footer class="bg-red-400">footer</footer>
</div>
Another so clever solution is using sticky top-[100vh]
for footer. by Chris Coyier
.as-console-wrapper {
display: none !important; /* just to hide stackoverflow console warning */
}
<script src="https://cdn.tailwindcss.com"></script>
<div class="min-h-screen">
<header class="bg-yellow-600">content</header>
<div class="bg-blue-600">content</div>
<footer class="bg-red-400 sticky top-[100vh]">footer</footer>
</div>
- [Django]-How can I set a default value for a field in a Django model?
- [Django]-How to run Django's test database only in memory?
- [Django]-Easiest way to rename a model using Django/South?
2
This has been a major pain for me recently. I ended up solving this without flex
, I simply gave my body a min-height of 75vh and it seems to have produced the desired result.
- [Django]-How to show processing animation / spinner during ajax request?
- [Django]-What is related_name used for?
- [Django]-In django do models have a default timestamp field?
2
Iβm using this:
<div class="min-h-screen flex flex-col justify-start">
<div>your main content</div>
<footer class="mt-auto">
<div>your footer content</div>
</footer>
</div>
- [Django]-Can't install via pip because of egg_info error
- [Django]-How to find pg_config path
- [Django]-Django connection to postgres by docker-compose
2
The best answer that I have seen was to set container, containing the footer, to screen height (βmin-h-screenβ) and make it a flexbox (βflexβ) and set the element above the footer to flex-1 so that it would consume all the spaces and push the footer below.
In your code, it would look like these:
<body class='flex flex-col min-h-screen'>
{% include "partials/nav.html" %}
<div class='flex-1'>
{% block content %}
{% endblock %}
</div>
{% include "partials/footer.html" %}
</body>
Hereβs a sample code for my use-case:
<div className='flex flex-col min-h-screen'>
<div className='flex-1 mx-24 mt-12'>
<Header />
<div className='grid grid-cols-4 gap-12 my-12'>
{data.map( (item, i) => <Todo key={i} title={item.title} note={item.note} texts={item.texts}/>)}
</div>
</div>
<Footer />
</div>
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
- [Django]-How to use subquery in django?
- [Django]-Using django-rest-interface
1
While I avoided using display: grid
for a long time, using it seems to be the simplest solution to date (compared to the flexbox solution):
<script src="https://cdn.tailwindcss.com"></script>
<!-- Wrapper element ('display: grid', 'grid-template-rows' defined) -->
<div class="min-h-screen grid grid-rows-[min-content_1fr_min-content] ">
<header class="bg-blue-200">Header</header>
<!-- Content element (display: grid) -->
<main class="grid bg-blue-300">Content</main>
<footer class="bg-blue-400">Footer</footer>
</div>
There are two key points:
-
min-h-screen
,grid
, andgrid-rows-[...]
have been applied to the wrapping element1. Consideringgrid-rows
, an arbitrary value has to be used (Tailwind v3 offers only evenly distributed rows). The element that should take up all available space needs to have1fr
value; the rest is up to you (min-content
might be the best choice). -
The element that is supposed to take up all the available space needs to have
grid
class.
1Alternatively, instead of min-h-screen
, h-full
can be used, but it needs to be applied to all of the wrapping elements, starting with <html/>
.
It is actually easier done than said. Take a look at the example above.
- [Django]-How to automate createsuperuser on django?
- [Django]-Django 1.5 β How to use variables inside static tag
- [Django]-Paginate relationship in Django REST Framework?
0
Use the h-[100vmin]
custom class on the first div inside your layout component, typically as follows:
<Layout>
<div class="container">
<div class="h-[100vmin]">
...
</div>
</div>
</Layout>
- [Django]-DRF: custom ordering on related serializers
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?
- [Django]-How to put comments in Django templates?
0
<footer class="absolute bottom-0 w-full px-6 py-6 text-white bg-emerald-500">
I think you can just use absolute bottom-0
to locate it, w-full
to fill the width, and px
and py
to size your footer .
- [Django]-How to run Django's test database only in memory?
- [Django]-Referencing multiple submit buttons in django
- [Django]-How to add multiple arguments to my custom template filter in a django template?
- [Django]-Django AutoField with primary_key vs default pk
- [Django]-Set up a scheduled job?
- [Django]-How to access the local Django webserver from outside world
0
Itβs a lot simpler than what Iβm seeing. Hereβs a sticky header and sticky footer. Always visible no matter how tall the content is:
<>
<div className="sticky top-0 min-w-full">Header</div>
<div className="min-w-full min-h-screen">SOME CONTENT</div>
<div className="min-w-full min-h-screen">SOME CONTENT</div>
<div className="fixed bottom-0 min-w-full">Footer</div>
</>
- [Django]-How do I use pagination with Django class based generic ListViews?
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
- [Django]-Django aggregate or annotate
0
Based on tailwind documentations, I used positionning like this:
<body>
<div class="static min-w-full min-h-screen p-1">
<p>content<p>
<div class="absolute bottom-0 min-w-full">
<p>foot</p>
</div>
</div>
</body>
static
andabsolute
allow to position child (footer) relatively to parent (content)min-h-screen
tells the parent is as high as the screenbottom-0
tells footer must be at the bottom of the parent
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-Is this the right way to do dependency injection in Django?
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
-1
<div class='relative'>
<div class='fixed bottom-0 right-10 cursor-pointer rounded-t-3xl bg-red-500 w-10 h-10 grid content-center justify-center'>
your text
</div>
</div>
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-What's the best Django search app?
- [Django]-Nginx doesn't serve static