【问题标题】:New background image for each MDlite card每个 MDlite 卡的新背景图像
【发布时间】:2017-03-31 00:56:28
【问题描述】:

第一次在 StackOverflow 上发帖,但它是长期用户。我正在尝试向我的网站添加两张 MDlite 卡,每张卡都有不同的背景图片,但最后声明的背景图片始终显示两者。很感谢任何形式的帮助。这是完整的代码,您可以在任何浏览器中查看问题(抱歉格式化,在我的手机上执行此操作):

                <!doctype html>

            <html lang="en">
            <head>
                <meta charset="utf-8">
                <title>Website Title</title>
                <meta name="description" content="Percula Creative">
                <meta name="author" content="Percula Creative Author">
                <link href="https://fonts.googleapis.com/css?family=Roboto:300,400" rel="stylesheet">
                <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
                <link rel="stylesheet" href="https://code.getmdl.io/1.2.1/material.deep_purple-orange.min.css" />
                <script defer src="https://code.getmdl.io/1.2.1/material.min.js"></script>  
                <meta name="viewport" content="width=device-width, initial-scale=1.0">

            <!--[if IE]><link rel="shortcut icon" href="favicon.ico"><![endif]-->
                <link rel="icon" href="favicon.png">
            </head>
            <body>
                <!-- Uses a transparent header that draws on top of the layout's background -->
            <style>
            .demo-layout-transparent {
              background: url('../assets/demos/transparent.jpg') center / cover;
            }
            .demo-layout-transparent .mdl-layout__header,
            .demo-layout-transparent .mdl-layout__drawer-button {
              /* This background is dark, so we set text to white. Use 87% black instead if
                 your background is light. */
              color: white;
            }
            </style>

            <!-- Always shows a header, even in smaller screens. -->
            <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
              <header class="mdl-layout__header">
                <div class="mdl-layout__header-row">
                  <!-- Title -->
            <a href="index.html" style="text-decoration: none; color:white" class="mdl-layout-title">Website Title</a>
                  <!-- Add spacer, to align navigation to the right -->
                  <div class="mdl-layout-spacer"></div>
                  <!-- Navigation. We hide it in small screens. -->
                  <nav class="mdl-navigation mdl-layout--large-screen-only">
                    <a class="mdl-navigation__link" href="apps.html">Apps</a>
                    <a class="mdl-navigation__link" href="">About</a>
                    <a class="mdl-navigation__link" href="">Contact</a>
                  </nav>
                </div>
              </header>
              <div class="mdl-layout__drawer">
                <span class="mdl-layout-title">Website</span>
                <nav class="mdl-navigation">
                  <a class="mdl-navigation__link" href="">Apps</a>
                  <a class="mdl-navigation__link" href="">About</a>
                  <a class="mdl-navigation__link" href="">Contact</a>
                </nav>
              </div>
              <main class="mdl-layout__content" style="background-color: #EEEEEE;">
                <div class="page-content" >


                    <div style="width:80%; max-width:780px; margin: 0 auto; padding: 16px">
            <!-- Wide card with share menu button --> <style> .demo-card-wide.mdl-card { width: 100%; } .demo-card-wide > .mdl-card__title { color: #fff; height: 176px; background: URL('http://www.imagenspng.com.br/wp-content/uploads/2015/07/minions-03.png') center / cover; } .demo-card-wide > .mdl-card__menu { color: #fff; } </style> 
            <div class="demo-card-wide mdl-card mdl-shadow--2dp""> 
            <div class="mdl-card__title"> 
            <h2 class="mdl-card__title-text"></h2> </div> 
            <div class="mdl-card__supporting-text">
            <p>Lorem Ipsum dolor1...</p>
            </div> 
            <div class="mdl-card__actions mdl-card--border"> 
            <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect" href="">Download</a> 
            </div> 

            <div class="mdl-card__menu"> 
            <button class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect"> 
            <i class="material-icons">share</i> 
            </button> 
            </div>

            </div>
            </div>




                    <div style="width:80%; max-width:780px; margin: 0 auto; padding: 16px">
            <!-- Wide card with share menu button --> <style> .demo-card-wide.mdl-card { width: 100%; } .demo-card-wide > .mdl-card__title { color: #fff; height: 176px; background: URL('http://i2.mirror.co.uk/incoming/article6379795.ece/ALTERNATES/s615b/Minion.jpg') center / cover; } .demo-card-wide > .mdl-card__menu { color: #fff; } </style> 
            <div class="demo-card-wide mdl-card mdl-shadow--2dp""> 
            <div class="mdl-card__title"> 
            <h2 class="mdl-card__title-text"></h2> </div> 
            <div class="mdl-card__supporting-text">
            <p>Lorem Ipsum dolor2...</p>
            </div> 
            <div class="mdl-card__actions mdl-card--border"> 
            <a class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect" href="">Download</a> 
            </div> 

            <div class="mdl-card__menu"> 
            <button class="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect"> 
            <i class="material-icons">share</i> 
            </button> 
            </div>

            </div>
            </div>


                </div>
              </main>
            </div>


            </body>
            </html>

【问题讨论】:

    标签: html css material-design material-ui


    【解决方案1】:

    这是因为您在页面中内联了两次 CSS,第二次覆盖了第一次。这行 CSS 会影响两个 div,而不仅仅是第一个或第二个。

    .demo-card-wide > .mdl-card__title { color: #fff; height: 176px; background: URL('http://www.imagenspng.com.br/wp-content/uploads/2015/07/minions-03.png') center / cover; } 
    

    您需要创建两个单独的规则,如果您希望每个 div 中有两个不同的背景图像,则每个 div 一个。

    简而言之,请执行以下操作:

    1) 删除整个第二个&lt;style&gt;...&lt;/style&gt;

    2) 向第二个 div 添加一个新类,使其变为

    <div class="mdl-card__title second"> 
    

    3) 像这样向您的第一个 &lt;style&gt;...&lt;/style&gt; 块添加新的 css 规则

    .demo-card-wide > .mdl-card__title.second { color: #fff; height: 176px; background: URL('http://i2.mirror.co.uk/incoming/article6379795.ece/ALTERNATES/s615b/Minion.jpg') center / cover; } 
    

    【讨论】:

    • 谢谢!您的解决方案完美运行。不幸的是,我对 StackOverflow 太陌生,所以我无法将此标记为正确答案,但非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多