How to insert elements into a responsive screen

Asked 2 years ago, Updated 2 years ago, 398 views

When I set the PC size screen on the left to the smartphone size on the right, I want to put the green element between red and blue.
Please tell me how to do it.By the way, there is no code yet.Enter a description of the image here

html css

2022-09-30 22:05

1 Answers

Share the method using Grid.(Mobile First)

...
<div class="container">
  <div class="text-container red">text</div>
  <div class="image-container green">Image</div>
  <div class="text-container blue">text</div>
</div>
...
.container{
  display:grid;
  grid-template-columns —100px;
  grid-template-rows:repeat(3,100px);
}
/* Center text*/
.text-container,
.image-container{
  display:grid;
  place-items:center;
}
.text-container {
  color:white;
}
.red{
  background-color:red;
}
.green{
  background-color:greenyellow;
}
.blue{
  background-color:blue;
}

@media screen and (min-width:450px) {
  US>.container{
    grid-template-columns:repeat(3,100px);
    grid-template-rows:repeat(2,100px);
    grid-template-areas:
      "text-one image image"
      "text-two image";
  }
  .text-container.red{
    grid-area:text-one;
  }
  .image-container{
    grid-area:image;
  }
  .text-container.blue{
    grid-area:text-two;
  }
}

Demo codesandbox
https://codesandbox.io/s/stakoverflow-jp-resposible-mi3xk8?file=/style.css:0-736

mobile Mobile View

Desktop Desktop View


2022-09-30 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.