Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:56:53+00:00 2026-05-26T10:56:53+00:00

I have a <div> styled like a box: .box { border:2px solid #ccc; background:#eee;

  • 0

I have a <div> styled like a box:

.box {
    border:2px solid #ccc;
    background:#eee;   
    padding:1em;
    margin:1em;
}

In this box, there could be any number of any type of child elements in any order:

<div class="box">
    <h3>Howzit goin</h3>
    <p>Here comes a list:</p>
    <ul>
        <li>I don't know what</li>
        <li>this box will contain</li>
        <li>but it could be anything</li>
    </ul>
</div>

Most or all of the child elements inherit bottom margin of various lengths from the base typography stylesheet:

/* Example global typography */
p {margin:0 0 1.5em;}
ul {margin:0 0 2.5em;}

Which produces output like this:

enter image description here

…but we want to normalize the “padding” of the box so that it appears equal on all sides:

enter image description here

  • .box :last-child would be too easy, this has to work in at least IE8 as well as
    modern browsers (but it could be used in conjunction with an IE-only method).
  • I don’t want to use extra markup or javascript.

Is there any other CSS trick I can use to get the output I want?

Fiddle: http://jsfiddle.net/yuXcH/1/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:56:53+00:00Added an answer on May 26, 2026 at 10:56 am

    As you can read in this question, even if it’s about 2 years old, there’s no “easy” way to do this in IE8 (the other thread is just about IE6/7, but things haven’t changed – IE8 doesn’t support :last-child either).

    The best way, in my opinion, is to manually add a class last-child to your last child so you can do:

    .box .last-child{ margin-bottom: 0; }
    

    The alternative is using javascript, which is easier if you have a lot of boxes. With jQuery, it would just look like this:

    $(function(){
      $(".box :last-child").css("margin-bottom","0");
    })
    

    The only “pure CSS” solution I can think of is changing all of your padding/margins to always result in a box with same padding on all sides like Lollero suggested, but this will, compared to your previous solution, result in different margins between the elements inside of the box.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element like this: <div id=content style=border: 2px solid black> </div> And
Given this HTML snippet: <div id=box style=overflow:auto; width:200px; height:200px; border:1px solid black;> 1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br> 11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>
I have a Div layer like this ... <style type=text/css> <!-- #newImg { position:absolute;
I have a div tag styled through CSS. I set the padding to 10px
I have this CSS style that does a rounded corners for div border, but
I have repetitious markup for elements, like: <div class=box> <div class=top></div> <div class=content> content
I really like a div box that is styled a particular way on another
I have a draggable div element with a hover style. This works fine, but
I have a div box and a link on my page. I have written
Currently, I have a code like this <!DOCTYPE HTML PUBLIC -// W3C//DTD HTML 4.01

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.