1

I try to use TailwindCSS and CSS (as static), but TailwindCSS utilities override the static CSS styles. I tried moving TailwindCSS invocation tags after, before static CSS invocation, but it didn't work.

Whenever I remove TailwindCSS from my template, the static CSS starts working.

How can I use both? I need to use CSS file to change properties in Django forms.

{% load tailwind_tags %}
{% tailwind_css %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %} {% endblock %}</title>

    {% load static %}
    <link rel="stylesheet" href="{% static 'base.css' %}">
</head>
<body>
    <div class="bodydiv">
        {% block body %}
        {% endblock %}
    </div>
</body>
</html>
body{
    background-color: aqua;
}

settings.py

"""
Django settings for tweeterproject project.

Generated by 'django-admin startproject' using Django 5.1.2.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-9piqti35i-q&85=cebmg*td+g=ut#f$k+t0cttav^_sb8izu-)'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tweeter',
    'tailwind',
    'theme'
]

TAILWIND_APP_NAME = 'theme'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tweeterproject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'tweeterproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
5
  • 1
    I don't understand. v3 or v4? With what command are you generating the TailwindCSS output file, or how are you integrating it into Django? Commented Nov 16 at 11:56
  • Using standard method - pypi.org/project/django-tailwind version 4 Commented Nov 16 at 12:19
  • How about adding !important to the CSS? Commented Nov 16 at 16:14
  • No, I tried adding important, but didn't solve Commented Nov 17 at 6:23
  • The issue cannot be reproduced. The example does not include the necessary native CSS or TailwindCSS styling. We have no way of knowing what overrides what, because the error cannot be reproduced from the provided code snippet. Fundamentally, TailwindCSS v4 uses CSS layers, and unlayered CSS is always the strongest. The purpose of TailwindCSS utilities is to be declared inline in the HTML, but you did not do that. Commented 2 days ago

1 Answer 1

1

Maybe try using !important within the CSS. It always overrides the TailwindCSS code within my own code! So it should work with yours to :)

body{
    background-color: aqua !important;
}

(Only use the !important tag when you really need it, otherwise your code will look messy and it wil be harder to find something)

New contributor
ManoukBislick is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.