为什么我的图像没有对尺寸调整做出反应,而其他一切都响应?

时间:2019-03-27 03:32:49

标签: html css css3

https://imgur.com/a/b667X1Y

我正在为我的IT课程做一个项目。我在使网站及时响应方面遇到困难。 我的徽标和导航栏似乎都响应,但是,房子的图像似乎不响应。 (上面的链接是90%缩放)

我尝试过编辑宽度和高度的大小,但是由于我很陌生,所以我正在努力解决该问题。

我希望最上面的图像能够响应。我该怎么做?

@charset "utf-8";
/* CSS Document */
body {
	margin: 0px;
}
/*This is the container css*/
.container {
	margin-left: 200px;
	margin-right: 200px;
	margin-top: 0;
	box-sizing: border-box;
}

.logo {
	position: relative;
	display: inline-block;
}

.logo h1 {
	margin-top: 20px;
	margin-bottom: 0;
	color: #232323;
	font-family:Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", "monospace";
}

.navbar {
	float: right;
	top: 0;
	padding: 10px;
}

.navbar ul a:hover {
	padding: 5px;	
	margin: 0;
	background-color: lightblue;
}

.navbar li {
	display: inline-block;
	text-decoration: none;
	padding: 10px;
	color: black;
	font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", "monospace";
}

/*This is the top image css*/
#top-img {
	background-image: url("images/top-img.jpg");
	background-position: center;
	min-width: 100%;
	height: 500px;
	position: relative;
	background-repeat: no-repeat;
	border-bottom: solid #3E3E3E 3px;
}

/*This is the @media i am trying to make the top image responsive*/
@media screen and (min-width: 40em) {
    #top-img {
        max-width: 50em;
    }
}

@media screen and (min-width: 64em) {
    #top-img {
        max-width: 70em;
    }
    
}
<html>
<head>
<meta charset="utf-8">
<title>Plantscape</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body>
	<div id="top-img">
	<div class="container">
	<header>
		<div class="logo">
			<h1>Plantscape</h1>
		</div>
		<div class="navbar">
			<ul>
				<a href="portfolio.html"><li>Folio</li></a>
				<a href="services.html"><li>Services</li></a>
				<a href="about.html"><li>About Us</li></a>
				<a href="contact.html"><li>Contact Us</li></a>
			</ul>
		</div>
	</header>
	</div>
	</div>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

您正在将图片用作背景,而不是HTML元素。您已告诉#top-img元素进行响应,但尚未告诉背景进行响应。尝试将以下声明添加到CSS:

 #top-img {
     ...
     background-size: cover;
 }

答案 1 :(得分:0)

尝试为您的background-size:cover类添加top-img,以适合页面的整个宽度。

@charset "utf-8";

/* CSS Document */
* {
    box-sizing:border-box;
}
body {
    margin: 0px;
}


/*This is the container css*/

.container {
    margin-left: 200px;
    margin-right: 200px;
    margin-top: 0;
    box-sizing: border-box;
}

.logo {
    position: relative;
    display: inline-block;
}

.logo h1 {
    margin-top: 20px;
    margin-bottom: 0;
    color: #232323;
    font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", "monospace";
}

.navbar {
    float: right;
    top: 0;
    padding: 10px;
}

.navbar ul a:hover {
    padding: 5px;
    margin: 0;
    background-color: lightblue;
}

.navbar li {
    display: inline-block;
    text-decoration: none;
    padding: 10px;
    color: black;
    font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", "monospace";
}


/*This is the top image css*/

#top-img {
    background-image: url("https://via.placeholder.com/1200x400");
    background-position: center;
    background-size: cover;
    min-width: 100%;
    height: 500px;
    position: relative;
    background-repeat: no-repeat;
    border-bottom: solid #3E3E3E 3px;
}


/*This is the @media i am trying to make the top image responsive*/

@media screen and (min-width: 40em) {
    #top-img {
        max-width: 50em;
    }
}

@media screen and (min-width: 64em) {
    #top-img {
        max-width: 70em;
    }
}
<div id="top-img">
    <div class="container">
        <header>
            <div class="logo">
                <h1>Plantscape</h1>
            </div>
            <div class="navbar">
                <ul>
                    <a href="portfolio.html">
                        <li>Folio</li>
                    </a>
                    <a href="services.html">
                        <li>Services</li>
                    </a>
                    <a href="about.html">
                        <li>About Us</li>
                    </a>
                    <a href="contact.html">
                        <li>Contact Us</li>
                    </a>
                </ul>
            </div>
        </header>
    </div>
</div>

答案 2 :(得分:0)

我检查了您的代码,发现您正在使用房屋图像作为背景图像,因此我的建议是将css属性background-size:cove用作类名#top-img,并在创建时删除高度移动响应式布局,那个时候提到height:auto。

suggested css{

#top-img {
   float:left;
   width:100%;
   background-image: url("images/top-img.jpg");
    background-position: center;
    min-width: 100%;
    height: 500px;
    position: relative;
    background-repeat: no-repeat;
    border-bottom: solid #3E3E3E 3px;
    background-size:cove;
}