The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Centering floated things, How to get css (or something) to center floated elements
Ephraim F. Moya
post Jul 11 2010, 10:30 AM
Post #1


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



Does anyone know how to center floated elements after they've been floated?

I know this doesn't make much sense but take a look at one of the album indexes on my site's Photo section and see what I mean. http://174.64.56.193/ in the PHOTOS project. With CSS i'm able to fit sort of random width photos into a div. But the last row, if its short, is also left aligned.

I want all the rows centered.

EFM

This post has been edited by Ephraim F. Moya: Jul 11 2010, 10:31 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 11 2010, 10:36 AM
Post #2


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



You mean like this page?
http://174.56.64.193/Photos/?act=alb&al=8
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jul 11 2010, 11:15 AM
Post #3


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(pandy @ Jul 11 2010, 09:36 AM) *


Yes.
But if you look at the last line the photo is stuck left. That's the line I'd like centered. Look at this page at the bottom: http://174.56.64.193/Photos/?act=alb&al=1 . Those panoramas I'd like centered.

Thanks
EFM

This post has been edited by Ephraim F. Moya: Jul 11 2010, 11:18 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 11 2010, 12:55 PM
Post #4


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



Can't think of a way, not with everything floated like that.

But since it's fixed with anyway and the images seem to be sized to fit three in a row, couldn't you group them three by three and center them? I.e. no floats, just centering and margins.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jul 11 2010, 02:05 PM
Post #5


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(pandy @ Jul 11 2010, 11:55 AM) *

Can't think of a way, not with everything floated like that.

But since it's fixed with anyway and the images seem to be sized to fit three in a row, couldn't you group them three by three and center them? I.e. no floats, just centering and margins.


The style for the width is min-width. It can get larger if the photo pushes it. Then I wanted fewer per line with the whole line centered. I could do it easily with a table. Also, divs can contain divs. I used to do it with a p holding the images only but I decided to add the caption which complicated things. Again, a table with the caption in another row would solve the problem -- but NOOOO.

Thanks,


This post has been edited by Ephraim F. Moya: Jul 11 2010, 02:09 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 11 2010, 03:35 PM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



Wouldn't the single image be in the left cell all alone in a table?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jul 11 2010, 05:00 PM
Post #7


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(pandy @ Jul 11 2010, 02:35 PM) *

Wouldn't the single image be in the left cell all alone in a table?


I used to do:

<p style=center><img...><img...><img...></p>

and that did what I want since the images are inline and they're all the same height.

Then I decided to add a caption to each image.

With tables:
<p style=center><table><tr><td><img...></td></tr><tr><td>caption</td></tr></table><table etc></p>
I haven't tried this but I think it might work. But its not strict. I might be able to use a div??

Without tables:
<div>
<div float:left>
<div min-width:33.3%>
<div text-align:center><img...></div>
<div text-align:center>caption</div>
</div>
etc
</div>
<div clear:both></div>
</div>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 11 2010, 06:15 PM
Post #8


.
********

Group: WDG Moderators
Posts: 9,658
Joined: 10-August 06
Member No.: 7



You might use "display: inline-block" like this:

CODE

div {border: 1px solid #000;}

#container {
float: left;
width: 320px;
text-align: center;
}
#container span {display: inline-block;}
#container span img {display: block;}


<div id="container">

<span>
<img src="dog.jpg" width="100" height="50" alt="">
caption
</span>

</div>


IE6/7 only support "display: inline-block" when applied on inline elements (hence the SPANs). According to IETester the above even works in IE5.5, which sounds too good to be true (IETester bug?).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jul 11 2010, 08:20 PM
Post #9


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



Christian,

By golly! You've done it! It works. Take a look. http://174.56.64.193/

I'm working on it now to determine the stability. But it looks good so far.

Thanks,
EFM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 12 2010, 04:50 AM
Post #10


.
********

Group: WDG Moderators
Posts: 9,658
Joined: 10-August 06
Member No.: 7



Forgot to say that Firefox 2 and older don't support this...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 12 2010, 05:14 AM
Post #11


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



Yup, in the version of Gecko I looked in the images come one under the other.

There's also this, but from the look if it I think it would still require grouping.
http://www.pmob.co.uk/pob/centred-float.htm
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 12 2010, 11:06 AM
Post #12


.
********

Group: WDG Moderators
Posts: 9,658
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Jul 12 2010, 12:14 PM) *

Yup, in the version of Gecko I looked in the images come one under the other.

People always mention the "display: -moz-inline-box" and similar CSS extensions, but I've never made them work. Any luck for you?

QUOTE
There's also this, but from the look if it I think it would still require grouping.
http://www.pmob.co.uk/pob/centred-float.htm

That page linked to http://www.search-this.com/2008/08/28/lets...ock-in-a-block/ which shows how to make IE6/7 support inline-block even on (default) block elements, excellent! cool.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Jul 12 2010, 11:21 AM
Post #13


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



In testing Christian's solution above I found that a single line of photos did not center and the whole page did not flow when the width of the page is changed.

By changing the css and mark-up like this I solved both problems. Also note that float: is not required. So the layout works easier.

#container { text-align: center; }
#container span {display: inline-block;}

<div id="container">

 <span>
  <img src="..." height="50" alt=""><br>
  caption
 </span>
:more spans:

</div>

This post has been edited by Ephraim F. Moya: Jul 12 2010, 11:31 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 12 2010, 11:37 AM
Post #14


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



QUOTE(Christian J @ Jul 12 2010, 06:06 PM) *


People always mention the "display: -moz-inline-box" and similar CSS extensions, but I've never made them work. Any luck for you?




Don't think I've tried.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 12 2010, 02:42 PM
Post #15


.
********

Group: WDG Moderators
Posts: 9,658
Joined: 10-August 06
Member No.: 7



QUOTE(Ephraim F. Moya @ Jul 12 2010, 06:21 PM) *

In testing Christian's solution above I found that a single line of photos did not center and the whole page did not flow when the width of the page is changed.

By changing the css and mark-up like this I solved both problems. Also note that float: is not required.

True, the width and float make no sense and I should have removed them before posting. blush.gif

But by making the images "display: block" (like I did) you don't need any BR elements.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 02:33 PM