When a floating element takes up some space, the "normal" text makes room to accommodate the floating element. But the border around the normal text does not and instead intersects with the floating element.
In the example below, how can I make the first red box end before the blue box starts with some gap, and not overlap? And let the second red box, which does not need to share space with the floating element, still take up 100%?
div#toc {
float: right;
width: 300px;
padding: 20px;
border: 1px solid blue;
}
div.warn {
padding: 20px;
border: 1px solid red;
}
<div id="toc">
<h2>Table of contents</h2>
<ol>
<li>Chapter 1</li>
<li>Chapter 2</li>
<li>Chapter 3</li>
</ol>
</div>
<div id="main">
<h1>Chapter 1</h1>
<p>In the beginning ...</p>
<div class="warn">
Fairy tale, watch out!
</div>
<p>... and were married happily ever after.</p>
<div class="warn">
Don't retell!
</div>
</div>
2
Answers
No can do. Use a different design.
Block-level boxes that establish an independent formatting context shrink to avoid floats. The most direct way to make a box establish a block formatting context (the most suitable type of independent formatting context in this case) is to give it
display: flow-root
.Use
margin
on the float to create the gap.