简体   繁体   中英

Set same height and responsiveness for div only css

I have two divs and they should be the same size. I tried to put a height it works in desktop size but when I use different platform it become a mess

 ____________
|  ___________                 
| |         | |
| |         | |  
| |         | |
|_|_________  |
  |___________|

 .custombox { text-align: center; padding: 10px; position: absolute; border: 1px solid; } .customparentbox { background: red } 
 <div class="customparentbox"> <div class="custombox"> <p>Sample text</p> <button>test</button> </div> </div> 

Here's my working jsFiddle .

You have to add height and width to your parent div element and also to the child div. I have added box-sizing in the style so that it will render the actual width and height of the element.

Add position:relative to the parent element.

 *{ box-sizing: border-box; margin: 0; padding: 0; } .customparentbox{ background: red; position: relative; height: 80px; width: 100px; } .custombox{ text-align: center; padding-top: 10px; position: absolute; border: 1px solid; left: 10px; top: 10px; height: 80px; width: 100px; background: #fff; } 
  <div class="customparentbox"> <div class="custombox"> <p>Sample text</p> <button>test</button> </div> </div> 

I've made some changes in your css

From

.custombox{
  text-align: center;
  padding: 10px;
  position: absolute;
  border: 1px solid;}
  .customparentbox{background:red}

To

.custombox {
  text-align: center;
  padding: 10px;
  position: relative;
  top:10px;
  left:10px;
  border: 1px solid;
}

.customparentbox {
  width:100px;
  background: red;
  border:1px solid green;
}

Here is a jsfiddle I made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM