English

Learn HTML-CSS: How To Horizontally Center a Block Element div in a div?

Have you worked with div blocks? If yes, you already know how simple they can be. However, complexity can arise due to how you can stack simple concepts on top of one another. If you are looking to center a block element div in a div horizontally, then just use the following CSS code:

#innerdiv {

width: 50%;

margin: 0 auto;

}

The above code is for inner div of your HTML. We are using CSS to do the centering. The outer div can be of any width, all the way to 100%. All you need to make sure of is that the inner div width is less than the width of the outer div.

The magic line here is margin: 0 auto,  which centers the inner div.

What should you do if you want to make it work for IE8?

We all know supporting IE8 can be a pain. For the above to work in IE8, you need to use the following CSS.

#innerdiv {

display: table;

margin: 0 auto;

}

As div is a block-level element, the above CSS will work flawlessly.

Do you have anything to add to the tutorial? Then comment below and let us know.

You can also check on our website for videos about HTML/CSS. Below are some examples:

  • HTML5 App Development (part 1) – HTML5
  • Website Update (part 2) – HTML5

 

You can also follow some of our broadcasters who program in HTML-CSS as below:

 LearnToProgram

 awakekat

Another cool way to find out interesting things about HTML/CSS is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago