Page 1 of 1

Automatically center HTML elements horizontally

Posted: Fri Aug 10, 2018 4:29 pm
by Nipuna
Today I am going to show you a simple code segment which will save you a lot of time.
Using this you can easily center any HTML element horizontally on all resolutions.

Here is the HTML part

Code: Select all

<div class="main-div">
    <div>Div 1</div>
    <div>Div 2</div>
    <div>Div 3</div>
</div>
This is where we do our magic. CSS

Code: Select all

.main-div{
 // This is what centers our elements
    text-align: center;
}
.main-div div{
 
// these all just to make the elements look good 
    width : 100px;
    background-color: red;
    margin: 10px 0;
    color: white;
    text-align: center;

// This is the important part
    display: inline-block;
}

Re: Automatically center HTML elements horizontally

Posted: Fri Aug 10, 2018 6:13 pm
by Neo
I have no idea why I'm very bad at learning HTML and CSS correctly. You are an expert, so your quick tips will surely helpful. Keep up the good work.

Re: Automatically center HTML elements horizontally

Posted: Fri Aug 10, 2018 7:33 pm
by Nipuna
Neo wrote:
Fri Aug 10, 2018 6:13 pm
I have no idea why I'm very bad at learning HTML and CSS correctly. You are an expert, so your quick tips will surely helpful. Keep up the good work.
Thank you Neo, Yeah I have seen many expert programmers have problems learning and coding HTML and CSS but they are very good at other languages, I think these syntax maybe bothering them.

Anyway Thank you Neo, If I am at a place right now, you and Expertcore helped me a lot for that.. Looking at my early posts I always remember how much I've learned from here and you at my early stages :)