0

I've got the following scenario:

div.container {
    min-width: 500px;
}

div.container div.left {

}

div.container div.right {

}

<div class="container">
    <div class="left">Fill-me</div>
    <div class="right">As-needed-only, align right</div>
</div>

I need the second div to be right-aligned in a row and be only as wide as needed, and left one to fill remaining space on the left. How can I achieve that?

ASCII-art:

[ Fill-me                        ][As-needed-only, align right]

XAML version of what I want to achieve:

<DockPanel>
    <Grid DockPanel.Dock="Right">
        <TextBlock>Right</TextBlock>
    </Grid>
    <Grid> <!-- Will fill rest of space -->
        <TextBlock>Left</TextBlock>
    </Grid>
</DockPanel>
Spook
  • 25,318
  • 18
  • 90
  • 167

2 Answers2

2

You can use display: table-cell to child div's and display: table to parent:

div > div {
  display: table-cell;
}
div > div:first-child {
  width: 100%;
  background: red;
}
div > div:last-child {
  background: green;
  white-space: nowrap;
}
<div style="width: 500px;display: table;">
  <div>Fill-me</div>
  <div>As-needed-only, align right</div>
</div>
Alex Char
  • 32,879
  • 9
  • 49
  • 70
0

I think it is best acheived easily by FLOATING, You have to float both left and right divs to right , then i prefer that you calculate width by % , like right 30% the other div (left) will take the remaining space i think .. give it a try

If you need the right div to take as much as space it needed , you have to do some calculations around like if you use php or other server side language, you can specify the number of words of the line and upon this determine the width accordingly.

mercury
  • 2,390
  • 3
  • 30
  • 39