Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

CSS styled DIV that stretches in width to fit contents

  • 29-07-2009 05:05PM
    #1
    Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭


    Hi,

    I have a div styled like so:
    div.Wrapper div.MainContents
    {
      margin-left:205px;
      margin-right:5px;
      display:block;
      min-height:500px;
      background-color:White;
    }
    

    Within this div, I have a Grid that is variable in width. At the moment, the Grid extends out past the container but I would like MainContents to stretch to fit this Grid.

    How can I achieve this?

    Thanks!
    John


Comments

  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Try this: display: table-cell seems to be the right property.

    I have two parent divs below both with a fixed width. But they seem to expand OK if a child div is wider. (FF3 / IE7 / Chrome)

    [html]
    <style type="text/css">
    .parentDiv
    {
    border: solid red 2px; display: table-cell; width: 390px;
    }
    </style>

    <div class="parentDiv">
    parent
    <div style="border: solid green 2px; width: 410px;">child with wider width</div>
    </div>
    <br/>
    <div class="parentDiv">
    parent1
    <div style="border: solid green 2px; width: 370px;">child without wider width</div>
    </div>
    [/html]


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    Hi Eoin,

    Many thanks for your reply. Unfortunately it doesn't work though :(

    Do you think this could be to do with the the Grid being a table with no specified width?

    Thanks,
    John


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Sorry! By grid, do you mean an ASP.net gridview that renders as a table or something? I just tried it with a table with loads of cells in one row (no width specified) and it expanded as expected. My CSS isn't great, I'm afraid.

    Could it be something to do with the parent of the parent? Can you separate the two elements out to a different page and test it?


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    eoin wrote: »
    Sorry! By grid, do you mean an ASP.net gridview that renders as a table or something? I just tried it with a table with loads of cells in one row (no width specified) and it expanded as expected. My CSS isn't great, I'm afraid.

    Could it be something to do with the parent of the parent? Can you separate the two elements out to a different page and test it?

    Yes you were right, it was the container of the container that needed display: table-cell set.

    Its working great in FF but not stretching in IE. I understand IE doesn't work with this, is there any hack that to workaround it?


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Weird, it seemed to work for me in IE7. Can you post the rendered HTML?


  • Advertisement
Advertisement