Centering a div with auto width?

Hi there,

I am trying to center the follow logo div so it appears centered horizontally, but it’s not working.

This is what I have tried:

		<header class="container-fluid">
			<div class="container">
			<div class="logo" style="margin: 0px auto;  display: table-cell;">
			<img src="images/nm-logo.png" />
			<div class="logo-text">
				<h2>Sub heading</h2>

Is it possible to center the div with the class logo without giving it a width? If not, I think I will just give it a fixed width.


Use display:table not table-cell and then it will centre horizontally.

1 Like

Thank you, that has now centered it.

However, the logo image is appearing on a separate line to the logo-text div. I’m wondering if this is because nothing has a set width?

It’s because logo-text is a block element (div) unless you have styled it differently.

1 Like

You don’t show the styles associated with those elements so I can’t second guess what you have done.:slight_smile:

Block elements like divs will of course naturally start on a new line unless you tell them not to.

Sorry, this is the CSS:

				margin: 0px auto; 
				display: table;
			.logo img{
				max-width: 90px;
				margin-right: 15px;
			.logo-text h1{
				font-size: 30px;
				padding: 0;
				margin: 0 0 15px; 0
			.logo-text h2{
				font-size: 22px;
				padding: 0;
				margin: 0

I’ve tried using display: flex which aligns the items on one line, but it doesn’t then center the whole thing.

I have created a fiddle:

Try this:

	display: flex;
1 Like

Thank you, that worked great :smiley:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.