Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement this layout using CSS float:left and float:right, or using CSS margin-left and position:absolute?

I want to place some annotations to the left of a topic using HTML and CSS (for example the 'status' and 'author' annotations shown in the following mockup/image):

example of desired layout

I prefer CSS instead of a table-based layout.

Annotations should be displayed before (to the left of) the heading, as shown above.

In the HTML, annotations should be located after the corresponding heading, e.g. as follows (because the information/contents associated with a topic are usually whatever is after the topic's heading):

<h1>This is a section title</h1>
<div class="status">approved</div>
<div class="author">chris</div>
<p>This is some text. Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<h1>Different section title</h1>
<div class="status">rejected</div>
<p>Lorem ipsum.</p>

There are (at least) two possibilities:

  • This answer uses float:right, with float:left
  • This answer uses margin-left, with position:absolute and left

Which of these (or any other answer) is the better way to implement this layout, and why?

like image 640
ChrisW Avatar asked Feb 23 '11 02:02

ChrisW


People also ask

How do you apply a float in CSS?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

How do you float a div to the right?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do I float an image to the right in HTML?

To use a floating image in HTML, use the CSS property float. It allows you to float an image left or right.

What is the difference between float and position in CSS?

Floats are meant to be used in images that you want the text to wrap around. Position absolute is meant to place elements with some kind of x and y coordinates.


2 Answers

Use the solution with floats. Absolute positioning should not be used here, because text is being positioned, and the layout is dependent upon the size of the text. If your user changes his browser to magnify the text-size, the layout will become distorted. You need to be especially conscious of this if you are designing pages for accessibility (consider using % instead of px for the size as well), but in general, use absolute positioning only when it is the only way to do what you want.

A great resource for understanding how to correctly use floats is this smashing magazine article. I bookmarked it a while back and I use it as a reference.

like image 185
smartcaveman Avatar answered Sep 30 '22 19:09

smartcaveman


No need for position:absolute. float:left will work best for this situation. float:right is also not needed. This solution is cross browser compatible. It will work in all browsers including quirks mode. Let me know how this works for you or if you need any changes.

Check working example at http://jsfiddle.net/PyHGy/9

If HTML order is important to you where annotations must come after corresponding heading, then we need to add position:absolute to .statusContainer and then adjust the margin in .titleContainer. This is also a cross browser compatible solution.

Check working example at http://jsfiddle.net/PyHGy/8/

like image 41
Hussein Avatar answered Sep 30 '22 18:09

Hussein