提问者:小点点

背景-图像在div元素中不起作用


不要在div中使用类nav-item工作background-image。所有映像都在同一个目录中。页眉工作中的背景。有了img src一切都很好。问题出在哪里?

我什么都试过了。

null

header {
    background: url('backSmall2.png');
    height: 670px;
}
 
img {
    width: 14%;
    transform:skewY(-1deg);
    margin-top: 40px;
    margin-left: 40px;
    box-shadow:  0 0 10px rgba(0,0,0,0.5);
    transform: rotate(-3deg);
}
 
.nav-item {
    width: 12%;
    background-image: url('frame.png');
    margin-top: 80px;
    margin-left: 140px;
}
 
.logo-contacts {
    background-image: url('frame.png');
}
 
.contacts {
    width: 100%;
    height: 300px;
    background-color: aqua;
}
<!DOCTYPE html>
<html>
    <head>
    <link href="normalize.css" rel='stylesheet'>
    <link href="index-style.css" rel='stylesheet'>
    <link href='https://fonts.googleapis.com/css?family=Roboto+Condensed&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
    <script src="jquery-2.2.1.js"></script>
    </head>
    <body>
        <header>
            <img src="logo1.png">
            <div class="nav-item"></div>
            <a href="#contacts" class="logo-contacts"> </a>
        </header>
        <a name="contacts">
            <div class="contacts">
            </div>
        </a>
    </body>
</html>

null


共2个答案

匿名用户

您应该为background-image提供有关位置的更多信息。

例如:

.nav-item {
    width: 12%;
    background-image: url('frame.png');
    background-position: center; /* Added */
    background-size: cover; /* Added */
    background-repeat: no-repeat; /* Added */
    margin-top: 80px;
    margin-left: 140px;
}

有关background属性的详细信息,请访问Mozilla Developer Network。

匿名用户

问题是您的div是空的--所以--它没有高度。尝试向CSS类添加高度:

.nav-item {
    width: 12%;
    background-image: url('frame.png');
    margin-top: 80px;
    margin-left: 140px;
    height: XXXpx;
}