Can a div fill the entire screen in HTML with a background color?

I am having an issue with the responsiveness of my website. When in desktop mode, I have a centered div with a grey background color. However, in tablet mode, I would like the div and its content to remain centered but fill the entire width of the screen with grey. Is this achievable?

@media screen and (min-width: 767px) and (max-width: 1366px) {
  .backgroundGrey {
    background-color: #F2F2F2;
    /* hoping it's doable */
  }
}
<body>
  <section>
    <div class="flexCenter backgroundGrey">
      <div>
        <h2>Content<h2>
        
      </div>
    </div>
  </section>
</body>
EDIT with solution

Thank you everyone for your responses. After some trial and error, I managed to find a solution to my problem by adding a new div with the background color set to position absolute:

<body>
    <section>
        <div class="backgroundGreyFullWidth"></div>
        <div class="flexCenter">
            <div>
                <h2>Content</h2>
            </div>
        </div>
    </section>
</body>
@media screen and (min-width: 767px) and (max-width: 1366px) {

    .backgroundGreyFullWidth {
        background-color: #f2f2f2;
        position: absolute;
        z-index: -1;
        width: 100%;
        height: 650px;
        left: 0; 
        right: 0;
    }
}

Answer №1

To create a background with a grey color that covers the entire page, you can use either absolute or fixed positioning and anchor the element to each side. Here is an example:

.greyBackground {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: #F2F2F2;
  /* additional styles for positioning content within */
}

Answer №2

To achieve the desired result, utilize two separate div elements - one for setting the background color and the other for maintaining the content.

Refer to the code snippet below:

<body>
   <section>
      <div class="flexCenter backgroundGrey">
         <div class="content">
            <h2> Content </h2>
         </div>
      </div>
   </section>
</body>

@media screen and (min-width: 767px) and (max-width: 1366px) {
   .backgroundGrey {
       background-color: #F2F2F2;
       width : 100%;
       height : 100%;
   } 
   .content{
       width:200px;
   }
}

Answer №3

Make sure to utilize width and height at 100%

.backgroundGrey {
       background-color: #F2F2F2;
       width:100%;
       height:100vh;
       display:flex;
       justify-content:center;
       align-items:center;
   }
<div class="backgroundGrey">content</div>

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

Make sure the font size on your iPhone matches the font size on other web browsers

Once upon a time, I came across an insightful article detailing a method to size text using CSS that is compatible with multiple browsers. The approach outlined in the article includes: body { font-size:100%; line-height:1.125em; /* 16×1.125=18 * ...

One Div is Blocked by the Navigation Bar Div

I've been hard at work on my webpage, creating a top navigation bar with links and more. However, I've hit a snag while trying to add a settings button. Everything seems fine until I try to position it, then the button stops working. I suspect it ...

The footer fails to appear even after inspecting the element following the successful submission of my PHP email form

After implementing a PHP mail form on my website, I noticed that upon page load after filling out the email form, my footer does not appear and the tags are missing from the inspect element. The pages linked below show the issue: edit: Please note that th ...

generate a customized synopsis for users without storing any data in the database

In order to provide a summary of the user's choices without saving them to the database, I want to display it in a modal that has already been created. Despite finding some sources online, none of them have worked for me so far. Below is my HTML: &l ...

Curious about how the .delay() function works in this particular case with jQuery?

What is the reason behind using .delay(3000) as the optimal value to achieve a well-synced 1-second effect with .fadeOut(), rather than adding delays like this: .delay(3000) .delay(4000) .delay(5000)? The specific part in question: $(document).ready(func ...

Generating PDFs with multiple pages using android.graphics.pdf

Currently, I am facing an issue while attempting to generate a PDF using android.graphics.pdf. The problem arises when dealing with multiple pages in the document. While I can provide android.graphics.pdf with HTML content that could be converted into a PD ...

Tips for incorporating CSS classes into a React component

Currently, I am iterating through an array of data to create a list. My goal is to add a CSS class when a specific list item is clicked. Using Iteration for List Creation let sportsList = allSports.sportList.map((sport) => { return ( <SportItem i ...

When hovering over elements, my li tags undergo a transformation

I am working on a simple menu code, which is just a list of items. ul li { position: relative; top: 55px; height: 30px; background: #212; display: inline-block; width: 29%; margin: 1%; margin-bottom: 0; padding: 5px; -webkit-border-t ...

Steps to transfer Excel data to an HTML document

Apologies for my imperfect English, I'm currently working on a project involving HTML5, JavaScript, and CSS3. My task involves importing an Excel file into an HTML table. Any assistance you could provide would be greatly appreciated. Thank you in adva ...

Developing a new class within a function

In this section of the HTML file, I have set up a form to collect email and password information from new users in order to create a new profile: <form name="userInfo"> <fieldset> <legend>Create a new account</legend> ...

Tips on utilizing a local file as a background image URL

How can I modify my CSS to link to a local file path like C:\xampp\htdocs\Folder instead of the URL https://source.unsplash.com/1600x1050/?nature. I am currently using Bootstrap 4 and below is my CSS code: .jumbotron{ background: ...

Importing CSS and JavaScript files into a JSP page in Spring MVC

After adding a CSS file to my JSP file, I encountered an issue that prompted me to search for solutions. Despite trying the tutorial from the following link, it did not work for me: My project was created using Spring Tool Suite and here is my file struct ...

Assigning a default value to a drop-down menu based on the result of an SQL query in real time

Appreciate your help in advance. I am currently working on a project using codeIgniter with PHP. I have a drop-down menu that needs to display the default value as the element returned from a MySQL query, while also displaying the other options available ...

Changing the action of a form dynamically with Javascript and Selenium in a Java environment

I am having trouble updating the action attribute of a form using JavaScript and Selenium in Java. Here is the snippet of HTML from the site: </div> <div class="contionue-shopping-wrapper"> <form class="co-formcontinueshopping" action="htt ...

Control the playback of multiple HTML5 videos with the ability to pause and resume, while also adding a

Trying to achieve the functionality of playing and pausing multiple videos. When one video is played, the others should automatically pause. The videos should appear blurred, except when actively playing. Does this make sense? http://jsfiddle.net/1frjov8e ...

Changing the color of a div's text based on its content with CSS

I am facing a challenge in styling the text inside a “div” element using a Javascript function. Initially, when the page loads, these “divs” contain no value. The values of the "divs" will change between "Open" and "Closed" twice a day when the Jav ...

When you zoom in on the website, the appearance remains consistent in the middle

Hello, I need your assistance in solving a problem. I am trying to create a website where the zooming functionality focuses on the center of the web browser instead of the upper left corner. An example of what I'm looking for is Blizzard's Starcr ...

CSS hover effects are malfunctioning

Having an issue with hover in CSS. Attempting to create a menu bar using saved PNG files, each file within its own div. When applying a class name and hover modifier, it only works on the first element. As the pointer moves to subsequent elements, they are ...

Is there a way to bring to life the addClass() and removeClass() jQuery functions through animation?

I am currently developing a website and I want to be able to toggle the visibility of sections by using the ".hidden" bootstrap class within click events. Here is the basic code snippet: $('selector').click(function(){ $('*part-to-hi ...

How can you customize the default select box in a Django template using a passed parameter?

I have a situation where I am passing a dictionary to a Django template and need to set a default value for a select box. The context is that this template serves as an 'edit product' page for a previous 'add product' form. When the use ...