CSS expand element background to fit a container with padding











up vote
-2
down vote

favorite












I have a container with padding that includes child's elements.






body {
margin: 50px;
}

* {
box-sizing: border-box;
}

section {
width: 100px;
border: 1px solid;
padding: 10px;
}

div:hover {
background: red;
}

<section>
<div>One</div>
<div>Two</div>
</section>





When I hover on one of the children I want the background to fit the width of the container, how can I do it?



Note that I don't want to add the padding to the children.










share|improve this question


























    up vote
    -2
    down vote

    favorite












    I have a container with padding that includes child's elements.






    body {
    margin: 50px;
    }

    * {
    box-sizing: border-box;
    }

    section {
    width: 100px;
    border: 1px solid;
    padding: 10px;
    }

    div:hover {
    background: red;
    }

    <section>
    <div>One</div>
    <div>Two</div>
    </section>





    When I hover on one of the children I want the background to fit the width of the container, how can I do it?



    Note that I don't want to add the padding to the children.










    share|improve this question
























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I have a container with padding that includes child's elements.






      body {
      margin: 50px;
      }

      * {
      box-sizing: border-box;
      }

      section {
      width: 100px;
      border: 1px solid;
      padding: 10px;
      }

      div:hover {
      background: red;
      }

      <section>
      <div>One</div>
      <div>Two</div>
      </section>





      When I hover on one of the children I want the background to fit the width of the container, how can I do it?



      Note that I don't want to add the padding to the children.










      share|improve this question













      I have a container with padding that includes child's elements.






      body {
      margin: 50px;
      }

      * {
      box-sizing: border-box;
      }

      section {
      width: 100px;
      border: 1px solid;
      padding: 10px;
      }

      div:hover {
      background: red;
      }

      <section>
      <div>One</div>
      <div>Two</div>
      </section>





      When I hover on one of the children I want the background to fit the width of the container, how can I do it?



      Note that I don't want to add the padding to the children.






      body {
      margin: 50px;
      }

      * {
      box-sizing: border-box;
      }

      section {
      width: 100px;
      border: 1px solid;
      padding: 10px;
      }

      div:hover {
      background: red;
      }

      <section>
      <div>One</div>
      <div>Two</div>
      </section>





      body {
      margin: 50px;
      }

      * {
      box-sizing: border-box;
      }

      section {
      width: 100px;
      border: 1px solid;
      padding: 10px;
      }

      div:hover {
      background: red;
      }

      <section>
      <div>One</div>
      <div>Two</div>
      </section>






      html css






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 7:55









      undefined

      7961419




      7961419
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Try following way, I applied pseudo element:






          <!DOCTYPE html>
          <html>
          <head>
          <title></title>
          <style type="text/css">
          body {
          margin: 50px;
          }

          * {
          box-sizing: border-box;
          }

          section {
          width: 100px;
          border: 1px solid;
          padding: 10px;
          }

          div:hover {
          background: red;
          position:relative;
          }
          div::after {
          display:none;
          }
          div:hover::after {
          display:block;
          position:absolute;
          z-index:-1;
          content:"";
          top:0;
          left:-10px;
          right:-10px;
          bottom:0;
          background-color:red;
          }
          </style>

          </head>
          <body>
          <section>
          <div>One</div>
          <div>Two</div>
          </section>
          </body>
          </html>








          share|improve this answer






























            up vote
            1
            down vote













            I don't get why you don't want to add padding to the children.. Because if you just lose the padding on the section and put it on the div, all is solved!






            body {
            margin: 50px;
            }

            * {
            box-sizing: border-box;
            }

            section {
            width: 100px;
            border: 1px solid;
            }

            section div {
            padding: 10px
            }
            div:hover {
            background: red;
            }

            <section>
            <div>One</div>
            <div>Two</div>
            </section>








            share|improve this answer





















            • because in real life it's more complicated than that.
              – undefined
              Nov 7 at 8:14


















            up vote
            0
            down vote













            If you don't want to use js and you can't remove the padding from the section, you can use :before and :after.






            section {
            width: 100px;
            border: 1px solid;
            padding: 10px;
            position: relative;
            }

            div > span {
            position: relative;
            z-index: 2;
            }

            div:before {
            content: '';
            position: absolute;
            top: 0;
            right: 0;
            left: 0;
            bottom: 0;
            background: red;
            opacity: 0;
            }

            div:hover:before {
            opacity: 1;
            }

            <section>
            <div>
            <span>one</span>
            </div>
            <div>
            <span>two</span>
            </div>
            </section>








            share|improve this answer




























              up vote
              0
              down vote













              If you define section padding just for top and bottom and add padding to divs for right and left you can achieve what you want:






              body {
              margin: 50px;
              }

              * {
              box-sizing: border-box;
              }

              section {
              width: 100px;
              border: 1px solid;
              padding: 10px 0px;
              }

              section div {
              padding: 0px 10px;
              }

              div:hover {
              background: red;
              }

              <section>
              <div>One</div>
              <div>Two</div>
              </section>








              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185403%2fcss-expand-element-background-to-fit-a-container-with-padding%23new-answer', 'question_page');
                }
                );

                Post as a guest
































                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                Try following way, I applied pseudo element:






                <!DOCTYPE html>
                <html>
                <head>
                <title></title>
                <style type="text/css">
                body {
                margin: 50px;
                }

                * {
                box-sizing: border-box;
                }

                section {
                width: 100px;
                border: 1px solid;
                padding: 10px;
                }

                div:hover {
                background: red;
                position:relative;
                }
                div::after {
                display:none;
                }
                div:hover::after {
                display:block;
                position:absolute;
                z-index:-1;
                content:"";
                top:0;
                left:-10px;
                right:-10px;
                bottom:0;
                background-color:red;
                }
                </style>

                </head>
                <body>
                <section>
                <div>One</div>
                <div>Two</div>
                </section>
                </body>
                </html>








                share|improve this answer



























                  up vote
                  2
                  down vote



                  accepted










                  Try following way, I applied pseudo element:






                  <!DOCTYPE html>
                  <html>
                  <head>
                  <title></title>
                  <style type="text/css">
                  body {
                  margin: 50px;
                  }

                  * {
                  box-sizing: border-box;
                  }

                  section {
                  width: 100px;
                  border: 1px solid;
                  padding: 10px;
                  }

                  div:hover {
                  background: red;
                  position:relative;
                  }
                  div::after {
                  display:none;
                  }
                  div:hover::after {
                  display:block;
                  position:absolute;
                  z-index:-1;
                  content:"";
                  top:0;
                  left:-10px;
                  right:-10px;
                  bottom:0;
                  background-color:red;
                  }
                  </style>

                  </head>
                  <body>
                  <section>
                  <div>One</div>
                  <div>Two</div>
                  </section>
                  </body>
                  </html>








                  share|improve this answer

























                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    Try following way, I applied pseudo element:






                    <!DOCTYPE html>
                    <html>
                    <head>
                    <title></title>
                    <style type="text/css">
                    body {
                    margin: 50px;
                    }

                    * {
                    box-sizing: border-box;
                    }

                    section {
                    width: 100px;
                    border: 1px solid;
                    padding: 10px;
                    }

                    div:hover {
                    background: red;
                    position:relative;
                    }
                    div::after {
                    display:none;
                    }
                    div:hover::after {
                    display:block;
                    position:absolute;
                    z-index:-1;
                    content:"";
                    top:0;
                    left:-10px;
                    right:-10px;
                    bottom:0;
                    background-color:red;
                    }
                    </style>

                    </head>
                    <body>
                    <section>
                    <div>One</div>
                    <div>Two</div>
                    </section>
                    </body>
                    </html>








                    share|improve this answer














                    Try following way, I applied pseudo element:






                    <!DOCTYPE html>
                    <html>
                    <head>
                    <title></title>
                    <style type="text/css">
                    body {
                    margin: 50px;
                    }

                    * {
                    box-sizing: border-box;
                    }

                    section {
                    width: 100px;
                    border: 1px solid;
                    padding: 10px;
                    }

                    div:hover {
                    background: red;
                    position:relative;
                    }
                    div::after {
                    display:none;
                    }
                    div:hover::after {
                    display:block;
                    position:absolute;
                    z-index:-1;
                    content:"";
                    top:0;
                    left:-10px;
                    right:-10px;
                    bottom:0;
                    background-color:red;
                    }
                    </style>

                    </head>
                    <body>
                    <section>
                    <div>One</div>
                    <div>Two</div>
                    </section>
                    </body>
                    </html>








                    <!DOCTYPE html>
                    <html>
                    <head>
                    <title></title>
                    <style type="text/css">
                    body {
                    margin: 50px;
                    }

                    * {
                    box-sizing: border-box;
                    }

                    section {
                    width: 100px;
                    border: 1px solid;
                    padding: 10px;
                    }

                    div:hover {
                    background: red;
                    position:relative;
                    }
                    div::after {
                    display:none;
                    }
                    div:hover::after {
                    display:block;
                    position:absolute;
                    z-index:-1;
                    content:"";
                    top:0;
                    left:-10px;
                    right:-10px;
                    bottom:0;
                    background-color:red;
                    }
                    </style>

                    </head>
                    <body>
                    <section>
                    <div>One</div>
                    <div>Two</div>
                    </section>
                    </body>
                    </html>





                    <!DOCTYPE html>
                    <html>
                    <head>
                    <title></title>
                    <style type="text/css">
                    body {
                    margin: 50px;
                    }

                    * {
                    box-sizing: border-box;
                    }

                    section {
                    width: 100px;
                    border: 1px solid;
                    padding: 10px;
                    }

                    div:hover {
                    background: red;
                    position:relative;
                    }
                    div::after {
                    display:none;
                    }
                    div:hover::after {
                    display:block;
                    position:absolute;
                    z-index:-1;
                    content:"";
                    top:0;
                    left:-10px;
                    right:-10px;
                    bottom:0;
                    background-color:red;
                    }
                    </style>

                    </head>
                    <body>
                    <section>
                    <div>One</div>
                    <div>Two</div>
                    </section>
                    </body>
                    </html>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 7 at 8:06

























                    answered Nov 7 at 8:00









                    Hanif

                    2,4611511




                    2,4611511
























                        up vote
                        1
                        down vote













                        I don't get why you don't want to add padding to the children.. Because if you just lose the padding on the section and put it on the div, all is solved!






                        body {
                        margin: 50px;
                        }

                        * {
                        box-sizing: border-box;
                        }

                        section {
                        width: 100px;
                        border: 1px solid;
                        }

                        section div {
                        padding: 10px
                        }
                        div:hover {
                        background: red;
                        }

                        <section>
                        <div>One</div>
                        <div>Two</div>
                        </section>








                        share|improve this answer





















                        • because in real life it's more complicated than that.
                          – undefined
                          Nov 7 at 8:14















                        up vote
                        1
                        down vote













                        I don't get why you don't want to add padding to the children.. Because if you just lose the padding on the section and put it on the div, all is solved!






                        body {
                        margin: 50px;
                        }

                        * {
                        box-sizing: border-box;
                        }

                        section {
                        width: 100px;
                        border: 1px solid;
                        }

                        section div {
                        padding: 10px
                        }
                        div:hover {
                        background: red;
                        }

                        <section>
                        <div>One</div>
                        <div>Two</div>
                        </section>








                        share|improve this answer





















                        • because in real life it's more complicated than that.
                          – undefined
                          Nov 7 at 8:14













                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        I don't get why you don't want to add padding to the children.. Because if you just lose the padding on the section and put it on the div, all is solved!






                        body {
                        margin: 50px;
                        }

                        * {
                        box-sizing: border-box;
                        }

                        section {
                        width: 100px;
                        border: 1px solid;
                        }

                        section div {
                        padding: 10px
                        }
                        div:hover {
                        background: red;
                        }

                        <section>
                        <div>One</div>
                        <div>Two</div>
                        </section>








                        share|improve this answer












                        I don't get why you don't want to add padding to the children.. Because if you just lose the padding on the section and put it on the div, all is solved!






                        body {
                        margin: 50px;
                        }

                        * {
                        box-sizing: border-box;
                        }

                        section {
                        width: 100px;
                        border: 1px solid;
                        }

                        section div {
                        padding: 10px
                        }
                        div:hover {
                        background: red;
                        }

                        <section>
                        <div>One</div>
                        <div>Two</div>
                        </section>








                        body {
                        margin: 50px;
                        }

                        * {
                        box-sizing: border-box;
                        }

                        section {
                        width: 100px;
                        border: 1px solid;
                        }

                        section div {
                        padding: 10px
                        }
                        div:hover {
                        background: red;
                        }

                        <section>
                        <div>One</div>
                        <div>Two</div>
                        </section>





                        body {
                        margin: 50px;
                        }

                        * {
                        box-sizing: border-box;
                        }

                        section {
                        width: 100px;
                        border: 1px solid;
                        }

                        section div {
                        padding: 10px
                        }
                        div:hover {
                        background: red;
                        }

                        <section>
                        <div>One</div>
                        <div>Two</div>
                        </section>






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 7 at 8:04









                        Wimanicesir

                        3349




                        3349












                        • because in real life it's more complicated than that.
                          – undefined
                          Nov 7 at 8:14


















                        • because in real life it's more complicated than that.
                          – undefined
                          Nov 7 at 8:14
















                        because in real life it's more complicated than that.
                        – undefined
                        Nov 7 at 8:14




                        because in real life it's more complicated than that.
                        – undefined
                        Nov 7 at 8:14










                        up vote
                        0
                        down vote













                        If you don't want to use js and you can't remove the padding from the section, you can use :before and :after.






                        section {
                        width: 100px;
                        border: 1px solid;
                        padding: 10px;
                        position: relative;
                        }

                        div > span {
                        position: relative;
                        z-index: 2;
                        }

                        div:before {
                        content: '';
                        position: absolute;
                        top: 0;
                        right: 0;
                        left: 0;
                        bottom: 0;
                        background: red;
                        opacity: 0;
                        }

                        div:hover:before {
                        opacity: 1;
                        }

                        <section>
                        <div>
                        <span>one</span>
                        </div>
                        <div>
                        <span>two</span>
                        </div>
                        </section>








                        share|improve this answer

























                          up vote
                          0
                          down vote













                          If you don't want to use js and you can't remove the padding from the section, you can use :before and :after.






                          section {
                          width: 100px;
                          border: 1px solid;
                          padding: 10px;
                          position: relative;
                          }

                          div > span {
                          position: relative;
                          z-index: 2;
                          }

                          div:before {
                          content: '';
                          position: absolute;
                          top: 0;
                          right: 0;
                          left: 0;
                          bottom: 0;
                          background: red;
                          opacity: 0;
                          }

                          div:hover:before {
                          opacity: 1;
                          }

                          <section>
                          <div>
                          <span>one</span>
                          </div>
                          <div>
                          <span>two</span>
                          </div>
                          </section>








                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            If you don't want to use js and you can't remove the padding from the section, you can use :before and :after.






                            section {
                            width: 100px;
                            border: 1px solid;
                            padding: 10px;
                            position: relative;
                            }

                            div > span {
                            position: relative;
                            z-index: 2;
                            }

                            div:before {
                            content: '';
                            position: absolute;
                            top: 0;
                            right: 0;
                            left: 0;
                            bottom: 0;
                            background: red;
                            opacity: 0;
                            }

                            div:hover:before {
                            opacity: 1;
                            }

                            <section>
                            <div>
                            <span>one</span>
                            </div>
                            <div>
                            <span>two</span>
                            </div>
                            </section>








                            share|improve this answer












                            If you don't want to use js and you can't remove the padding from the section, you can use :before and :after.






                            section {
                            width: 100px;
                            border: 1px solid;
                            padding: 10px;
                            position: relative;
                            }

                            div > span {
                            position: relative;
                            z-index: 2;
                            }

                            div:before {
                            content: '';
                            position: absolute;
                            top: 0;
                            right: 0;
                            left: 0;
                            bottom: 0;
                            background: red;
                            opacity: 0;
                            }

                            div:hover:before {
                            opacity: 1;
                            }

                            <section>
                            <div>
                            <span>one</span>
                            </div>
                            <div>
                            <span>two</span>
                            </div>
                            </section>








                            section {
                            width: 100px;
                            border: 1px solid;
                            padding: 10px;
                            position: relative;
                            }

                            div > span {
                            position: relative;
                            z-index: 2;
                            }

                            div:before {
                            content: '';
                            position: absolute;
                            top: 0;
                            right: 0;
                            left: 0;
                            bottom: 0;
                            background: red;
                            opacity: 0;
                            }

                            div:hover:before {
                            opacity: 1;
                            }

                            <section>
                            <div>
                            <span>one</span>
                            </div>
                            <div>
                            <span>two</span>
                            </div>
                            </section>





                            section {
                            width: 100px;
                            border: 1px solid;
                            padding: 10px;
                            position: relative;
                            }

                            div > span {
                            position: relative;
                            z-index: 2;
                            }

                            div:before {
                            content: '';
                            position: absolute;
                            top: 0;
                            right: 0;
                            left: 0;
                            bottom: 0;
                            background: red;
                            opacity: 0;
                            }

                            div:hover:before {
                            opacity: 1;
                            }

                            <section>
                            <div>
                            <span>one</span>
                            </div>
                            <div>
                            <span>two</span>
                            </div>
                            </section>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 7 at 8:05









                            vmoh_ir

                            1,1341223




                            1,1341223






















                                up vote
                                0
                                down vote













                                If you define section padding just for top and bottom and add padding to divs for right and left you can achieve what you want:






                                body {
                                margin: 50px;
                                }

                                * {
                                box-sizing: border-box;
                                }

                                section {
                                width: 100px;
                                border: 1px solid;
                                padding: 10px 0px;
                                }

                                section div {
                                padding: 0px 10px;
                                }

                                div:hover {
                                background: red;
                                }

                                <section>
                                <div>One</div>
                                <div>Two</div>
                                </section>








                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  If you define section padding just for top and bottom and add padding to divs for right and left you can achieve what you want:






                                  body {
                                  margin: 50px;
                                  }

                                  * {
                                  box-sizing: border-box;
                                  }

                                  section {
                                  width: 100px;
                                  border: 1px solid;
                                  padding: 10px 0px;
                                  }

                                  section div {
                                  padding: 0px 10px;
                                  }

                                  div:hover {
                                  background: red;
                                  }

                                  <section>
                                  <div>One</div>
                                  <div>Two</div>
                                  </section>








                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    If you define section padding just for top and bottom and add padding to divs for right and left you can achieve what you want:






                                    body {
                                    margin: 50px;
                                    }

                                    * {
                                    box-sizing: border-box;
                                    }

                                    section {
                                    width: 100px;
                                    border: 1px solid;
                                    padding: 10px 0px;
                                    }

                                    section div {
                                    padding: 0px 10px;
                                    }

                                    div:hover {
                                    background: red;
                                    }

                                    <section>
                                    <div>One</div>
                                    <div>Two</div>
                                    </section>








                                    share|improve this answer












                                    If you define section padding just for top and bottom and add padding to divs for right and left you can achieve what you want:






                                    body {
                                    margin: 50px;
                                    }

                                    * {
                                    box-sizing: border-box;
                                    }

                                    section {
                                    width: 100px;
                                    border: 1px solid;
                                    padding: 10px 0px;
                                    }

                                    section div {
                                    padding: 0px 10px;
                                    }

                                    div:hover {
                                    background: red;
                                    }

                                    <section>
                                    <div>One</div>
                                    <div>Two</div>
                                    </section>








                                    body {
                                    margin: 50px;
                                    }

                                    * {
                                    box-sizing: border-box;
                                    }

                                    section {
                                    width: 100px;
                                    border: 1px solid;
                                    padding: 10px 0px;
                                    }

                                    section div {
                                    padding: 0px 10px;
                                    }

                                    div:hover {
                                    background: red;
                                    }

                                    <section>
                                    <div>One</div>
                                    <div>Two</div>
                                    </section>





                                    body {
                                    margin: 50px;
                                    }

                                    * {
                                    box-sizing: border-box;
                                    }

                                    section {
                                    width: 100px;
                                    border: 1px solid;
                                    padding: 10px 0px;
                                    }

                                    section div {
                                    padding: 0px 10px;
                                    }

                                    div:hover {
                                    background: red;
                                    }

                                    <section>
                                    <div>One</div>
                                    <div>Two</div>
                                    </section>






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 7 at 8:08









                                    Ryan.Hunt

                                    2,50811020




                                    2,50811020






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185403%2fcss-expand-element-background-to-fit-a-container-with-padding%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest




















































































                                        這個網誌中的熱門文章

                                        Tangent Lines Diagram Along Smooth Curve

                                        Yusuf al-Mu'taman ibn Hud

                                        Zucchini