How can Django implement dynamic CSS for a single item that changes its background dynamically?

I am looking to create a single CSS style that can dynamically change its background based on a Django variable. This becomes more challenging when dealing with a:hover.

The initial static style.css code appears as follows:

.titles-button h2 a {
    margin-left: 4em;
    margin-right: 4em;
    margin-top: 1em;
    margin-bottom: 1em;
    display: block;
    width: 240px;
    height: 80px;
    /* background: transparent url(../logos/djangoVX_logo.png) center left no-repeat; */
    text-indent: -999em;
    border: 1px dashed green;
}
.titles-button h2 a:hover {
    /* background: transparent url(../logos/djangoVX_logo2.png) center left no-repeat; */
}

Note that the background is currently commented out.

I want to implement this style within a DIV and update the background dynamically, like this:

{% if the_apps_list %}
    {% for apps in the_apps_list %}
    <div class="titles-button"> 
        <h2><a href="/{{ apps.app_name }}/" style="X">{{ apps.app_name }}</a></h2> 
    </div>
    {% endfor %}
{% else %}
    <p>No APPS are available. Internal Error.</p>
{% endif %}

I have tried my best with "X" but haven't been successful in making it work.

My requirements include: - Rewrite the entire style in X - Avoid using complex JavaScript solutions (on_mouse_over...) - Dismiss the use of a class: inheritALL

None of these solutions align with my goal of utilizing Django effectively.

I may be approaching the problem from the wrong angle...

... but which is the correct approach?

Cheers, F

Answer №1

If you have two distinct backgrounds - one dark and the other light, your code might resemble the following:

scripts.js:

...
if (preferring_dark_theme) {
    settings['theme'] = 'dark';
} else {
    settings['theme'] = 'light';
}

index.html:

<div id="main-content" class="{{ theme }}">
    ...
</div>

styles.css:

#main-content.dark h2 a {
    background: ...;
}
#main-content.dark h2 a:hover {
    background: ...;
}

#main-content.light h2 a {
    background: ...;
}
#main-content.light h2 a:hover {
    background: ...;
}

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

Transform all characters to lowercase, then capitalize them using only CSS

I came across a discussion on this topic at: First lowercase the text then capitalize it. Is it possible with CSS? However, the solution provided was not purely based on CSS. I have a div that contains the following text: <div> RaWr rAwR </div&g ...

Utilizing the map function in React to create a button reference

I am currently facing an issue that is relatively simplistic and doesn't have any real-world application. My goal is to locate a 'green' button and make it blink for a duration of 3 seconds. How can I achieve this using React? const btnLay ...

Difficulty with Bootstrap's media object

When commenting on my blog, the media object is used as shown here: . However, if the comment contains a large object, the media object ends up overlapping the sidebar like in this example: . Any suggestions on how to resolve this issue? Thank you! ...

Adding an image within the body of text in a Django model, where both the text and image coexist

I am currently seeking a method to seamlessly insert an image within the text of my Django-powered blog. My goal is to achieve a layout similar to the one showcased in this example: https://i.stack.imgur.com/cFKgG.png The desired layout consists of two c ...

Is it possible to save the project by generating incremental JSON diffs?

In the process of developing a web-based paint program, I have implemented a system where the user's artwork state is saved as a json object. Each time an addition is made to the client's undo stack (which consists of json objects describing the ...

Slide the menu off to the right

Check out these images to see my progress: Main Image. When you click on the menu, everything shifts right. I've managed to set up the push menu, toggle switch menu, and main divs. Now I need help with centering the 3 lines that are clicked within th ...

Encountering a Django Ajax HTTP 500 Error

I'm struggling to integrate Django with AJAX requests, and I keep getting an HTTP 500 error related to MultiValueDictKeyError even though everything seems fine? I passed in 3 variables: sendEmail, username, error When accessing request.POST on the 3 ...

Adjusting the size of all elements on a webpage

Upon completing my project, I noticed that my localhost:3000 is zoomed in at 125%, causing it to appear less than ideal at 100% zoom. Is there a way to adjust the zoom/scale of my website to match how it appeared on my localhost environment? I came across ...

Ways to increase the spacing between content boxes using Bootstrap

I currently have a row of three Bootstrap boxes structured like this: <div class="row"> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </div> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </di ...

A collapsible select list with checkboxes for children items

I am currently developing a website using Vue.js, HTML, and SCSS. I am looking to implement a drop-down functionality similar to the animated example provided in the gif below: https://i.stack.imgur.com/Mia2D.gif The gif demonstrates how the drop-down me ...

The Boostrap background is failing to display as expected in CSS, and the `position: fixed` property is

I am struggling to add an extra class within the div class= "container-fluid" in order to set a background with a fixed position, but it doesn't seem to work. The only way I can add a background-image is directly in the HTML. Why is that? Another is ...

What is the best way to incorporate Django settings into my logging configuration file, logging.ini?

Have you come across the BASE_DIR setting in my settings.py file? BASE_DIR = os.path.dirname(os.path.abspath(__file__)) I'm faced with the task of incorporating this variable into my logging.ini file to configure the paths for my file handlers. The ...

Rotate arrows by 90 degrees instead of 180 degrees in Bootstrap 5 Accordion

I have customized a Bootstrap 5 Accordion according to the guide provided at https://getbootstrap.com/docs/5.0/components/accordion/. The default behavior rotates the arrows 180 degrees when an item is opened, changing from 'v' to '^'. ...

My online platform appears fine across all browsers except for Firefox

It seems that different browsers are displaying my website differently. Chrome, Safari, and Opera show it without any issues, but Firefox removes the top section, including the menu, and replaces it with white space in the center. I'm not sure what&ap ...

Php/JavaScript Error: Identifier Not Found

I've double-checked my code multiple times, but it still doesn't seem to be working properly. When the PHP runs, the browser console displays the following error: Uncaught SyntaxError: Unexpected identifier. I'm not sure if this is a si ...

Dynamic HTML gallery that supports the addition of fresh images

Looking to showcase images from a local folder in a slideshow on a simple web page? The page should automatically update when new images are added or removed from the folder. I am not an expert and prefer a minimalist design with perhaps just a transitio ...

Replace existing styled-component CSS properties with custom ones

One of my components includes a CheckBox and Label element. I want to adjust the spacing around the label when it's used inside the CheckBox. Can this be achieved with styled()? Debugging Info The issue seems to be that the className prop is not b ...

Resolving CSS glitches that mysteriously vanish in Internet Explorer versions 8 and 9

My website was created using WordPress, with various plugins added to enhance its functionality. However, I have encountered an issue when viewing the site in Internet Explorer 8 & 9. The visual effects of the plugins seem to lose all CSS styling in IE bro ...

Grouping by the grandparent in Django DRF when using one-to-one relationships

As a newcomer to Django, the transition from SQL to the Django DRF/ORM has been both exciting and challenging for me. I'm currently grappling with understanding how to calculate the sum of a field within a class while grouping it by its parent's ...

The font type is glitched, Internet Explorer is displaying the incorrect font

Is there a problem here? Below is the additional CSS provided: @font-face { font-family: NeutraText-Book; src: url('../fonts/NeutraText/NeutraText-Book.eot'); /* IE9 Compat Modes */ src: url('../fonts/NeutraText/NeutraText-Book ...