[SOLVED] Centering!

This is a discussion on "[SOLVED] Centering!" within the Web Page Design section. This forum, and the thread "[SOLVED] Centering! are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 21st, 2007, 03:05
SuperMember

SuperMember
Join Date: Sep 2007
Location: somewhere
Age: 18
Posts: 219
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Centering!

ok. my css is this...
HTML: Select all
img.displayed {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

my html is this...
HTML: Select all
<IMG class="displayed" src="http://www.webforumz.com/images/aboutus.png" alt="About us">

it doesn't center. lol
Last Blog Entry: My blog? (Oct 18th, 2007)

Last edited by mcdanielnc89; Oct 21st, 2007 at 18:27. Reason: WHen inserting code i didn't put the < in the front, but still doesn't work.
Reply With Quote

  #2 (permalink)  
Old Oct 21st, 2007, 04:40
Junior Member
Join Date: Nov 2006
Location: Los Angeles
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Dare I point out the obvious, though I'm sure its just a mistake:
Code: Select all
<img class="displayed" src="http://www.webforumz.com/images/aboutus.png" alt="About Us" />
You need to open the image tag. I've found that centering objects like images is done best like this:
Code: Select all
<div align="center">
     <img src="http://www.webforumz.com/images/aboutus.png" alt="About Us" />
</div>
Reply With Quote
  #3 (permalink)  
Old Oct 21st, 2007, 08:56
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Quote:
Originally Posted by trandrus View Post
I've found that centering objects like images is done best like this:
Code: Select all
<div align="center">
     <img src="http://www.webforumz.com/images/aboutus.png" alt="About Us" />
</div>
That method is deprecated. Use CSS and auto margins instead.

Applying margin: 0 auto; to an element will cause it to be centred inside the parent container (but this fails on IE5 and IE6 in quirks mode).

Do a search, either here or on Google. This has been covered many, many times.
Reply With Quote
  #4 (permalink)  
Old Oct 21st, 2007, 16:50
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Centering!

This should work as it usually requires you to specify a height and width:
HTML: Select all
img.displayed {
display: block;
margin: 0 auto;
height: 100px; 
width: 100px;
}
Obviously change the height and width.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #5 (permalink)  
Old Oct 21st, 2007, 17:51
Junior Member
Join Date: Nov 2006
Location: Los Angeles
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Total server work required for CSS solution: 571B
Total server work required for align='center': 528B

Try it yourself:
http://www.rossandrus.net/examples/d...d/standard.php
http://www.rossandrus.net/examples/d...deprecated.php

Test the load time:
http://www.websiteoptimization.com/services/analyze/

That's not even taking IE5 and 6 into consideration. The CSS solution also forces you to display the image as block which is less flexible for things like rows of horizontally centered icons, etc. Deprecated doesn't always mean bad.
Reply With Quote
  #6 (permalink)  
Old Oct 21st, 2007, 18:03
SuperMember

SuperMember
Join Date: Sep 2007
Location: somewhere
Age: 18
Posts: 219
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

hmm ok so which way is teh way to do itr if it's depreciated, and tyhe height and width would be pointless to do as that vlass is used for several other images...
Last Blog Entry: My blog? (Oct 18th, 2007)
Reply With Quote
  #7 (permalink)  
Old Oct 21st, 2007, 18:08
alexgeek's Avatar
Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Centering!

I just told you how to do iit the non-depcricated way.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #8 (permalink)  
Old Oct 21st, 2007, 18:11
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Quote:
Originally Posted by trandrus View Post
Total server work required for CSS solution: 571B
Total server work required for align='center': 528B

Try it yourself:
http://www.rossandrus.net/examples/d...d/standard.php
http://www.rossandrus.net/examples/d...deprecated.php

Test the load time:
http://www.websiteoptimization.com/services/analyze/

You've got to be kidding me. You're talking about a mere 8% increase in server work, for a tiny total amount of work, on a presentational effect that will be used sparingly.

Compared to the total work required for a typical webpage, this is irrelevant. It's far more important to understand the reasons for preferring CSS-based presentation to HTML-based presentation. Saving a few bytes here and there is of dubious value.

Moreover, since both pages are invalid HTML, they hardly constitute good examples. Finally, note that although the CSS solution has an initial performance cost, it becomes more efficient the more you use it.

Quote:
The CSS solution also forces you to display the image as block which is less flexible for things like rows of horizontally centered icons, etc.
No it doesn't. You can make the image inline, and place a <div> around it instead. Then centre the <div>.

Quote:
Deprecated doesn't always mean bad.
Yes it does, actually. http://en.wikipedia.org/wiki/Deprecated

Last edited by MikeHopley; Oct 21st, 2007 at 18:15.
Reply With Quote
  #9 (permalink)  
Old Oct 21st, 2007, 18:20
SuperMember

SuperMember
Join Date: Sep 2007
Location: somewhere
Age: 18
Posts: 219
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Quote:
Originally Posted by alexgeek View Post
This should work as it usually requires you to specify a height and width:
HTML: Select all
img.displayed {
display: block;
margin: 0 auto;
height: 100px; 
width: 100px;
}
Obviously change the height and width.
Okay, I guess I'm not quite understanding this aparently. WHy do I need to state the height and width if the css class is used for more than one image? If i do that; then wouldn't all images be the same and disportioned if they're not the same size?
Last Blog Entry: My blog? (Oct 18th, 2007)
Reply With Quote
  #10 (permalink)  
Old Oct 21st, 2007, 18:24
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

You should not state the height and width. For images inserted via the <img> element (as opposed to CSS backgrounds), the height and width are given in the HTML.

Width is required for centring other boxes (<div>s, typically) that lack an inherent width.
Reply With Quote
  #11 (permalink)  
Old Oct 21st, 2007, 18:28
SuperMember

SuperMember
Join Date: Sep 2007
Location: somewhere
Age: 18
Posts: 219
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

If that is the case MikeHopley then what is wrong with my code to make it not center? Is my CSS wrong?(Obviously it has to be)
Last Blog Entry: My blog? (Oct 18th, 2007)
Reply With Quote
  #12 (permalink)  
Old Oct 21st, 2007, 18:46
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Quote:
Originally Posted by mcdanielnc89 View Post
If that is the case MikeHopley then what is wrong with my code to make it not center? Is my CSS wrong?(Obviously it has to be)
What browser are you using? Can you provide a link?

I suspect this is IE nuttiness. But the simplest explanation requires that I know more about your browser/code.
Reply With Quote
  #13 (permalink)  
Old Oct 21st, 2007, 18:47
Junior Member
Join Date: Nov 2006
Location: Los Angeles
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Quote:
You're talking about a mere 8% increase in server work, for a tiny total amount of work, on a presentational effect that will be used sparingly
...
Finally, note that although the CSS solution has an initial performance cost, it becomes more efficient the more you use it.
If it's going to be used sparingly, then why does CSS make sense?
Quote:
It's far more important to understand the reasons for preferring CSS-based presentation to HTML-based presentation.
Yes, I agree it is important to understand the difference between the two. However, I would argue that it's far more important to understand the advantages and disadvantages for both cases. If you're trying to center a horizontal navigation bar on the page, why would you go through all the trouble of setting it up in CSS? The real power of CSS comes from targeting child objects that are repeatedly used in a page which decreases the needed amount of markup.
Quote:
Moreover, since both pages are invalid HTML, they hardly constitute good examples.
They're invalid because there is no doctype because I wrote it by hand in about 2 minutes. Google makes this same mistake, by the way:
http://validator.w3.org/check?uri=www.google.com
Reply With Quote
  #14 (permalink)  
Old Oct 21st, 2007, 18:48
SuperMember

SuperMember
Join Date: Sep 2007
Location: somewhere
Age: 18
Posts: 219
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Yes it is IE7, and a link to where one of the images not responding is located here.
Last Blog Entry: My blog? (Oct 18th, 2007)
Reply With Quote
  #15 (permalink)  
Old Oct 21st, 2007, 19:08
Junior Member
Join Date: Nov 2006
Location: Los Angeles
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

You need to set the class of your images to link with the information in your stylesheet. Your code modified:
Code: Select all
<div>
      <img class="mycenter" src="images/monthlysignin.png" alt="Monthly Sign-In Logo">
      <br>
      <br>
      <p class="style7">
        NBJF's Sign-in will be discontinued until the site is up
        and running 100%!<br>
        Thank you for your cooperation!
      </p>
      <br>
      <img class="mycenter" src="http://www.newbreedjesusfreaks.com/images/saesons.jpg"  alt="Seasons Logo" />
    </div>
And add the class to your stylesheet:
Code: Select all
img.mycenter {
    display:block;
    margin:0 auto;
}
If this doesn't work then it might be a browser bug related issue, in which case you should consult:
http://centricle.com/ref/css/filters/
But make sure your code is sound before you start looking into browser discrepancies.
Reply With Quote
  #16 (permalink)  
Old Oct 21st, 2007, 19:09
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Quote:
Yes it is IE7, and a link to where one of the images not responding is located here.
You need to specify width and height in the html. Like this:

Code: Select all
<img src="http://www.newbreedjesusfreaks.com/images/saesons.jpg" alt="Seasons Logo" width="200" height="100">
Quote:
Originally Posted by trandrus View Post
If you're trying to center a horizontal navigation bar on the page, why would you go through all the trouble of setting it up in CSS?
Because:
  1. It's no trouble if you actually know what you're doing.
  2. The CSS way is much more flexible, allowing you to change your mind about the style of the navbar.
Quote:
The real power of CSS comes from targeting child objects that are repeatedly used in a page which decreases the needed amount of markup.
The real power of CSS is the systematic separation of style from content, allowing site-wide changes to be made with minimal effort, and reducing the overall complexity of your code.
Reply With Quote
  #17 (permalink)  
Old Oct 21st, 2007, 19:24
SuperMember

SuperMember
Join Date: Sep 2007
Location: somewhere
Age: 18
Posts: 219
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

ok. my css is

Code: Select all
img.displayed {
    display: block;
    margin-left: auto;
    margin-right: auto;
}
html is now...

Code: Select all
<img src=
  "http://www.newbreedjesusfreaks.com/images/saesons.jpg"
  alt="Seasons Logo" width="382" height="271" class="displayed">
Last Blog Entry: My blog? (Oct 18th, 2007)
Reply With Quote
  #18 (permalink)  
Old Oct 21st, 2007, 20:23
Marc's Avatar
Moderator

SuperMember
Join Date: Apr 2007
Location: Scotland, UK
Age: 15
Posts: 1,636
Thanks: 0
Thanked 6 Times in 6 Posts
Send a message via MSN to Marc Send a message via Skype™ to Marc
Re: Centering!

Why not

Code: Select all
img.displayed{
display:block;  --------What is this being used for?? -----------------
margin:0 auto;
}
Reply With Quote
  #19 (permalink)  
Old Oct 21st, 2007, 20:27
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

Are you sure you've updated your styesheet? The style rules are not present.
Reply With Quote
  #20 (permalink)  
Old Oct 21st, 2007, 20:41
Junior Member
Join Date: Nov 2006
Location: Los Angeles
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering!

HTML objects can be rendered as either inline or block.

Inline elements like <span> and <img> have no default margins or padding and are intended to display on one line (not multiple lines) and therefore have no height attribute. If you try to specify the height of an inline element you will fail.

Block elements like <div> and <p> are intended as a block. There is default margin and padding (a carriage return to the user) and you can specify height and width attributes.

The CSS specifies the horizontal AND vertical margins for the image:
Code: Select all
margin: 0 /*vertical margins*/ auto /*horizontal margins*/;
and should therefore be redefined as a block element. This tells the image to automatically render the horizontal margins based on the parent DOM node and to set the vertical margins to 0.


Read about margins:
http://www.w3schools.com/css/pr_margin.asp

And inline vs. block elements:
http://www.webdesignfromscratch.com/...and-inline.cfm
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Centering Vertically R8515198 Web Page Design 5 Dec 6th, 2007 11:05
[SOLVED] page centering issues VanderBOOM Web Page Design 12 Dec 4th, 2007 15:06
[SOLVED] Centering horizontally and vertically Emzi Web Page Design 4 Nov 1st, 2007 16:07
[SOLVED] Problems with centering in CSS. mcdanielnc89 Web Page Design 26 Nov 1st, 2007 06:07
Tip : Vertical and Horizontal Centering - Solved! gwx03 Web Page Design 0 Dec 15th, 2003 06:11


All times are GMT. The time now is 05:16.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9