【问题标题】:CSS background image won't show in HTML fileCSS 背景图像不会显示在 HTML 文件中
【发布时间】:2021-07-31 23:33:29
【问题描述】:

我是一名 CSS 初学者,我正在做我的第一个个人项目。我正在尝试使用文件中的背景图像创建一个 div,并在图像上放置一个 h1。但是,我这辈子都无法显示我的背景图片——我的 h1 也不会采用我在 CSS 文件中编写的样式。

关于我的代码可能有什么问题有什么想法吗?我已经阅读了几个这样的问答,但似乎没有一个解决方案适合我。

我还尝试在 jpg 文件名之前添加一个正斜杠,在文件名之前添加一个 .../,并且我尝试写出整个文件路径。这些都没有奏效。使用背景速记也无济于事。我尝试将 jpg 文件直接放在与 html 和 css 文件相同的文件夹中,并尝试将其放置在包含 html 和 css 文件的文件夹中的子文件夹中。没有变化。

这是我的 CSS 样式:

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}

#banner {
    width: 80%;
    height: 400px;
    background-image: url("veg-bowl.jpg");
    background-size: cover;
    background-position: top;
    margin: 0 auto;
}

h1 {
    font-size: 100px;
    text-align:center;
    text-shadow: black -6px 6px;
    font-family: 'Courier New', Courier, monospace;
    font-weight: lighter;
    color:white;
}

ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

button {
    color:#ffffff;
    background-color: #000;
    border: 1px solid #000;
    border-radius: 0;
    padding: 0.5em 0.75em 0.5em 0.75em;
    letter-spacing:1px /* needs to be last as not to interfere with other styles */
}

button:hover {
    color:#ffffff;
    background-color: rgb(64, 87, 66);
    border: 1px solid rgb(64, 87, 66);
    border-radius: 0;
    padding: 0.5em 0.75em 0.5em 0.75em;
    letter-spacing:1px /* needs to be last as not to interfere with other styles */
}

这是 HTML 标记:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Planta Vita</title>
    <link rel="stylesheet" href="vegan_res.css">
</head>
<body>
    <section id="banner">
        <h1>Planta Vita</h1>
        <h2>West Village</h2>
    </section>
    <div id="banner-2">
        <h2>Planta Vita</h2>
        <h3>West Village</h3>
    </div>
    <div id="about"> 
        <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Enim dolore neque aliquam, corrupti tempore debitis! Reprehenderit quasi corporis laboriosam aliquid aspernatur nemo sunt tempora numquam quos dolorem, error magni ex!</p>
        <p>Join Us:</p>
        <p>Mon-Thurs 1-11pm ; Fri-Sun 12-11pm</p>
    </div>
    <div id="menu">
        <h2>Menu</h2>
    </div>
    <div id="reservation">
        <button>Book</button>
    </div>
    <footer>
        <h3>Find Us</h3>
        <p>30 2nd Street</p>
        <p>New York NY 10004</p>
        <br>
        <p>Instagram: @PlantaVitaNYC</p>
        <br>
        <p>212-720-3340</p>
        <p>plantavita@gmail.com</p>
        <br>
        <br>
        <p>100% vegan, organic, non-GMO, and sustainable whenever possible</p>
    </footer>
</body>
</html>

【问题讨论】:

    标签: html css background css-selectors background-image


    【解决方案1】:

    你想要这个吗?:-

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Planta Vita</title>
        <link rel="stylesheet" href="vegan_res.css" />
        <style>
            * {
                margin: 0;
                padding: 0;
                border: 0;
                font-size: 100%;
                font: inherit;
                vertical-align: baseline;
            }
    
            /* HTML5 display-role reset for older browsers */
            article,
            aside,
            details,
            figcaption,
            figure,
            footer,
            header,
            hgroup,
            menu,
            nav,
            section {
                display: block;
            }
    
            body {
                line-height: 1;
            }
    
            #banner {
                width: 80%;
                height: 400px;
                background-image: url("https://cdn.pixabay.com/photo/2016/05/05/02/37/sunset-1373171__340.jpg");
                background-size: cover;
                background-position: top;
                margin: 0 auto;
            }
    
            h1 {
                font-size: 100px;
                text-align: center;
                text-shadow: black -6px 6px;
                font-family: "Courier New", Courier, monospace;
                font-weight: lighter;
                color: white;
            }
    
            ol,
            ul {
                list-style: none;
            }
    
            blockquote,
            q {
                quotes: none;
            }
    
            blockquote:before,
            blockquote:after,
            q:before,
            q:after {
                content: "";
                content: none;
            }
    
            table {
                border-collapse: collapse;
                border-spacing: 0;
            }
    
            button {
                color: #ffffff;
                background-color: #000;
                border: 1px solid #000;
                border-radius: 0;
                padding: 0.5em 0.75em 0.5em 0.75em;
                letter-spacing: 1px;
                /* needs to be last as not to interfere with other styles */
            }
    
            button:hover {
                color: #ffffff;
                background-color: rgb(64, 87, 66);
                border: 1px solid rgb(64, 87, 66);
                border-radius: 0;
                padding: 0.5em 0.75em 0.5em 0.75em;
                letter-spacing: 1px;
                /* needs to be last as not to interfere with other styles */
            }
        </style>
    </head>
    
    <body>
        <section id="banner">
            <h1>Planta Vita</h1>
            <h2>West Village</h2>
        </section>
        <div id="banner-2">
            <h2>Planta Vita</h2>
            <h3>West Village</h3>
        </div>
        <div id="about">
            <p> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Enim dolore neque aliquam, corrupti tempore
                debitis! Reprehenderit quasi corporis laboriosam aliquid aspernatur nemo sunt tempora numquam quos dolorem,
                error magni ex! </p>
            <p>Join Us:</p>
            <p>Mon-Thurs 1-11pm ; Fri-Sun 12-11pm</p>
        </div>
        <div id="menu">
            <h2>Menu</h2>
        </div>
        <div id="reservation">
            <button>Book</button>
        </div>
        <footer>
            <h3>Find Us</h3>
            <p>30 2nd Street</p>
            <p>New York NY 10004</p>
            <br />
            <p>Instagram: @PlantaVitaNYC</p>
            <br />
            <p>212-720-3340</p>
            <p>plantavita@gmail.com</p>
            <br />
            <br />
            <p>100% vegan, organic, non-GMO, and sustainable whenever possible</p>
        </footer>
    </body>
    
    </html>

    【讨论】:

    • 感谢您的回复,Groot,但不仅仅是复制某人的标记,我想知道我自己的标记可能缺少什么。无论如何,现在我认为问题不在于我的标记:我的浏览器 (Chrome) 突然拒绝显示我对 CSS 和 HTML 文件所做的任何 更改。我会自己解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 2017-06-25
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2019-02-27
    相关资源
    最近更新 更多