Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS float right moves element right and down (I don't want down).

Tags:

css

position

I have a table (bootstrap themed, generated from Django admin).

In one of the columns I have a div, which contains three elements, and anchor and two spans - each span to display bootstrap glyphicon.

<div class="my-fixed-width under-review data-sent-false">
    <a href="myobjectview/789/" style="inline-block;">C4U0UACXX-8 6nb</a>&nbsp;
    <span class="glyphicon glyphicon-asterisk" style="color:blue"></span>
    <span class="glyphicon glyphicon-pause" style="color:darkgray"></span>
</div>

I would like to have the icons moved to the right (ideally lined up between table elements in the same column).

My problem is that when I add float:right to the spans, it moves them right, but also down and expands the div height.

before float:right is added

After the float:right is added :

icons are pushed down as well as right

How can I keep the icons at the same vertical position as before, while moving the elements right? (I have tried position:absolute, and clear:both).

like image 594
wobbily_col Avatar asked Feb 28 '15 16:02

wobbily_col


People also ask

How do you float a right 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 stop DIVS from sliding when floating?

Set height and width to 100% for the bottom div and set overflow auto on the bottom div as well. Now the div will not move up to fill space.

Which CSS property is used to prevent elements from floating?

Note: Elements next to a floating element will flow around it. To avoid this, use the clear property or the clearfix hack (see example at the bottom of this page).

How do you stop a floating element in CSS?

To clear a float, add the clear property to the element that needs to clear the float. This is usually the element after the floated element. The clear property can take the values of left , right , and both . Usually you'll want to use both.


2 Answers

This question has been here a while, but I found a good answer so I want to share.

According to this answer I found elsewhere on StackOverflow, the elements that you want to have floated right need to be given first in your html structure.

<div class="my-fixed-width under-review data-sent-false">
    <span class="glyphicon glyphicon-asterisk" style="color:blue"></span>
    <span class="glyphicon glyphicon-pause" style="color:darkgray"></span>
    <a href="myobjectview/789/" style="inline-block;">C4U0UACXX-8 6nb</a>&nbsp;
</div>

This bug was giving me all sorts of trouble on my own website, but once I found this out I realized that it's actually quite simple to understand the fix. When you put a float:right element after everything else, then it will float to the right just like you asked it to. But if there's not enough room to the right (or if some quirk of browser rendering makes it think there's not enough room) then that element gets pushed down as well, so the browser is satisfied that it will fit. But if you put the float:right element first, then it goes right where it's supposed to before the browser lays out any other elements. Then the ones without float:right get put in according to their usual layout, including adjusting auto-widths or auto-margins to accommodate floated elements.

It didn't happen when I was testing this, but this configuration might still cause both of them to be on top of each other even if they're not initially pushed down from their original position, but if that happens try adding the display:inline-block like this:

span.glyphicon{
    float:right;
    display:inline-block;
}

See this JSFiddle on an example of it working with the spans placed before the anchor.

like image 76
Ashe Erickson Avatar answered Sep 24 '22 03:09

Ashe Erickson


if it goes down when you don't want it to then simply add a "Margin-top: -(###)px;" to the CSS

like image 26
Jimnah Avatar answered Sep 20 '22 03:09

Jimnah