title: Avoid doubling up margins and padding in CSS
body: <p>I have a simple example of something that seems reasonable to me, but that I can't figure out how to model in HTML/CSS.</p>
  
  <p>I'd like to create a fixed-width container in <code>px</code> units with padding in <code>px</code> units.  Inside, I'd like to display three paragraphs, separated by vertical margins in <code>em</code> units.</p>
  
  <p>The width and padding should be in pixels, so I can calibrate to screen size.  The margins should be in text units, so the spacing adjusts to changes in font size.  This all makes sense so far.</p>
  
  <p><a href="https://i.stack.imgur.com/DTHnk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DTHnk.png" alt="Example of desired result"></a></p>
  
  <p>The problem is, the most obvious HTML/CSS implementation gives me leading and trailing margins I don't want.</p>
  
  <p><a href="https://i.stack.imgur.com/1N8yx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1N8yx.png" alt="Example with undesirable leading and trailing margins"></a></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;style&gt;
          div {
              width: 300px;
              padding: 10px;
              border: solid black 1px;
          }
  
          p {
              margin-top: 1em;
              margin-bottom: 1em;
              border: dotted black 1px;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          &lt;p&gt;Paragraph 1&lt;/p&gt;
          &lt;p&gt;Paragraph 2&lt;/p&gt;
          &lt;p&gt;Paragraph 3&lt;/p&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Is there a good, general way to achieve this without tampering with the margins on the first and last <code>p</code> elements?  Obviously, I can suppress those specific margins, but that's not a very scalable solution.</p>