Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-04-07
15
Medium Priority
?
152 Views
Last Modified: 2014-05-07
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
Comment
Question by:coolispaul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 12

Expert Comment

by:Tej Pratap Shukla ~Dexter
ID: 39982741
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
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39982750
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
 

Author Comment

by:coolispaul
ID: 39982769
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39982789
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
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39982800
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
 
LVL 12

Expert Comment

by:Tej Pratap Shukla ~Dexter
ID: 39982801
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
 
LVL 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 39982816
>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
 

Author Comment

by:coolispaul
ID: 39982821
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
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39982837
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
 

Author Comment

by:coolispaul
ID: 39982840
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
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39982847
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
 

Author Comment

by:coolispaul
ID: 39982865
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
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39982876
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
 
LVL 12

Expert Comment

by:Tej Pratap Shukla ~Dexter
ID: 39982878
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
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39982879
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question