Solved

Floats not working the way I wanted

Posted on 2015-01-21
2
67 Views
Last Modified: 2015-02-06
Im trying to get my CSS to float correctly. What Im trying to do is float my spacers to the remaining space after the label and textbox so I can add in error and warnings if required by jQuery later.

CSS:
#divBody {
    width:80%;
}
#divLeft, #divRight {
    float:left;
    width:45%;
    border:solid black 1px;
}
.spacer {
    float:left;
    background-color:red;
    width:100%;
}
input {
    width:50px;
}

Open in new window

HTML:
<div id="divBody">
    <div id="divLeft">
        <label>Field 1:</label>
        <input type="text"></input>
        <div class="spacer">Poss</div>
        
        <label>Field 2:</label>
        <input type="text"></input>
        <div class="spacer">Invalid</div>
    </div>
    <div id="divRight">
        <label>Field 1:</label>
        <input type="text"></input>
        <div class="spacer"></div>
        
        <label>Field 2:</label>
        <input type="text"></input>
        <div class="spacer"></div>
    </div>
</div>

Open in new window

Result:

Open in new window


What Im getting is:-
Current Result
What it should be is like:-
Should be
Ive stuck it on jsfiddle at http://jsfiddle.net/vnb06zou/1/ in the hope someone can help me :-S

What I dont understand is why the div is floating at the bottom of the area and not next to the text boxes as needed.
0
Comment
Question by:tonelm54
2 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40563144
'floats' need to have a specified width and that width must be less than or equal to the element that contains them.  This basically works, you can finish the details.  Note the inline 'style' and the <br clear="all"> to return to the left.
<div id="divBody">
    <div id="divLeft">
        <div style="float:left;"><label>Field 1:</label>
        <input type="text"></input>&nbsp;</div>
        <div class="spacer">Poss</div><br clear="all">
        
        <div style="float:left;"><label>Field 2:</label>
        <input type="text"></input>&nbsp;</div>
        <div class="spacer">Invalid</div>
    </div>
    <div id="divRight">
        <div style="float:left;"><label>Field 1:</label>
        <input type="text"></input>&nbsp;</div>
        <div class="spacer"></div><br clear="all">
        
        <div style="float:left;"><label>Field 2:</label>
        <input type="text"></input>&nbsp;</div>
        <div class="spacer"></div>
    </div>
</div>

Open in new window

0
 
LVL 6

Expert Comment

by:Tony O'Byrne
ID: 40566728
First thing worth noting is that floats are difficult to work with - even when I've been doing this professionally for 5 years.

Where possible, I prefer to not use floats.

I started from scratch and used your image as a prototype to match.  Though I didn't worry about getting it "pixel-perfect", it gets pretty darn close:
http://jsfiddle.net/vnb06zou/4/
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

776 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