5

I need help, I have a 4 div elements, three of them have fixed width, one of them needs to be with auto width. Second element needs to have variable width.

For example:

<div id="wrapper">
  <div id="first">
  </div>
  <div id="second">
  </div>
  <div id="third">
  </div>
  <div id="fourth">
  </div>
</div>

Css:

#first,#second,#third,#fourth{
  float:left;
}
#second{
  width:auto;
  overflow:hidden;
}
#first,#third,#fourth{
  width: 200px;
}

Thanks for help

3
  • 2
    Please post your CSS too Commented Apr 30, 2013 at 8:39
  • What have you tried? This looks pretty simple to achieve with css, set widths for the 3 id's you want and not for the second element (or set its width to 100%) Commented Apr 30, 2013 at 8:41
  • Yeah, we need to see the CSS. My bet is that the wrapper has a set width, and the second inner div needs to stretch outside that. Is that right? Commented Apr 30, 2013 at 8:43

2 Answers 2

8

This can be achieved using display: table-cell jsfiddle

CSS

#wrapper .item{ 
    display: table-cell; 
    width: 150px; 
    min-width: 150px; 
    border: 1px solid #777; 
    background: #eee; 
    text-align: center;
}

#wrapper #second{
    width: 100%
}

Markup

  <div id="wrapper">
     <div id="first" class="item">First
     </div>
     <div id="second" class="item">Second
     </div>
     <div id="third" class="item">Third
     </div>
     <div id="fourth" class="item">Fourth
     </div>
  </div>

Update

Float version

CSS

     #wrapper div{background:#eee; border: 1px solid #777; min-width: 200px;}
     #first{
        float: left;
     }
     #wrapper #second{
        width: auto;
        background: #ffc;
        border: 1px solid #f00;
        min-width: 100px;
        overflow: hidden;
     }
     #first, #third, #fourth{
        width: 200px;
     }

     #third, #fourth{float: right;}

Markup, Move #second to end

  <div id="wrapper">
     <div id="first">First</div>
     <div id="third">Third</div>
     <div id="fourth">Fourth</div>
     <div id="second">Second</div>
  </div>
Sign up to request clarification or add additional context in comments.

1 Comment

I tried it, it works fine. But I have a problem when I put display:table-cell, actually I have a problem in chrome. I need solution with display block. Thanks for help
1

i think you might be looking for this one:

This is for your reference if you are having such a thing then you can do the trick with this, i exactly don't know how your css looks like but this is basic idea.

Demo Here

CSS

#wrapper
{
    width:960px;
}
#first
{
    float:left;
    width:240px;
}
#second
{
    width:240px;
    float:left;
}
#third
{
    float:left;
    width:240px
}

Here your last div width will be set automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.