Почему мое изображение не реагирует на изменение размера, но все остальное реагирует?

#html #css

#HTML #css

Вопрос:

https://imgur.com/a/b667X1Y

Я делаю проект для своего курса по ИТ. У меня возникли проблемы с адаптивностью веб-сайта. Мой логотип и моя панель навигации кажутся отзывчивыми, однако изображение дома, похоже, не реагирует. (Ссылка выше увеличена на 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>  

Ответ №1:

Вы используете изображение в качестве фона, а не HTML-элемент. Вы указали, что ваш #top-img элемент должен быть отзывчивым, но вы не указали, что фон должен быть отзывчивым. Попробуйте добавить следующее объявление в свой CSS:

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

Комментарии:

1. Потрясающе! Большое спасибо, я буду иметь это в виду в следующий раз.

Ответ №2:

Попробуйте добавить 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>  

Ответ №3:

Я проверил ваш код и обнаружил, что вы используете изображение дома в качестве фонового изображения, поэтому я предлагаю, пожалуйста, использовать свойство 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;
}