Tips for Creating a Responsive Background Image Using CSS

I've been struggling to make the background of my FAQ section on the website responsive. It doesn't seem to adapt well to 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>

Difficulty:

The challenge lies in making the background of the FAQ section responsive.

  • I've attempted using max-width, min-height, and percentages, but it hasn't resolved the issue of layout adjustment across various screen sizes.
  • The content either spills over or appears congested on smaller screens.

Attempts Made:

  • Implemented max-width and min-height for the container.

  • Set dimensions as percentages.

  • Incorporated media queries to modify styles based on different screen sizes.

Objective: I aim for the FAQ section to seamlessly adapt to diverse screen sizes, without any overflow or cramped appearance.

If anyone can assist me in identifying the reason why the UI background fails to respond and propose enhancements, it would be greatly appreciated.

Displayed below are screenshots depicting the section's appearance at 1440px, 1024px, and 425px respectively.

https://i.sstatic.net/KcJMZyGy.png

https://i.sstatic.net/JslVyb2C.png

https://i.sstatic.net/wrQBJ3Y8.png

Answer №1

Seems like the viewport meta tag is missing, you should consider adding something similar to this:

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

This example includes (user-scalable=no, but for accessibility reasons it may be ignored). The absence of this tag in your snippet could be due to Stack Overflow automatically adding it to maintain consistent scaling on their pages.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accessing geolocation on a mobile web browser: A step-by-step guide

I have implemented HTML5 geolocation functionality using code I found in the documentation: getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(this.showPosition, this.showError); } else { alert("Geoloc ...

apply CSS to highlight a clicked div as if it were a radio button

Is it possible to use CSS to highlight a div when clicked (when the div is acting as a label for a radio button)? Here is my code: <?php foreach ($images as $image) { ?> <input type="radio" name="id" value="<?php echo $image['id& ...

Center the p tag vertically

To ensure the text inside the p tag aligns vertically in the middle, I've set a specific height for the tag. While this works perfectly for single-line text, it shifts to the top of the p tag when there are two lines of text. It's important to k ...

What is the best way to retrieve AJAX post data from the request in Symfony2?

My goal is to efficiently upload a file using jquery-file-upload (blueimp) and include some data in a cross-domain environment. To achieve this, I have utilized code from the basic.html file provided by jqueryfileupload on the client side. Additionally, I ...

Adding an asterisk to mark a required field in Angular reactive form inputs is a simple task that can be accomplished with just a

Currently, I am in the process of developing an application that utilizes a reactive dynamic angular form. The fields for this form are retrieved from an API request and generated dynamically. I have encountered the need to include a 'required field& ...

Incorporate different courses tailored to the specific job role

https://i.stack.imgur.com/iGwxB.jpg I am interested in dynamically applying different classes to elements based on their position within a list. To elaborate: I have a list containing six elements, and the third element in this list is assigned the class ...

Background image remains unaffected by HTML

I'm looking to update the look of a website by changing the background color to a picture. The CSS code I am using is as follows: .bg { opacity: 5; background-repeat: no-repeat; background-size: cover; background-position: 50% 100%; ...

Is the homepage the only page rendering correctly?

After working on an MVC project for over 6 months, I consistently faced a problem with HTML and CSS. The homepage always rendered correctly, as shown here: http://prntscr.com/tucp1 However, all other pages had strange white spaces between words, like in t ...

Is it possible to transfer the data from the previous row's input to the input of a new row?

Greetings to the StackOverflow community, I am currently utilizing a table form with a specific query: Here is an abridged version of my form structure: <script> $('#addrowbutton').click(function() { $('tr:hidden:first').show(); ...

Adjustable height and maximum height with overflow functionality

Currently, I am in the process of developing a task manager for my application and facing an obstacle when trying to calculate the height of a widget. My goal is to determine the maximum height (assuming a minimum height is already set) by subtracting a ce ...

Aligning text inputs inline within a Bootstrap form for a cleaner and organized

I am currently working on designing a form using Bootstrap, and I have a specific requirement where the first name and last name fields need to be displayed inline while the rest of the form remains unchanged. Although I have managed to make the inline fu ...

There was an issue encountered while working with Node.js: Unable to load resource - the server returned a 404 error (Not Found)

Hello everyone, I hope you're all well. I have a question regarding creating a navigation bar and serving it on my server. Every time I try to do so, I am getting a 404 not found error. However, I have placed my logo file in the same directory as my H ...

Tips for making a scrollable container that allows its contents to overflow without clipping?

Currently working on implementing a horizontal card row for my website with a unique hover effect - the cards lightly rotate and raise on hover. Here is a preview: https://i.sstatic.net/eDQ59.gif To make this row responsive, I added overflow-x: auto; to e ...

Internet Explorer reveals hotspots

Is there a way to hide the polygonal hotspots in Internet Explorer when using a picture with them? I want to prevent them from being visible. Thank you! To see an example, visit this link in IE and select one: ...

Modify the icon in the header of MaterializeCSS Collapsible when it is opened

I'm struggling to figure out how to change the icon of a toggled collapsible element. I have been reviewing their documentation but am having trouble making it work as intended. $('.collaps_roles_permission').collapsible({ accordion: tr ...

Erase the border around the color selection field

Here's a demonstration: http://jsfiddle.net/WpJRk/ I've incorporated a color picker into my webpage using the new input type "color": <input type="color"> However, I've noticed that there is an unwanted black border within the eleme ...

Utilizing a box within a background image using HTML and CSS

In the midst of my college project, I've encountered a hurdle. There's an image I need to overlay with a box that has padding on all sides corresponding to the outline of the image. Despite my attempts, I haven't been successful in achieving ...

I am encountering challenges and confusion with CSS

I am encountering an issue with my code that is causing some difficulties. The goal is to create a basic div with left, right, and center panes. The left pane loads perfectly, but the right pane, which has the same css styling, displays its children with a ...

z-index not functioning properly

Currently, I'm diving into the world of custom map creation using Leaflet and Mapbox. Everything was going smoothly until I encountered a challenge with the popups. Here's the issue: I have a unique navigation/control panel that I want to displa ...

Tips for displaying a section of an image similar to the style seen on the website 9gag.com

Is there a way to display only a portion of an image (in this case, to hide the watermark) similar to how it is done on 9gag.com? I attempted to use clip in CSS, but it requires the image to be positioned absolutely, which is not ideal for me. Are there a ...