1

I have a div inside another div and I want this second div to ignore the padding, the second div to have full width and no margin bottom. How can I make it work?

#first {
  padding: 10px;
  border: 1px solid #000;
}

#second {
  background-color: red;
  color: #fff;
}
<div id="first">
  first div with 10px padding
  <div id="second">
    no padding
  </div>
</div>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
RGS
  • 4,062
  • 4
  • 31
  • 67
  • I dont believe this is possible. And i dont understand why you would ever wanna do it Please read up on padding – Anoxy Oct 28 '15 at 08:43
  • Not sure if that exactly what you want, but you can add negative margin to second div `margin: -10px;` – Igal S. Oct 28 '15 at 08:45

7 Answers7

2

Inner element can compensate parents padding by using negative margin.

#second {
    background-color:red;
    color:#fff;
    margin: -10px;
}

http://jsfiddle.net/n5yx8903/1/

Māris Kiseļovs
  • 16,957
  • 5
  • 41
  • 48
  • Wonder why this got down voted. [**Negative margin**](http://www.w3.org/wiki/The_CSS_layout_model_-_boxes_borders_margins_padding#Negative_margins) is totally valid. May i suggest doing `margin: 0 -10px -10px;` to avoid the the elements overlapping each other. +1 – DavidDomain Oct 28 '15 at 08:56
0

wrap text in p or span tag

    <div id="first">
    <p>first div with 10px padding</p>
    <div id="second">
        no padding
    </div>
</div>

set margin for element wrapping text, this will be the cleanest solution.

https://jsfiddle.net/n5yx8903/

pankijs
  • 6,531
  • 3
  • 16
  • 13
0

Set margin-left and margin-right to -10px.

#second{
    background-color:red;
    color:#fff;
    padding: 0;
    margin-left: -10px;
    margin-right: -10px;
}

Here is Fiddle.

Noopur Dabhi
  • 1,867
  • 25
  • 38
0

Well CSS is not intended to do this,

you may do this with negative margin, but as you see in the fiddle, it will then overflow: http://jsfiddle.net/n5yx8903/6/

#first{
    padding:10px;
    border:1px solid #000;
}
#second{
    background-color:red;
    color:#0ff;
    margin:-10px;
}

Maybe this SO answer will help you understand this problem better:Why does CSS not support negative padding?

Community
  • 1
  • 1
Xavjer
  • 8,838
  • 2
  • 22
  • 42
0

That is not doable in CSS. I think when that happens there are two options you can do.

  1. Use negative margin to compensate the padding
  2. Restructure the HTML

I would choose the latter one whenever possible and use negative margin as a fallback option.

#container {
  border: 1px solid #000;
}

#first {
  padding: 10px;
}

#second {
  background-color: red;
  color: #fff;
}
<div id="container">
  <div id="first">
    first div with 10px padding
  </div>
  <div id="second">
    no padding
  </div>
</div>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
-1

You can just put "first div with 10px padding" text into another div:

#first {
  border: 1px solid #000;
}
#second {
  background-color: red;
  color: #fff;
}
#inner {
  padding: 10px;
}
<div id="first">
  <div id="inner">
    first div with 10px padding
  </div>
  <div id="second">
    no padding
  </div>
</div>
Darek.K
  • 128
  • 2
  • 8
-1

You can just switch the divs

#first{
    
    border:1px solid #000;
}
#second{
    background-color:red;
    color:#fff;
  padding:0px 10px;
}
<div id="first">
  <div id="second">
    first div with 10px padding
  </div>
        no padding
    
</div>
Gacci
  • 1,388
  • 1
  • 11
  • 23