skip to Main Content

Is it possible to resize a div with absolute position?

I want this pink modal to keep the same width of the input & resize with it.
This modal will be used in a dropdown component. So, should be resized as a whole with input.

By the way, is absolute position a correct choice for such a use case?

<!DOCTYPE html>
<html>
  <head>
    <title>My CSS Grid</title>
    <style>
      body {
        color: #fff;
        font-family: "Nunito Semibold";
        text-align: center;
      }
      .page {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-auto-rows: minmax(100px, auto);
        grid-gap: 10px;
        max-width: 960px;
        margin: 0 auto;
        grid-template-areas:
          "header header header"
          "aside comp main";
      }
      .page > * {
        background: #3bbced;
        padding: 10px;
      }

      .header {
        grid-area: header;
      }
      .comp {
        grid-area: comp;
      }
      .input_cell {
        display: grid;
      }
      .input {
        flex: 1;
      }
      .modal_container {
        margin-top: 20px;
        flex: 1;

        background-color: pink;
        position: absolute;
      }
    </style>
  </head>
  <body>
    <div class="page">
      <div class="header">Header</div>
      <div class="comp">
        <div class="input_cell">
          <input class="input" />
          <div class="modal_container">Modal</div>
        </div>
      </div>
    </div>
  </body>
</html>

2

Answers


  1. you didn’t have to use absolute position. just by giving width: 100% to both input and modal, problem is solved, no need to display grid.

    <!DOCTYPE html>
        <html>
          <head>
            <title>My CSS Grid</title>
            <style>
              body {
                color: #fff;
                font-family: "Nunito Semibold";
                text-align: center;
              }
              .page {
                display: grid;
                grid-template-columns: repeat(3, 1fr);
                grid-auto-rows: minmax(100px, auto);
                grid-gap: 10px;
                max-width: 960px;
                margin: 0 auto;
                grid-template-areas:
                  "header header header"
                  "aside comp main";
              }
              .page > * {
                background: #3bbced;
                padding: 10px;
              }
    
              .header {
                grid-area: header;
              }
              .comp {
                grid-area: comp;
              }
              .input_cell {
              /*nothing !*/
              }
              .input {
                width: 100%;
                padding:0;
                margin-bottom: 20px
              }
              .modal_container {
    width: 100%;
                background-color: pink;
              }
            </style>
          </head>
          <body>
            <div class="page">
              <div class="header">Header</div>
              <div class="comp">
                <div class="input_cell">
                  <input class="input" />
                  <div class="modal_container">Modal</div>
                </div>
              </div>
            </div>
          </body>
        </html>
    Login or Signup to reply.
  2. <!DOCTYPE html>
    <html>
      <head>
        <title>My CSS Grid</title>
        <style>
          body {
            color: #fff;
            font-family: "Nunito Semibold";
            text-align: center;
          }
          .page {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-auto-rows: minmax(100px, auto);
            grid-gap: 10px;
            max-width: 960px;
            margin: 0 auto;
            grid-template-areas:
              "header header header"
              "aside comp main";
          }
          .page > * {
            background: #3bbced;
            padding: 10px;
          }
    
          .header {
            grid-area: header;
          }
          .comp {
            grid-area: comp;
            position: relative;
          }
          .input_cell {
            display: flex;
            position: relative;
          }
          .input {
            flex: 1;
          }
          .modal_container {
            position: absolute;
            left: 0;
            width: 100%;
            background-color: pink;
            margin-top: 20px;
            flex: 1;
          }
        </style>
      </head>
      <body>
        <div class="page">
          <div class="header">Header</div>
          <div class="comp">
            <div class="input_cell">
              <input class="input" />
              <div class="modal_container">Modal</div>
            </div>
          </div>
        </div>
      </body>
    </html>

    Set .comp,.input_cell position relative. Styling .modal_container width 100%

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search