Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text wraps around floating div but borders and <hr />s do not

I have a div that is float: right and it is inside a parent div. There are p elements inside that same parent div also, and the text wraps around the float: right div properly.

However, if I set the p elements to have a border, or do a <hr />, the border does not stop where the text stops, but extends behind the float: right div.

Here is a beautiful mspaint depiction of the situation:

Note that the green part of the black horizontal line is behind the floating div.

How do I get the border or <hr /> or whatever to be only as wide as the text, and not go behind the div?

like image 498
Seth Carnegie Avatar asked Apr 27 '11 01:04

Seth Carnegie


People also ask

How do I make a div float over a page?

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 you make an HR inline?

Its simple to add a horizontal line in your markup, just add: <hr>. Browsers draw a line across the entire width of the container, which can be the entire body or a child element. Originally the HR element was styled using attributes.

What happens if you use a float property on an absolutely positioned element?

Absolutely positioned page elements will not affect the position of other elements and other elements will not affect them, whether they touch each other or not. There are four valid values for the float property. Left and Right float elements those directions respectively.


2 Answers

I know this problem was posted some time ago, but I had the same problem today and found another solution:

http://jsfiddle.net/MvX62/

I use border-bottom instead of the <hr /> tag and had to add an overflow: hidden;. Look at the fiddle, i think this is more useful then the accepted solution, because you can also add a margin to the horizontal line and there is the same gap, as the text has.

Also you don't need to define z values and don't need any hacks or workarounds.

like image 145
iuiz Avatar answered Sep 20 '22 18:09

iuiz


I've had this problem before, and I wasn't sure if it was solvable.

In your case, however, you could wrap the green box with another element and swap margin with padding and set its background to #fff to cover the offending line.

like image 36
alex Avatar answered Sep 18 '22 18:09

alex