【问题标题】:How to use a background image in a div?如何在 div 中使用背景图片?
【发布时间】:2018-05-16 20:19:59
【问题描述】:

我在将图像添加到 div 时遇到问题。我将文件和图像都放在同一个文件夹中,并且图像是 jpg。我也试过把在线图片链接(http://www.aussieinfrance.com/wp-content/uploads/stockholm-4.jpg)放进去,但没有成功。这是我的html文件:

<!DOCTYPE html>
    <html>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
       <body>
            <div class="topnav" id="myTopnav">
  <a class="active" href=“#home”>Sweden</a>
  <a href=“government.html”>Government</a>
  <a href=“borders.html”>Borders</a>
  <a href="#about">Iconography</a>
</div>
<div class="overview" id="overview">
<p>Sweden is a northern country that specializes in trading machinery, paper products, and iron and steel products</p>
</div>
</body>
</html

这是我的 css 文件:

/* Add a black background color to the top navigation */
.topnav {
    background-color: #262228;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

/* Style the links inside the navigation bar */
.topnav a {
    font-family: verdana;
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 25px 27px;
    text-decoration: none;
    font-size: 19px;
}

/* Change the color of links on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}

/* Add a color to the active/current link */
.topnav a.active {
    background-color: #FFCE00;
    color: white;
}

body {
  margin: 0;
  padding: 0;
  font-family: verdana;
}

.overview p {
    font-size: 20px;
    padding-top: 30px;
    padding-right: 50px;
    padding-left: 550px;
    color: #262228;
    text-align: right;
}

.overview {
      background-image: url(“stockholm.jpg”);
}

【问题讨论】:

    标签: html css image


    【解决方案1】:

    您引用的图像位于 style.css 文件所在的同一目录中。如果不是,则指定图像的路径相对路径,例如background-image: url('./img/stockholm.jpg');(如果文件位于 img 文件夹中)。请参阅相对和绝对文件路径声明以了解。希望能帮助到你。谢谢。

    【讨论】:

      【解决方案2】:
      1. 在 CSS url 指令中使用纯引号 " 而不是印刷引号 “”
      2. 在 CSS 中,图像的路径是相对于 CSS 文件而不是 HTML 文件指定的。因此,请确保图片与 CSS 文件位于同一文件夹中。
      3. 尝试使用./stockholm.jpg 而不是stockholm.jpg

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-14
        • 2021-06-24
        • 1970-01-01
        • 2016-03-24
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多