【问题标题】:Background top-bar images Zurb Foundation and Flask template inheritance背景顶栏图像 Zurb Foundation 和 Flask 模板继承
【发布时间】:2019-08-28 23:06:30
【问题描述】:

我正在使用Flask 开发应用程序。作为前端框架,我使用的是Zurb-Foundation。我创建了一个layout.html 模板,我正在使用template inheritance 对其进行扩展。

网站的布局包括一个带有背景图片的顶栏,类似于this onelayout.html的相关代码如下:

<!doctype html>
<html class="no-js" lang="">

<head>
  <!-- More code here -->
</head>    
<body>
  {% block navbar %}
  <header class= {{ headerclass }}>
    <div class="contain-to-grid startup-top-bar">
        <nav class="top-bar" data-topbar>
        ...
        ...
        ...
  <!-- More code here -->

我想为与导航栏上的链接关联的每个网站页面设置不同的顶栏背景图像。顶栏背景图像取决于&lt;header class= {{ headerclass }}&gt;。为了让每个网站页面有不同的顶栏背景图像,我已经参数化了&lt;header class= {{ headerclass }}&gt;。参数{{ headerclass }} 的提供方式如下:

from flask import render_template
from appmodule import app

@app.route("/")
def index():
    return render_template('index.html', headerclass = 'index')

@app.route("/anotherchild")
def anotherchild():
    return render_template('anotherchild.html', headerclass = 'anotherchild')

if __name__ == "__main__" :
    app.run(port=5000, debug=True)

例如,扩展layout.htmlindex.html模板是这样的:

{% extends "layout.html" %}

{% block title %}Index{% endblock title %}

{% block content %}
    <h1>Index</h1>
    <p class="important">
      Hello world.
    </p>
{% endblock content %}

最后,为了生成styles.css,我编译了一些.scss 文件。定义类&lt;header class= {{ headerclass }}&gt; 样式的相关代码如下_main.scss 文件:

// Style for the index page
.index {
    background: url('../img/img1.JPG') no-repeat center center fixed;
    background-size: cover;
    .startup-hero {
        padding-top: rem-calc(150px);
        padding-bottom: rem-calc(150px);
        .hero-lead {
            color: darken(#fff, 30%);
        }
    }
  ...
  ...
  // more code here

// Style for the anotherchild page
.anotherchild {
    background: url('../img/img2.JPG') no-repeat center center fixed;
    background-size: cover;
    .startup-hero {
        padding-top: rem-calc(150px);
        padding-bottom: rem-calc(150px);
        .hero-lead {
            color: darken(#fff, 30%);
        }
    }
  ...
  ...
  // more code here

好消息是一切都按预期工作,但您可能已经注意到我违反了_main.scss 文件中的DRY 原则。我的问题是:我可以使用mixins 来解决问题吗?或者可以使用哪些不同的方法更合适?

【问题讨论】:

    标签: flask zurb-foundation scss-mixins


    【解决方案1】:

    如果您遇到的问题是样式重复 - 只需将这些样式提取到单独的类中即可。像这样的:

    .header-main {
        background-repeat: no-repeat;
        background-position:  center center;
        background-attachment: fixed;
        background-size: cover;
        .startup-hero {
            padding-top: rem-calc(150px);
            padding-bottom: rem-calc(150px);
            .hero-lead {
                color: darken(#fff, 30%);
            }
        }
    
    
    .index {
        background-image: url(../img/img1.JPG);
    }
    .anotherchild {
        background-image: url(../img/img2.JPG);
    }
    

    所以标记就变成了:

    <header class="header-main {{ headerclass }}">
    

    希望我正确理解了您的问题。

    【讨论】:

    • 谢谢@dmitrybelyakov 如果我完全按照您的想法将背景图像放置在正确的中心位置。但是,如果我将更多 css 添加到特定的 {{ headerclass }} 中,例如 .index { background: url('../img/img1.JPG') no-repeat center center fixed; background-size: cover; },它会完美运行。你有什么可能发生的原因吗?
    • 我想我知道为什么会发生这种行为。根据我所见,.css 代码是从上到下执行的。如果这是真的,最后执行的代码是 .index { background-image: url(../img/img1.JPG); } 但没有任何样式参数,如 centercover 等。你怎么看?
    • .index 仅定义了背景,因此如果您在 &lt;header&gt; 元素上同时使用这两个类,它应该只将该属性添加到 .header-main 中定义的任何内容。
    猜你喜欢
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2011-02-03
    相关资源
    最近更新 更多