Solved

Floats not working the way I wanted

Posted on 2015-01-21
2
66 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Urgent need help ASAP With CSS 5 38
Urgent Help with HTML CSS Positioning 9 43
razorCMS: Change Menu Font 4 49
Message not shown 5 33
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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 …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now