【问题标题】:How to use div for background in html如何在html中使用div作为背景
【发布时间】:2021-05-30 07:17:01
【问题描述】:

您好我正在尝试建立一个网站,我正在尝试使用particles.js 作为我的背景并以叠加方式显示内容。但是,它显示在页面顶部而不是背景上。当我将其位置设置为绝对位置时,它会改变我网站的格式。如何将那个 div 设置为背景?

当我将其设置为绝对格式时,它如何改变网站的格式

我的背景 div id 设置为 particles-js 这是我的 base.html 代码:

    <!DOCTYPE html>
<html lang=en>

{% load static %}
{% static 'style.css' %}
{% static 'particles.json' %}

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    
</head>

<body>

{% include 'snippets/base_css.html' %}
{% include 'snippets/header.html' %}



<!-- Body -->
<style type="text/css">
    .main{
        min-height: 100vh;
        height: 100%;
    }
</style>
<div class="main">
    <div id="particles-js"></div>
    <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
      
    <script>
        particlesJS.load('particles-js', "{% static 'particles.json' %}", function(){
            console.log('particles.json loaded...');
        });
    </script>
    {% block content %}

    {% endblock content %}
</div>
<!-- End Body -->

{% include 'snippets/footer.html' %}

</body>


</html>

这是我的 home.html:

    {% extends 'base.html' %}
{% block content %}
<style type="text/css">

    @media (max-width: 768px) { 
        .right-column{
            margin-left: 0px;
        }
    }

    @media (min-width: 768px) { 
        .right-column{
            margin-left: 20px;
        }
    }

    .blog-post-container{
        margin-bottom: 20px;
        width: 100%;
    }
    .create-post-bar{
        background-color: #fff;
        margin-bottom:20px;
    }

    .left-column{
        padding:0px;
    }

    .right-column{
        padding:0px;
    }
    .lead{
        font-size: 17px;
        color: #000220;
        text-align: center;
    }
</style>



<!-- main content -->
<div class="container">
    <div class="row">
        

        <!-- blog feed -->
        <div class="left-column col-lg-7 offset-lg-1">

            <!-- Top 'create post' bar -->
            <div class="d-lg-none mb-3">
                <div class="card m-auto d-flex flex-column p-3">
                    <img class="img-fluid d-block m-auto pb-2" src="{% static 'covalent.png' %}" width="72" height="72">
                    <p class="m-auto"><a class="btn btn-primary" href="{% url 'blog:create' %}">Create post</a></p>
                </div>
            </div>
            <!-- end Top 'create post' bar -->

            <!-- Blog posts-->
            {% if blog_posts %}
                {% for post in blog_posts %}
                    <div class="blog-post-container">
                        {% include 'blog/snippets/blog_post_snippet.html' with blog_post=post %}
                    </div>
                {% endfor %}
            {% else %}
                <div class="blog-post-container">
                    {% include 'blog/snippets/blog_post_snippet.html' with query=query %}
                </div>
            {% endif %}
            <!-- End Blog posts-->

        <!-- Pagination -->
        {% include 'blog/snippets/blog_post_pagination.html' with blog_posts=blog_posts %}

        </div>
        <!-- end blog feed -->


        <!-- Right 'create post' column  -->
        <div class="right-column col-lg-3 d-lg-flex d-none flex-column">

            <div class="card create-post-bar d-flex flex-column p-3">
                <img class="img-fluid d-block m-auto pb-2" src="{% static 'covalent.png' %}" width="72" height="72">
                <p class="lead">Welcome to the HUB</p>
                <p class="m-auto"><a class="btn btn-primary" href="{% url 'blog:create' %}">Create post</a></p>
            </div>
        
        </div>
        <!-- end Right 'create post' column  -->



    </div>
</div>





{% endblock content %}

这是我的 style.css

    <style type="text/css">


    body{
        height: 100%;
        background-color: #000220;

    }
    #particles-js{
         height: 100% ;

 
        }


</style>

【问题讨论】:

  • 对特定的 div 应用否定的 z-index,例如 z-index: -1;
  • 具有position: absolute;(或除static 之外的任何位置)的元素始终相对于最近的非静态父元素定位自身。因此,由于您的&lt;div&gt; 直接位于&lt;body&gt; 内,因此它相对于主体定位自身(默认情况下定位为relative)。尝试用position: relative; 将你的背景和覆盖在另一个&lt;section&gt; 中(使用部分只是为了使它在语义上正确的html),你可以给它100vh100vwheightwidth 以进一步使它全屏横幅效果(可选)。
  • @deekeh,它做了我想要的,但改变了网页的格式。我只是希望该 div 显示在后台而不更改我的内容的位置
  • @tacoshy 它只是把 div 放到了底部。

标签: javascript html css django particles.js


【解决方案1】:

你可以试试这个,但我不确定这是否适合你。

* {
  padding: 0;
  margin: 0;
}

#background-div {
  position: absolute;
  z-index: -1;
  background: linear-gradient(#f7f4eb, #ded9cc);
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
<div id="background-div">
  <!--background-content-->
</div>
<div class='container'>
  your other contents
</div>

【讨论】:

  • 请对您的回答给出充分的解释。 code only 答案被认为是低质量的。
  • 好的,我会尝试,我是堆栈溢出的新手
  • 当我尝试这个方法时,它不会影响格式,但它会在内容而不是背景上显示 div
  • 它做到了!谢谢!
猜你喜欢
  • 2021-02-23
  • 2021-08-18
  • 2015-10-25
  • 2023-03-14
  • 2018-12-14
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多