htmlcssresponsive-design

How to Make Background Image Responsive with CSS?


I'm working on an FAQ section for my webpage, but I'm having trouble making its background responsive. It isn't adjusting well on different screen sizes.

html {
  height: 100vh;
  width: 100vw;
}

body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url('https://wallpapers.com/images/featured/1920-x-1080-night-city-y13hx1qucv0ztepv.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

h2 {
  text-align: center;
  font-weight: 600;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.container {
  min-height: 40%;
  max-width: 60%;
  border: 1px solid black;
  border-radius: 3%;
  background-color: white;
  padding: 0 10px 10px 10px;
}

.question {
  font-size: 1.1rem;
  padding: 0 0 15px 0;
  border-bottom: 2px solid black;
  cursor: pointer;
}

.question::after {
  content: "+";
  font-size: 1.4rem;
  position: absolute;
  right: 10px;
}

.answer {
  overflow: hidden;
  height: 0;
  transition: all 0.5s ease-in;
}

.question-container.active .answer {
  height: 40px;
}

.question-container.active .question {
  font-size: 1rem;
  transition: all 0.5s ease-in;
}

.question-container .active .question::after {
  content: "-";
  transition: all 0.5s ease-in;
}
<div class="container">
  <h2>Frequently Asked Questions</h2>
  <div class="question-container">
    <p class="question">What is the return policy?</p>
    <p class="answer">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veniam porro quidem eum praesentium officia voluptas repellendus amet minima alias voluptates beatae ea non, natus numquam sint, nam quasi aspernatur aliquam.</p>
  </div>
  <div class="question-container">
    <p class="question">When will I receive my order?</p>
    <p class="answer">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veniam porro quidem eum praesentium officia voluptas repellendus amet minima alias voluptates beatae ea non, natus numquam sint, nam quasi aspernatur aliquam.</p>
  </div>
  <div class="question-container">
    <p class="question">Where are you located?</p>
    <p class="answer">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veniam porro quidem eum praesentium officia voluptas repellendus amet minima alias voluptates beatae ea non, natus numquam sint, nam quasi aspernatur aliquam.</p>
  </div>
</div>

Issue:

The FAQ sections background isn't responsive.

What I've Tried:

Desired Outcome: I want the FAQ section to be fully responsive, adjusting smoothly across various screen sizes, without overflow or cramped content.

Can anyone help me figure out why my UI's background is not responsive and suggest improvements?

Below are the screenshots of how they look at different screen size i.e 1440px,1024px,425px respectively

enter image description here

enter image description here

enter image description here


Solution

  • It looks like you are missing a viewport meta tag, something like:

    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no" />
    

    This is just an example (user-scalable=no will probably get ignored due to accessibility concerns). The reason your snippet didn't have this behaviour is that Stack Overflow adds the meta tag itself to the snippet pages to keep their scaling consistent.