This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.
Smartphone icons created by Freepik - Flaticon
To horizontally center a block element (like <div>), use
Setting the width of the element will prevent it from stretching out to the edges of its container. The element will then take up the specified width, and the remaining space will be split equally between the two margins:.center { margin: auto; width: 50%; border: 3px solid green; padding: 10px; }
Note: Center aligning has no effect if the
property is not set (or set to 100%).To just center the text inside an element, use
.center { text-align: center; border: 3px solid green; }
Tip: For more examples on how to align text, see the CSS Text Effects chapter.
To center an image, set left and right margin to
and make it into a element:img { display: block; margin-left: auto; margin-right: auto; width: 40%; }