title: How can I get rounded corners without using overflow: hidden or applying border-radius to an unknown number of child elements?
body: <p>I have a container element that should have rounded corners. This element (<code>well</code>) should be a piece of reusable HTML and CSS that can contain a few common children with established styles such as primary or secondary regions (<code>well__primary</code>, <code>well__secondary</code>) as well as the possibility for a one-off style (<code>well__custom</code>). These second-level elements can be used in an arbitrary order and amount and each second-level element can contain arbitrary children elements.</p>
  
  <p>To start with, I was using <code>border-radius</code> on the outermost container coupled with <code>overflow: hidden</code> to prevent the squared corners of the inner elements from leaking out. This worked fine:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.body {
    background-color: #000;
    padding: 1em;
  }
  
  .well {
    margin: 1em 0 0 0;
    border: 1px solid #000;
    border-radius: 1em;
    overflow: hidden;
  }
  
  .well__primary {
    background-color: #fff;
  }
  
  .well__secondary {
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    background-color: #eee;
  }
  
  .well--first {
    border-top-left-radius: 1em;
    border-top-right-radius: 1em;
  }
  
  .well--last {
    border-bottom-left-radius: 1em;
    border-bottom-right-radius: 1em;
  }
  
  .item {
    padding: 1em;
  }
  
  .item--special {
    display: flex;
  }
  
  .item--special-left {
    flex: 1 1 auto;
    background-color: #88f;
  }
  
  .item--special-right {
    flex: 1 1 auto;
    background-color: #8f8;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="body"&gt;
    &lt;div class="well"&gt;
      &lt;div class="well__primary"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__secondary"&gt;
        &lt;div class="item"&gt;Secondary content&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="well"&gt;
      &lt;div class="well__secondary"&gt;
        &lt;div class="item"&gt;Secondary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__primary"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="well"&gt;
      &lt;div class="well__primary"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__custom"&gt;
        &lt;div class="item--special"&gt;
          &lt;div class="item item--special-left"&gt;Left&lt;/div&gt;
          &lt;div class="item item--special-right"&gt;Right&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>We then started using a third party component (<code>awkward</code>) that uses absolute positioning to show an editing panel. Parts of the panel were hidden because it overflowed the container.</p>
  
  <p>We are OK if the panel actually extends beyond the container, but having it be cut-off makes it impossible to use. When the whole panel is shown, its visual aspects are fine.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.body {
    background-color: #000;
    padding: 1em;
  }
  
  .well {
    margin: 1em 0 0 0;
    border: 1px solid #000;
    border-radius: 1em;
    overflow: hidden;
  }
  
  .well__primary {
    background-color: #fff;
  }
  
  .well__secondary {
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    background-color: #eee;
  }
  
  .item {
    padding: 1em;
  }
  
  .item--special {
    display: flex;
  }
  
  .item--special-left {
    flex: 1 1 auto;
    background-color: #88f;
  }
  
  .item--special-right {
    flex: 1 1 auto;
    background-color: #8f8;
  }
  
  .awkward {
    position: relative;
  }
  
  .awkward__text {
    position: absolute;
    background-color: #f88;
    width: 100px;
    height: 100px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="body"&gt;
    &lt;div class="well"&gt;
      &lt;div class="well__primary"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__secondary"&gt;
        &lt;div class="item"&gt;
          Secondary content
          &lt;div class="awkward"&gt;
            &lt;div class="awkward__text"&gt;Hello&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="well"&gt;
      &lt;div class="well__secondary"&gt;
        &lt;div class="item"&gt;Secondary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__primary"&gt;
        &lt;div class="item"&gt;
          Primary content
          &lt;div class="awkward"&gt;
            &lt;div class="awkward__text"&gt;Hello&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="well"&gt;
      &lt;div class="well__primary"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__custom"&gt;
        &lt;div class="item--special"&gt;
          &lt;div class="item item--special-left"&gt;
            Left
            &lt;div class="awkward"&gt;
              &lt;div class="awkward__text"&gt;Hello&lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="item item--special-right"&gt;Right&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I attempted to solve this by adding classes (<code>well--first</code>, <code>well--last</code>) to the first and last direct children of the well and applying the <code>border-radius</code> to those, but this doesn't work when the element contained within <em>those</em> elements is the one to specify the background color, as now that element is spilling outside of the border radius:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.body {
    background-color: #000;
    padding: 1em;
  }
  
  .well {
    margin: 1em 0 0 0;
    border: 1px solid #000;
  }
  
  .well__primary {
    background-color: #fff;
  }
  
  .well__secondary {
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    background-color: #eee;
  }
  
  .well--first {
    border-top-left-radius: 1em;
    border-top-right-radius: 1em;
  }
  
  .well--last {
    border-bottom-left-radius: 1em;
    border-bottom-right-radius: 1em;
  }
  
  .item {
    padding: 1em;
  }
  
  .item--special {
    display: flex;
  }
  
  .item--special-left {
    flex: 1 1 auto;
    background-color: #88f;
  }
  
  .item--special-right {
    flex: 1 1 auto;
    background-color: #8f8;
  }
  
  .awkward {
    position: relative;
  }
  
  .awkward__text {
    position: absolute;
    background-color: #f88;
    width: 100px;
    height: 100px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="body"&gt;
    &lt;div class="well"&gt;
      &lt;div class="well__primary well--first"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__secondary well--last"&gt;
        &lt;div class="item"&gt;
          Secondary content
          &lt;div class="awkward"&gt;
            &lt;div class="awkward__text"&gt;Hello&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="well"&gt;
      &lt;div class="well__secondary well--first"&gt;
        &lt;div class="item"&gt;Secondary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__primary well--last"&gt;
        &lt;div class="item"&gt;
          Primary content
          &lt;div class="awkward"&gt;
            &lt;div class="awkward__text"&gt;Hello&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="well"&gt;
      &lt;div class="well__primary well--first"&gt;
        &lt;div class="item"&gt;Primary content&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="well__custom well--last"&gt;
        &lt;div class="item--special"&gt;
          &lt;div class="item item--special-left"&gt;
            Left
            &lt;div class="awkward"&gt;
              &lt;div class="awkward__text"&gt;Hello&lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="item item--special-right"&gt;Right&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I don't want to have to modify the CSS of an arbitrary number of components (<code>item*</code>) just to give them rounded corners. I'd rather modify the CSS of the <code>well*</code> classes once.</p>
  
  <hr>
  
  <p>This is within a React application where I am striving to create a reusable <code>Well</code> component. The code would look like</p>
  
  <pre><code>&lt;Well&gt;
    &lt;WellPrimary&gt;
      Primary content
    &lt;/WellPrimary&gt;
    &lt;WellSecondary&gt;
      Secondary content
    &lt;/WellSecondary&gt;
    &lt;WellCustom&gt;
      Custom content
    &lt;/WellCustom&gt;
  &lt;/Well&gt;
  </code></pre>
  
  <p>Where <code>WellPrimary</code>, <code>WellSecondary</code> and <code>WellCustom</code> can all come in arbitrary order and amount, and the children of each of those three components is also arbitrary.</p>