How can I centre-align an item horizontally within another element?

To centre-align an element using CSS, we will make the margins either side of the element of equal width. First, ensure that the elements width is set to less than 100%.width: 80%; This is important as otherwise, changing the margins will have no effect as the margins will be nonexistent.Now, to ensure the left and right margins are equal, simply use:margin: auto; This will centre-align your element within its parent element.

DS

Related HTML and CSS Mentoring answers

All answers ▸

How can we integrate animation to websites?


What's wrong with the following HTML markup: <p style"font-size:10px;">Copyright <span>2015</span></p>?


Make a dropdown list with red type and yellow background.


Show how you would give all elements that have a class of 'class1', a green background.