Suggestions for creating a <div> that takes up the entire space when using the display property set to flex

html,
body {
  height: 100%;
  margin: 0px;
}

.container {
  display: flex;
  width: 100%;
  height: 100%;
  background-color: yellow;
}

.box {
  flex-shrink: 0;
}
<div>
  <div class="container">
    <div class="box">
      <h1>A</h1>
      <p>clap</p>
    </div>
    <div class="box">
      <h1>W</h1>
      <p>clap</p>
    </div>
    <div class="box">
      <h1>S</h1>
      <p>clap</p>
    </div>
    <div class="box">
      <h1>D</h1>
      <p>clap</p>
    </div>
    <div class="box">
      <h1>Q</h1>
      <p>clap</p>
    </div>
    <div class="box">
      <h1>E</h1>
      <p>clap</p>
    </div>
    <div class="box">
      <h1>R</h1>
      <p>clap</p>
    </div>
  </div>
</div>

How can I make the (.container) full screen with a flex display?

I am trying to achieve full window coverage in both height and width using the flex property, but setting height to 100% does not fill the entire window. Grid layout seems to cover the full window, how can I accomplish this with flex?

Answer №1

If you want to prevent the flex items from shrinking, make sure to set shrink to 0:

.container {
    display: flex;    
    width: 100%;
    height: 100%;
    background-image: url("asso-myron-HZCBJmlUPas-unsplash.jpg");
}

.flex-item {
    flex-shrink: 0;
}

Here is an example:

.flex-container {
  display: flex;
  width: 100%;
  height: 100%;
}

.flex-item {
  flex-shrink: 0;
}
<div class="flex-container">
      <div class="flex-item">
        1
      </div>       
    </div>

Answer №2

Here are some sample codes for you to try.

body {
  margin:0;
}

.container {
    height:100vh;
    background-repeat:no-repeat;
    background-size: 100% 100%;
    object-fit:cover;
    display: flex;
    background-image: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSxD1r0WianEq0iQy_H8INxLhdpmcz6c_DslA&usqp=CAU");
}
<body>
<div class="container"></div>
</body>

Answer №3

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="d-flex p-3 bg-secondary flex-shrink-0 text-white">
    <div class="p-2 bg-info">Flex item 1</div>
  </div>

</body>

</html>

If this is not what you are looking for, please provide the HTML code.

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

"Encountered an error while trying to access properties that

Struggling to create a practice menu with the functionality of making elements appear and disappear on click? If you are encountering issues with a class named "option" not working as expected, you are not alone. Clicking on nested objects like images or i ...

Is there a way to extract the unicode/hex representation of a symbol from HTML using JavaScript or jQuery?

Imagine you have an element like this... <math xmlns="http://www.w3.org/1998/Math/MathML"> <mo class="symbol">α</mo> </math> Is there a method to retrieve the Unicode/hex value of alpha α, which is &#x03B1, using JavaScrip ...

What is the best way to ensure that a <div> extends all the way to the bottom?

My goal is to have this element reach the bottom of the screen, but I also have a header which complicates things. Setting height: 100%; or 100vh results in the page being too big and scrollable. I don't want to manually calculate it because of respon ...

Is it possible to utilize the <hgroup> element with various <h> heading levels?

<hgroup> <h4>Role</h4> <h3>Graphic Designer</h3> <h5>Company: Godigital</h5> </hgroup> Should I keep this structure, or should I use the <h3><h4><h5> order instead? ...

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

Enhance mix-blend mode or filtering effects in SCSS using an SVG component in VueJS

I am currently developing a fixed navbar at the top of a webpage with several SVGs inside anchor tags. I want the SVGs to appear black when displayed over lighter colored divs (other Vue components) and white when displayed over darker colored ones. The ba ...

Can ChatGPT Service Error be resolved?

I tried using chatGPT to help me with my HTML code, but every time I opened it I received an error message saying "Failed to get service" Here is the code that I want to make work: <html> <head></head> <body> <script& ...

Is it possible to align a clip-path starting from the right edge?

On my webpage, I have a unique hamburger icon that is positioned 2rem away from the right and top edges. I am looking for a convenient way to create a clip path that grows from its center point. Calculating the center point is not difficult but unfortunate ...

What causes a custom element to suddenly crumble?

I am attempting to create a custom field that can display either an SVG or a Canvas, but the rendering is not as expected. I anticipate two boxes that are 400 pixels wide and 300 pixels high, however, they appear to collapse in an unusual manner. How can I ...

Changing the width of an HTML table does not produce any noticeable results

I attempted to create a striped table with 5 columns. The desired column sizes are: 10% width --- 10% width --- 40% width --- 20% width --- 20% width However, my current code is not achieving the correct widths (the 'description' column does n ...

Saving sortable portlet items to a database using JQuery AJAX

I've searched through numerous resources to find a solution to my problem, but none of them have been successful. Here is the code I am working with: http://plnkr.co/edit/pcfz33lzHz4wdehjGEuQ I am trying to utilize AJAX to save the order of two port ...

Guide to aligning a div element along the right edge of the webpage

I'm struggling to align this div all the way to the right side of the screen. Below is the HTML: <html> <head> title></title> <link rel="stylesheet" type="text/css" href="../css/style.css"/> </head> <h1&g ...

The Evolution of Bulma's Navigation Menu

Creating a transparent menu in Bulma has been successful for the desktop viewport: VIEW DESKTOP MENU However, when attempting to implement the same design on mobile, the menu ends up like this: VIEW MOBILE/TABLET MENU The mobile version seems to inheri ...

Basics of CSS border-image explained

Although it seems simple, I'm struggling to figure out what the issue might be. I am attempting to create a div with a specific border on an ASP.Net webpage. CSS: .ResourcesDiv { border-image:url(http://blogrope.com/wp-content/uploads/2013/06/003-w ...

How do I go about transferring a file using PHP?

I'm working on file uploads and saving them in a directory. However, I'm facing an issue: when a file is denied it needs to be deleted from the directory. This part is sorted out. But what if the file is approved? Should I use rename() or move_up ...

Automated logout feature will be enabled if no user interaction is detected, prompting a notification dialog box

Here is my working script that I found on this site. After a period of idle time, an alert message will pop up and direct the user to a specific page. However, instead of just the alert message, I would like to implement a dialog box where the user can ch ...

Why won't my picture track the movement of the cursor?

I am trying to make my image follow the cursor using a specific code, but it doesn't seem to be working. I'm having trouble figuring out what's wrong with it. Here is the code snippet that I'm currently using: function followIt(e) ...

Presenting JSON data in a table format on-the-fly even without prior knowledge of the data's structure or field names

When working with my application, I will be receiving data in JSON format and I am looking to showcase this data in a tabular form within a web browser. It's important to mention that I won't know beforehand the structure or field names of the re ...

Trouble with CSS3 Perspective rendering issue

Here's a simple example showcasing the use of CSS3 Perspective property to create a 3D side flip effect. However, this basic demonstration does not seem to achieve the desired outcome. <html> <head> <style> .div1{height:300px; ...

What steps can be taken to automatically set a button in a navigation bar to active when its corresponding route is clicked on?

I'm trying to implement a feature in my react app where an SVG button in the navbar changes color when its route is active or switches colors based on which button's route was selected. For example, clicking on the settings button would change it ...