English

Learn HTML-CSS “How To Align a div to The Middle horizontally on a Page?

Are you looking for aligning a <div> tag horizontally to the middle of the page using CSS? If so, you have come to the right place.

Aligning a <div> tag horizontally to the middle of the page using CSS can be done with the help of the code shown below.

HTML

<body>

   <div id="centered">

       Content that needs to be centered

   </div>

</body>

CSS

#centered {

 width:900px; //You can use any width according to your requirement.

 margin:0 auto;

 border: 2px solid grey;

 padding: 100px;

 text-align: center;

}

The above code does the job. However, you may want to make sure that no issues arise when you are using IE6. To make it work for IE6, you need to add “text-align: center” to the body and then add “simple text-align: left” to your div to make everything work. Easy, right? These corner cases will ensure that your code works on every browser out there and can be used by anyone on the web.

That’s all! Do you have any other trick that can help? If yes, don’t forget to share it in the comment section below.

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

  • HTML5/CSS/JS (Full HTML markup process) (part 12) – HTML-CSS
  • [PT/EN] Website From Scratch (part 10) – HTML-CSS

You can also follow some of our broadcasters who program in JavaScript as below:

 Chocsx

 alexsimkovich

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

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…

1 month 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…

3 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…

6 months ago

Highest Stable Coin Yields – (W16 – 2024)

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

7 months ago

LEDU Token OTC Trading

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

8 months ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

8 months ago