473,498 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline float right, but not down?



Greetings,

I've been playing with CSS inline floats of <span> elements and the
results are not quite what I would expect. In the example attached
at the end of this post, I would expect the floated span 2 to be
on the same line as span 1, but instead it is on the same line
as span 3, which is on the line below span 1. Eric Meyer's CSS
pocket reference gives a clue as to why this is so: "A floated
element will generate a block-level box no matter what kind
of element it may be". Thus, my floated spans are "elevated"
to block level status and are therefore not placed on the same
line in which they are declared.

Now, I can compensate for this by using a negative top margin
equal in magnitude to the line-height as is done with span 2b
in the example below.

My questions are:

1. Is my method of getting the floated span on the same
line reliable under major recent 6th generation browsers
(all my tests so far were done under Firefox).

2. Is there a better way to get an inline element against the
right/let side of the paragraph containing a line, such as
when placing a hyperlink box to the right of a line of text
that describes and points to it?
Thanks in advance,

Mike Shell
------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Floated Element Height Test</title>
<meta http-equiv="Content-type"
content="text/html; charset=utf-8"></meta>
<style type="text/css">
body {line-height:1.25em;}
span {margin:0em; padding:0em; border:1px solid red;}
</style>
</head>

<body>
<p>
Body has a line-height (leading) of 1.25em.
</p>
<hr></hr>
<p>
<span>Span 1</span>
<span style="float:right;">Right Floated Span 2</span>
<br/>
<span>Span 3</span>
</p>
<hr></hr>
<hr></hr>
<p>
<span>Span 1b</span>
<span style="float:right; margin-top:-1.25em;">Right
Floated Span 2b with -1.25em top margin</span>
<br/>
<span>Span 3b</span>
</p>
<hr></hr>

</body>
</html>

------

Jul 21 '05 #1
3 19390
Els
Michael Shell wrote:
Greetings,

I've been playing with CSS inline floats of <span> elements and the
results are not quite what I would expect. In the example attached
at the end of this post, I would expect the floated span 2 to be
on the same line as span 1, but instead it is on the same line
as span 3, which is on the line below span 1. Eric Meyer's CSS
pocket reference gives a clue as to why this is so: "A floated
element will generate a block-level box no matter what kind
of element it may be". Thus, my floated spans are "elevated"
to block level status and are therefore not placed on the same
line in which they are declared.
Correct.
Now, I can compensate for this by using a negative top margin
equal in magnitude to the line-height as is done with span 2b
in the example below.
No need.
2. Is there a better way to get an inline element against the
right/let side of the paragraph containing a line, such as
when placing a hyperlink box to the right of a line of text
that describes and points to it?


Place the float before the rest of the text that you want on the same
line.

Instead of this:

<span>Span 1</span>
<span style="float:right;">Right Floated Span 2</span>
<br/>
<span>Span 3</span>

write this:

<span style="float:right;">Right Floated Span 2</span>
<span>Span 1</span>
<br/>
<span>Span 3</span>
--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Now playing: The Stranglers - All Day And All Of The Night
Jul 21 '05 #2
On Mon, 27 Jun 2005 12:37:05 +0200
Els <el*********@tiscali.nl> wrote:

Place the float before the rest of the text that you want on the same
line.

Well, it makes me feel a bit like an idiot, but at least I got a good
answer. In my defense, this is very counter intuitive - I would have
thought that placing it first would have given me an additional
line break/space at the start of the structure. For the record,
display:inline will not help either.
Thank you for your help,

Mike
Jul 21 '05 #3
Els
Michael Shell wrote:
On Mon, 27 Jun 2005 12:37:05 +0200
Els <el*********@tiscali.nl> wrote:
Place the float before the rest of the text that you want on the same
line.
Well, it makes me feel a bit like an idiot, but at least I got a good
answer. In my defense, this is very counter intuitive - I would have
thought that placing it first would have given me an additional
line break/space at the start of the structure.


Depends how you think about it - I see the float as "shove this to one
side, and see how much will fit in the freed space".
A bit like organising the mess in the attic or garage ;-)
For the record,
display:inline will not help either.
Display:inline and float are mutually exclusive :-)
Thank you for your help,


You're welcome.

--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6599
by: Günther Stößl | last post by:
Hello I am trying to position two floats inside a table column. <td> <div style="position:relative; width:100%"> <div style="position:absolute; left:0px; width:60%>... </div> <div...
1
3305
by: JKenyon | last post by:
I am attempting to display two images on a web page. The second "image" is actually two images, one overlaid on the other. The first one is aligned on the left using float:left. I have enclosed...
10
10262
by: Jesper Rønn-Jensen | last post by:
Hope you can help me out on this one. I have a a box that is floated right. Below (in the normal float) I have a wide table. <body> <div style="float:right; width:200px">...</div> <p>This...
9
17922
by: leegold | last post by:
Why does float:right cause a line break? Try the code below. Is there a fix? I want the link on the same line as the text. Thanks <html> <head> <STYLE> span.linkpos { float:right; clear:...
5
4901
by: Oliver Block | last post by:
Hello everybody, I wonder why a <span style="float:right">some text</span> might be displayed out of a surrounding div element. It is shifted to the next line. I thougt it is supposed to...
22
51398
by: ashkaan57 | last post by:
Hi, I am trying to put text on left and right side of the page and used: <div> <span>blah blah</span> <span style="float:right">blah blah</span> </div> The 2nd text does go to the right but the...
2
6388
by: paragpdoke | last post by:
Hello All. I have a weird observation regarding the CSS float style for span. Allow me to share details before asking my question: Screen resolution: 1024x768 (I know my monitor is outdated; it...
2
3776
by: seegoon | last post by:
Hi guys. I have a footer set to display:inline-block so that it'll shrink to fit its contents. It works fine when it follows other block or inline elements, it seems. However, when it follows a...
1
3708
by: Chinsu | last post by:
<html> <head> <style> #ex { width: 700px; background: red; float: left; } a, span { display: block;
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.