Click here to Skip to main content
15,909,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to achieve the following setup:

| div fit to content |margin 48px| ----- s t r e t c h e d ----- |

So the left div needs margin: 0 48px 0 0, and needs to fit its content, and the right div needs to stretch horizontally to fit the remaining width of the parent.

I've tried setting the parent to
display: table;
width: 100%;

And using different combinations of widths, display: table-cell etc for the columns but nothing seems to work.

One thing to note, the left and right div must both fill the height of the parent.

| div fit to content |           | ----- s t r e t c h e d ----- |
|                    |margin 48px|                               |
| -- more content -- |           | -- stretched down to here  -- |


It would be great to get this simple layout working.

What I have tried:

Everything mentioned in question.
---------------------------------
Posted
Updated 20-Apr-18 3:43am
v4

1 solution

If you can drop support for IE10 and earlier, Flexbox would be a simple solution:
HTML
<div class="wrapper">
    <div>
        Div fit to content<br />
        More content<br />
        And some more
    </div>
    <div>
        Stretched
    </div>
</div>
CSS
.wrapper
{
    display: flex;
}
.wrapper > div
{
    flex-grow: 1;
}
.wrapper > div:first-child
{
    margin-right: 48px;
    flex-grow: 0;
}
Demo[^]
Browser support[^]
A Complete Guide to Flexbox | CSS-Tricks[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900