LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2012, 12:05 PM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
CSS for Aligning Inline Images


Hi,


Before I would post the problem here, I did some googling but could not find an example to address my problem.

Here is the problem statement:

(Look at the above two emoticons. The second one is right-aligned and first one is center-aligned. However, the center image looks okay because the Monk is not on the same line. It is on the second line. How would the center-image get aligned if both the images were on the same line?)

I have two inline images, <img /> tags, just after the <body> tag. The first image (Logo.jpg) is aligned right using CSS "float:right". I want other one (Welcome.jpg) to be center-aligned. HTML's "align=center" or CSS' "float:center" and even HTML's <center> tags do not seem to work properly because I want the second image to sit in the middle (horizontally, of course) at the top of the page irrespective of the other image's location. What is happening is the second image is getting aligned with proportion to the gap between the left edge of the page and the left side/border, the point from where the Logo image begins, of the right-aligned image.

Here is what I am doing:
PHP Code:
<body>

<
img src='logo.jpg' width='200px' style='border: 0px; float: right;' />

<
center><img src='welcome.jpg' height='100px' style='border: 0px; margin-left: 200px;' /></center>

<
hr />

BlahBlahBlahBlahBlah

</
body
I have used "margin-left: 200px;" intentionally so that the Welcome Image can move some more spaces towards the right side so as to look to be sitting in the center of the top of the page.

But that is not desirable. There has to be a proper solution using CSS. I could not find one by Googling on it.

Could anybody address it?

Last edited by devUnix; 01-19-2012 at 12:07 PM.
 
Old 01-19-2012, 12:42 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Would this work:

PHP Code:
<body>
<
div style="text-align:center">
<
img src='logo.jpg' width='200px' style='border: 0px; float: right;' />
<
img src='welcome.jpg' height='100px' style='border: 0px;' />
</
div>
<
hr />

BlahBlahBlahBlahBlah

</
body
 
Old 01-19-2012, 03:48 PM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by Cedrik View Post
PHP Code:
<div style="text-align:center"
Well, I should have mentioned that that also would not work. It is not working. It is giving the same identical result as center tag and other such techniques I described above.
 
Old 01-19-2012, 04:40 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Note that I put both images in the div, which puts them in same line

I tested and it works in firefox 8...

edit: I added clear:both in hr, though
PHP Code:
<hr style="clear:both" /> 
It is just for testing, ideally all those inline style should be in CSS sheet

edit2, maybe I did not understand the requirement

If you want the center image to be in absolute center, there are several ways
For example add negative right margin, with same size as right image width

PHP Code:
<body>
<
div style="text-align:center">
<
img src="logo.jpg" width="200px" style="border: 0px; float: right;" />
<
img src="welcome.jpg" height="100px" style="border: 0px;margin-right:-200px" />
</
div>
<
hr style="clear:both" />

BlahBlahBlahBlahBlah

</
body

Last edited by Cedrik; 01-19-2012 at 04:54 PM.
 
Old 01-20-2012, 04:12 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Make sure the container has position:relative; and put the tallest item into it normally. This makes sure the container will get the desired height. Then, use position:absolute;top:0;left:0;width:100%;height:100% sub-container to overlay another container on top of it.

PHP Code:
<div style="position:relative;">
 <
div align="center"center </div>
 <
div align="right" style="position:absolute;top:0;left:0;width:100%;height:100%;"right </div>
</
div
You can use the z-index:level; property to pick the layering order, higher values are on top, and negative values are in the background. If the window happens to be very narrow, the elements will go on top of each other; the layering order is important then.
 
Old 01-20-2012, 12:42 PM   #6
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
I will try what has been pointed out in the above two replies. Thanks!


Nominal Animal: I know you or your presence and ID on this web site for a long time. How are you doing?
 
Old 01-20-2012, 02:49 PM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by devUnix View Post
Nominal Animal: I know you or your presence and ID on this web site for a long time. How are you doing?
Still alive and kickin', so I guess I cannot complain.
 
Old 01-20-2012, 04:54 PM   #8
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

what I do sometimes is use the CSS magic with positioning (NominalAnimal)

like this (a working tested copy/paste code)
Code:
<body>
<div id="wrapper" style="width:962px; height:auto; text-align:center; margin:0 auto; background-color:#ddd;">
<!-- the above line makes a 'frame' for page to the center and align all text to center -->

<div id="page" style="width:762px; height:366px; background-color:#fff; margin:30px auto;">
     <!-- the page line makes a page 'frame' box to the center -->

<div style="position:relative; left:0px; top:0px; margin:0 auto; width:700px; height:120px; border:1px dotted red; z-index:1;">
<img src='linux-monk.gif' width='100px' style='border: 0px; float:right;' />
</div>
<div style="position:relative; left:0px; top:-120px; margin:0 auto; width:700px; height:120px; border:1px dotted green; z-index:2;">
<img src='cool_smiley.gif' width='100px' style='border: 0px;' />
</div>

<hr />

Blah! Blah! Blah! Blah! Blah! 

</div>
</div>

</body>

just remove that "border's" in your real code when used, there are just for an illustration .
good luck

Last edited by lithos; 01-20-2012 at 04:57 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mutt and inserting inline images above the signature scottro11 Linux - Software 5 06-12-2011 02:06 PM
inline images joeshmo LQ Suggestions & Feedback 1 10-11-2009 08:52 AM
send mail with inline images along with html in perl tanveer Linux - General 2 12-14-2007 05:23 AM
images and CSS rblampain General 2 02-28-2007 03:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration