css - Django动态css实现: only one item changes (background) dynamically.怎么办?

标签 css django dynamic

我想设置一个动态获取背景的单一CSS样式 来自一些 django 变量。

我认为增加的困难在于它与 a:hover 有关。

原始静态 style.css 代码运行如下:

.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; */
}

注意背景现在已被注释。

我想在 DIV 中使用这种样式并动态更新背景。

喜欢:

{% 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 %}

我为“X”尽了最大努力,但没有结果。

除了: - 在 X 中重写完整样式 - 使用一些 javascript 花哨的东西(on_mouse_over ...) - 我什至见过一个类:inheritALL

这些解决方案都不符合我使用 django 的想法。

我可能会从错误的一边得到公牛......

..哪一个是右边?

干杯 F

最佳答案

假设您有“深色”和“浅色”版本的背景。您的代码可能如下所示:

views.py:

...
if i_want_dark_version:
    context['style']='dark'
else:
    context['style']='light'

template.html:

<div id="titles-button" class="{{ style }}">
    ...
</div>

样式.css:

#titles-button.dark h2 a {
    background: ...;
}
#titles-button.dark h2 a:hover {
    background: ...;
}

#titles-button.light h2 a {
    background: ...;
}
#titles-button.light h2 a:hover {
    background: ...;
}

关于css - Django动态css实现: only one item changes (background) dynamically.怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6021509/

相关文章:

css - 在 chrome 中设置 css constant() 或 env() 变量以进行调试

javascript - 在底部对齐一个div

html - 如何强制 html 表格的 "td"元素内的单独元素以内联方式显示(从左到右)?

mysql - Django计算期初余额sql查询

ios - 适用于 ios 的 Android 应用程序包(动态功能)?

javascript - 在浏览器中具有精确的图像缩略图大小

查找字典期间的Python字典和循环混淆

html - 多列下拉菜单内联 block 不起作用

java - 不使用数据库的 Java 动态 XML 分解

class - Angular.js 中的动态类