• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

2 columns - both fluid and floated - 1 with min width

Hi

Im trying to float 2 divs alongside each other but 1 div needs amin-width of 300px

When i try this the right div drops. Why is this?

See my fiddle - http://jsfiddle.net/fj3C6/

Thanks
0
coolispaul
Asked:
coolispaul
  • 6
  • 4
  • 3
  • +1
1 Solution
 
Tej Pratap Shukla ~DexterServer AdministratorCommented:
Hey there,
I saw your code , few things are wrong and need to be corrected
1. If you are making a container where you would float two classes then make sure that container is an id not a class.
2.You mentioned .left, .right { float:left } this is but obvious any changes that would overflow the container would drop the right div to float left.
3. Here's the code snippet which might help you


<style>
#container {
       width:100%;
      background:#000;}
.left {
    width:300px;
    background:#ff0000;
      float:left;
}
.right{
    width:70%;
    background:#999;
}
</style>
</head>
<div id="container">
    <div class="left">fer</div>
    <div class="right">ferwf</div>
    </div>

Feel free to ask for further queries
Thanks
~Dex
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Your code seems to be fine.  Your fiddle was messed up because you had the html in the s. I moved it.  http://jsfiddle.net/fj3C6/5/
0
 
coolispaulAuthor Commented:
Hi Dexter

1) why you need an ID?
3)

Why is .right 70%?
your example code still causes the right div to drop?

Cheers
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Kyle HamiltonData ScientistCommented:
ref Scott's fiddle.

if you give your left div a min-width of 300px, and your overall container is too small, let's say for example the calculated width of the container is 400px, then your right div which is set to be 53%, will no longer fit in the container since the left div is now taking up more than 47% of the container.  

for what you are trying to achieve, you need a different approach. if you make the right container width: auto, then it wont drop, but your background color may not necessarily reach the right edge of the container. if you need the background color to "fill" the right div, then you need a completely different approach.
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The container is 100%

If you are having issues, it might have to do with other aspects of your code.  Can you give us a link to your test page?
0
 
Tej Pratap Shukla ~DexterServer AdministratorCommented:
Hey coolispaul
Well as you mentioned that your left div should be atleast 300px , to avoid confusion you should stick to one of the measurements .You need the results in pixels or percentage?
And it is coming normal and there might be another reason that your right div is dropping is that your browser zoom must be lesser or greater than normal.To optimise the divs in such a way that it remains static no matter how much you zoom in zoom out, you should prefer to percentage measurement.
I don't have much knowledge but the id tag is preferred whenever you use an overall container or something that would contain objects .
Another solution as stated by kyle is absolutely correct
<style>
#container {
       width:100%;
      background:#000;}
.left {
    width:300px;
    background:#ff0000;
      float:left;
}
.right{
    width:auto;
    background:#999;
}
</style>
</head>
<div id="container">
    <div class="left">fer</div>
    <div class="right">ferwf</div>
    </div>
<body>
You can use this method too


Thanks
~Dex
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
>the id tag is preferred whenever you use an overall container or something that would contain objects

ID tags can only occur once per page where classes can be repeated.  If the same element has both a class and id and the css for each is different, the id will win.  The code below the color of "stuff" will be blue.
.red{color:red;}
#blue{color:blue}
<div id="blue" class="red">stuff</div>

Open in new window


Typically, I think it is best to keep your styles to classes.  Where you start seeing the importance of ID's and Classes is in targeting objects via javascript.

Otherwise, there is no real benefit to style your container by it's ID or CLASS.
0
 
coolispaulAuthor Commented:
ok thanks - that seems to work

why does the right div not get floated left aswell?
i though it had to be floated left to sit adjacent to the left column?

Thanks
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Both are floated left with this bit of css below.  The comma between the 2 classes means apply the same style to both.
.left, .right { float:left }

Open in new window

0
 
coolispaulAuthor Commented:
yep i know that but in Dexter css thats not the case (but actually works how i intended) :

#container {
       width:100%;
      background:#000;}
.left {
    width:300px;
    background:#ff0000;
      float:left;
}
.right{
    width:auto;
    background:#999;
}
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
If you only have 2 columns, that will works.  But if you added a third, they both need to be floated.  The right most container does not need the float.
0
 
coolispaulAuthor Commented:
why is that though - out of interest?

div is a block element isnt it? i thought only inline elements would flow left like that
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Let's experiment.

Both work http://jsbin.com/mefecofu/1/edit?html,output

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
    width:100%;
    background:#000
    margin-bottom:10px;
    height:40px;
    border-style:solid;
    border-width:medium;
}
.left, .right { float:left;height:40px; }
.left {
    width:47%;
    background:#ff0000
}
.right{
    width:53%;
    background:#999
}
    </style>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="container">
    <div class="left">fer</div>
    <div class="right">ferwf</div>
</div>
 <div class="container">
    <span class="left">fer</span>
    <span class="right">ferwf</span>
</div>
  
</body>
</html>

Open in new window

0
 
Tej Pratap Shukla ~DexterServer AdministratorCommented:
The right most element would first stick to the leftover container space after the left element acquired it's place.At such occasions we use <div class="clear"> to separate out the div .
0
 
Kyle HamiltonData ScientistCommented:
yes. the container is set to 100%. but if the page is only 400 px wide, then your container's calculated value is also 400px. Or if your container's container is 350px wide, then your container's calculated value is also 350px wide. It is 100% of its container.

what you stated as working, is not due to any of Dexter's CSS. it's due to setting the width to auto.

in fact, you can just remove the width completely, since width:auto is the default behavior.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now