2

#modal {
  background-color: #f0f0f0;
  //border: thin red solid;
  height: 500px;
}
#category_main {
  width: 75%;
  height: 200px;
  border: thin red solid;
  display: block;
  margin: 0 auto;
  margin-top: 5%;
}
<div id="modal">
  <div id="category_main"></div>
</div>

When I run then , margin-top is not working. the div category_main has come down with the div in which it is presented. I don't want that white part. instead of , only that red bordered box should be affected to margin-top. enter image description here

Thank You.

Any help would be grateful.

Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
Bhavin Shah
  • 2,462
  • 4
  • 22
  • 35
  • Possible duplicate of [Why does this CSS margin-top style not work?](http://stackoverflow.com/questions/9519841/why-does-this-css-margin-top-style-not-work) – Vucko Aug 01 '16 at 10:21

5 Answers5

2

Instead of applying margin-top to #category_main, you should apply padding-top to #modal, as the former leaves a gap but the latter includes the same:

#modal{
    background-color: #f0f0f0;
    //border: thin red solid;
    height: 500px;
    padding-top: 5%;
}

See the demo here

Shashank
  • 2,010
  • 2
  • 18
  • 38
0

Add overflow:hidden to the parent so that the parent gain its real height you specified..

Here is the snippet

#modal{
        background-color: #f0f0f0;
        /*border: thin red solid;*/
        height: 500px;
        overflow:hidden;
}

  #category_main{
                width: 75%;
                height:200px;
                border: thin red solid;
                margin: 0 auto;
                margin-top: 15%;
            }
<div id="modal">
    <div id="category_main"></div>
</div>

NOTE

In css comments should be passed in between /*.......*/ this, not //....

M.Tanzil
  • 1,987
  • 1
  • 12
  • 28
0

I think you have this the wrong way around. you want to put padding on the outer div.

 padding-top: 5%;
Glenn Packer
  • 212
  • 1
  • 8
0

use this css for #modal,

vertical-align:middle;

display: table-cell;

#modal {
  background-color: #f0f0f0;
  //border: thin red solid;
  height: 500px;
  width: 500px;
  vertical-align: middle;
  display: table-cell;
}
#category_main {
  width: 75%;
  height: 200px;
  border: thin red solid;
  display: block;
  margin: 0 auto;
  margin-top: 5%;
}
<div id="modal">
  <div id="category_main"></div>
</div>
Community
  • 1
  • 1
Mahendra Kulkarni
  • 1,437
  • 2
  • 26
  • 35
0

Really if u dont want the white space as you desired, just add padding:5% to the main div..May b this will solve your problem

#modal {
  background-color: #f0f0f0;
  //border: thin red solid;
  height: 500px;
  padding: 5%;
}
#category_main {
  width: 75%;
  height: 200px;
  border: thin red solid;
  display: block;
  margin: 0 auto;
 
}
<div id="modal">
  <div id="category_main"></div>
</div>

#modal {
  background-color: #f0f0f0;
  //border: thin red solid;
  height: 500px;
  padding:5%;
}
#category_main {
  width: 75%;
  height: 200px;
  border: thin red solid;
  display: block;
  margin: 0 auto;

}
<div id="modal">
  <div id="category_main"></div>
</div>
Ganesh Putta
  • 2,622
  • 2
  • 19
  • 26