Click here to Skip to main content
15,881,831 members
Articles / Web Development / HTML

Interface Experiences: Float Right CSS!

Rate me:
Please Sign up or sign in to vote.
2.88/5 (5 votes)
17 May 2013CPOL3 min read 21.6K   4   12
There is more to styles in web development than .class and #id. The structure of your HTML is also important.

Introduction

There is more to styles in web development than .class and #id. The structure of your HTML is also important. This post will describe a recent problem a colleague had with his UI and how his issue was resolved. Basic HTML and CSS experience is assumed.

Background

Below is a basic example of what, I’ll call him “Joe”, needed to accomplish:

Price

20

Tax

1.34

Total

21.34

This is a disclaimer regarding purchases,
tax, and the like…

 

 

 

 

 

As you can see, there is simply a table on the left and a disclaimer on the right. This is what Joe was hoping for. Instead, he got this:

Price

20

Tax

1.34

Total

21.34

 

 

 

 

 

This is a disclaimer regarding purchases,
tax, and the like…

Above shows the table on the left and the disclaimer on the right but, the disclaimer is below the table. The intent is for the table and the disclaimer to be inline. How can this be fixed? Let us first look at Joe’s first version of his HTML and CSS. Below will describe Joe’s first attempt at developing the user interface.

Attempt #1 – Fail

When Joe first wrote his styling and HTML, he was thinking of the left-to-right (LTR) reading direction which is only natural. The issue with this is there is nothing natural about this page. Below is the HTML and CSS used:

HTML
<style type="text/css">
.disclaimer
{
   float: right;
}
</style>
<div>
   <table>
      <tr>
         <td>Price</td>
         <td>20</td>
      </tr>
      <tr>
         <td>Tax</td>
         <td>1.34</td>
      </tr>
      <tr>
         <td>Total</td>
         <td>21.34</td>
      </tr>
   </table>
   <div class="disclaimer">
      <span>This is a disclaimer regarding purchases, tax, and the like...</span>
   </div>
</div>

Here the table and the disclaimer are included in a div element classed as “price-section”. The table is created as usual and then the disclaimer is created. The disclaimer consists of a div element classed as “disclaimer” with an inner span element containing the disclaimer text. The only style applied here is for .disclaimer where a float:        right; style is used. This accomplishes the basic need for the disclaimer to display towards the right side of the page. Though, it is also displaying below the table. Fortunately, there is a very simple solution!

Attempt #2 – Success!

When Joe first wrote his styling and HTML, he was thinking of the left-to-right (LTR) reading direction which is only natural. The issue with this is there is nothing natural about this page. Below is the revised version of his markup:

HTML
<style type="text/css">
.disclaimer
{
   float: right;
}
</style>
<div>
   <div class="disclaimer">
      <span>This is a disclaimer regarding purchases, tax, and the like...</span>
   </div>
   <table>
      <tr>
         <td>Price</td>
         <td>20</td>
      </tr>
      <tr>
         <td>Tax</td>
         <td>1.34</td>
      </tr>
      <tr>
         <td>Total</td>
         <td>21.34</td>
      </tr>
   </table>
</div>

The table and the disclaimer are included in a div element like before. The difference is the disclaimer is created before the table! This conflicts with the normal translation between the LTR reading direction and the hierarchical flow of markup but it has everything to do with how a browser renders the markup and applying the float: right; style.

As you can see in the screenshot below, the previous markup produces the expected results:

Price

20

Tax

1.34

Total

21.34

This is a disclaimer regarding purchases,
tax, and the like…

Conclusion

As mentioned in the beginning of this post, there is more to styles in web development than .class and #id. In this case, the order of the elements in Joe’s HTML document directly impacted the results of the CSS applied. Keep this in mind when you are having “styling” issues.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Caleb is a software development consultant specialized in creating web solutions for critical business problems. He has a passion for front-end development and helping other developers find their role. He enjoys making development easier to do, easier to learn and easier to improve upon. His days are pleasantly filled with TypeScript, HTML5 and C#.

Comments and Discussions

 
QuestionYet another way... Pin
Jeremy Falcon17-Jul-15 4:52
professionalJeremy Falcon17-Jul-15 4:52 
QuestionTop Blog Pakistan Pin
Member 1160892214-Apr-15 23:29
Member 1160892214-Apr-15 23:29 
GeneralMy vote of 1 Pin
Sunasara Imdadhusen19-May-13 18:55
professionalSunasara Imdadhusen19-May-13 18:55 
GeneralRe: My vote of 1 Pin
Caleb McElrath22-May-13 1:40
professionalCaleb McElrath22-May-13 1:40 
GeneralRe: My vote of 1 Pin
Afzaal Ahmad Zeeshan4-Jan-15 7:34
professionalAfzaal Ahmad Zeeshan4-Jan-15 7:34 
QuestionWhy not Pin
HaBiX17-May-13 18:34
HaBiX17-May-13 18:34 
AnswerRe: Why not Pin
Caleb McElrath22-May-13 1:59
professionalCaleb McElrath22-May-13 1:59 
GeneralRe: Why not Pin
HaBiX22-May-13 5:31
HaBiX22-May-13 5:31 
GeneralRe: Why not Pin
Caleb McElrath22-May-13 6:43
professionalCaleb McElrath22-May-13 6:43 
SuggestionFireFox/Google Chrome precisam atributo z-index:1; Pin
Sergio (santarede)16-Aug-12 10:39
Sergio (santarede)16-Aug-12 10:39 
GeneralRe: FireFox/Google Chrome precisam atributo z-index:1; Pin
Caleb McElrath30-Oct-12 9:20
professionalCaleb McElrath30-Oct-12 9:20 
GeneralMy vote of 5 Pin
Sergio (santarede)16-Aug-12 10:24
Sergio (santarede)16-Aug-12 10:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.