Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an element as wide as the grandparent

Tags:

html

css

I have the following markup where #content is 80% wide and contains .slide elements. I want the slides to be as wide as their grandparent (i.e. body in this example). This is the markup I have and it cannot be changed:

body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
#content {
  margin: auto;
  width: 80%;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
}
<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <blockquote>
    <p>Phasellus euismod dolor imperdiet!</p>
  </blockquote>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
  <ul>
    <li>Quisque ornare mi in pharetra porttitor.</li>
    <li>Nulla ultrices quam nec vehicula porta.</li>
  </ul>
</div>

I have tried:

  • relative-absolute positioning which requires height of slides to be fixed (the slides contain variable length text)
  • setting 80% width on paragraphs instead of content but this is not elegant (content contains elements that cannot have 80% width or 10% left margin)
like image 987
Salman A Avatar asked Apr 30 '15 09:04

Salman A


1 Answers

If your surrounding content can demand a different combination of positioning properties on their own, you could always go with the following.

body {
  margin: 0;
  font: medium monospace;
  background: lightgray;
}
#content {
  margin: auto;
  width: 80%;
  background: white;
}
#content:before,
#content:after {
  content: "";
  display: table;
}
.slide {
  height: 6em;
  background: indianred;
  width: 125%; /*100*(100/80)*/
  margin-left: 50%;
  transform: translateX(-50%);
}
<div id="content">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <div class="slide">Donec mauris tellus</div>
  <p>Pellentesque sit amet venenatis diam, at interdum tortor.</p>
</div>
like image 97
Etheryte Avatar answered Oct 04 '22 20:10

Etheryte