Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Margin: 0 Auto not working in any browser

Featured Replies

Hello Everyone.

 

I have a #main div and inside that I have a couple of paragraphs and two images.

 

One of the images is floated to the left (which is what I want) but I want the other (further down the page to be centred). I have written the following code:

 

#main 
{
   position: relative;
   padding: 0 15px 0 15px;
   width: 800px;
}

#main img 
{
   float: left;
   margin: 0 25px 5px 0;
}

#main img.webtechnos 
{
   float: none !important;
   margin: 0 auto !important;
   padding: 30px 0 30px 0;
}

 

But the image still shows on the left and not centred. I would greatly appreciate any help.

 

Thank You.

<img> is an inline element so normally they don't pay attention to things like margin: 0 auto. What might help is in #main img.webtechnos adding display:block;, but it depends how the layout around this image is.

 

Try it and if it doesn't work would you be able to post a link to the page or the css and html code so it can be looked at in context to the rest of the layout :)

  • Author

@Dizi: I tried the display: block .. it didn't work.

 

@mteam: If you meant width for the image, I gave the image a width and still it doesn't work. If you mean the container of the image (in this case #main) that too has a width (see code above)

 

I don't have a web host to put the code in right now. :(

#main img.webtechnos  
{ 
   float: none !important; 
   margin: 0 auto !important; 
   padding: 30px 0 30px 0; 
   width:500px;/*width added in css*/

}

  • Author

Yep I did that mteam.

 

It didn't work :(

Yes sorry just tried it lol

 

this will work i've just use the class

 

.webtechnos  
{ 
   float: none !important; 
   margin: 0 auto !important; 
   padding: 30px 0 30px 0; 
   display:block;/*added display block*/
   width:500px;/*width added in css*/


}

Dizi said add display: block and mteam said add a width, did you try both?

#main img.webtechnos  
{ 
   float: none !important; 
   margin: 0 auto !important; 
   padding: 30px 0 30px 0; 
   width:500px;/*width added in css*/
   display: block;
}

 

That might work to place the image in the center of the parent (but not central between the side of the floated div and the right side).

 

[Looks like mteam beat me this time.]

Edited by Wickham

  • Author

I had tried that as well before and it didn't work.

 

But then accidentally my cursor went over my sidebar on the right (while in firebug) and I saw the problem ! lol

 

I had given the sidebar (on the right) such a long bottom padding that it was stopping the image from centring.

 

Thank You the problem is solved :D

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.