26

I want to put one element under another element. I am using position: absolute in CSS.

 .first{
     width:70%;
     height:300px;
     position:absolute;
     border:1px solid red;
 }
.second{
    border:2px solid blue;
    width:40%;
    height:200px;
}
    <div class="first"></div>
    <div class="second"></div>

I want the blue box to be positioned under the red box. How could I achieve this?

stevelove
  • 3,194
  • 1
  • 23
  • 28
khalid J-A
  • 574
  • 1
  • 7
  • 19

6 Answers6

34

just give position : relative to second div and top:315px or what ever you want

.first{
     width:70%;
     height:300px;
     position:absolute;
     border:1px solid red;
 }
.second{
    border:2px solid blue;
    width:40%;
    height:200px;
 position: relative;
    top: 315px;
}
<html>
<head>

</head>
<body>
<div class="first"></div>
<div class="second"></div>
</body>
</head>
Mudassar Saiyed
  • 1,146
  • 10
  • 20
11

Here is a solution:

.first{
     width:70%;
     height:300px;
     position:absolute;
     border:1px solid red;
     box-sizing: border-box;
}
.second{
    position: relative;
    border:2px solid blue;
    width:40%;
    height:200px;
    top: 300px;
    box-sizing: border-box;
}
<div class="first"></div>
    <div class="second"></div>

And you can to not point position, because div is block element and will be placed at new line by default.

.first{
     width:70%;
     height:300px;
     border:1px solid red;
 }
.second{
    border:2px solid blue;
    width:40%;
    height:200px;
}
<div class="first"></div>
<div class="second"></div>
Banzay
  • 9,310
  • 2
  • 27
  • 46
5

Try using clear: both;.

The clear property specifies on which sides of an element floating elements are not allowed to float.

https://www.w3schools.com/cssref/pr_class_clear.asp

jumpOnCommand
  • 97
  • 2
  • 8
1

Alternatively, you can put "display: block;" in the class style description and it will cause elements of that class to take up the whole width of the screen. So an element created after an element that has "display: block" will automatically be created below it.

 .first{
     display: block;
     width:70%;
     height:300px;
     border:1px solid red;
 }
.second{
    display: block;
    border:2px solid blue;
    width:40%;
    height:200px;
}
<div class="first"></div>

<div class="second"></div>
Slip
  • 11
  • 2
0

One way to position a div under another div is to put the second div inside the first div and style the second div with top: 100%; and position: absolute;. The first div should have position: relative;

.first{
     width:70%;
     height:300px;
     position: relative;
     border:1px solid red;
 }
.second{
    border:2px solid blue;
    width:100%;
    height: 200px;
    position: absolute;
    top: 100%;
    left: 0;
}
<html>
<head>

</head>
<body>
<div class="first">
  <div class="second"></div>
</div>

</body>
</head>
Pinqode
  • 71
  • 3
  • 10
0

use grid layout in parent class. And then add a property place-items: center;. Happy coding :).

sethugs63
  • 1
  • 2
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '23 at 13:01