According to this answer, putting display: block; float: none; position: relative;
in a selector should ensure that the margin: 0px auto
trick to horizontally center an element works. This is my code for attempting to center the body
within the html
element:
body {
background-color: lightgray;
float: left;
font-family: 'Lato', 'Roboto', 'Arial', 'Verdana', sans-serif;
margin: 0px auto;
/* Adding these last 3 doesn't seem to make a difference */
display: block;
float: none;
position: relative;
}
When I specify the pixels manually like 0px 500px
, it works out fine. Does anyone know why auto
doesn't seem to be working in this case?
edit: Guys, it doesn't work even when I specify the width for the body. https://jsfiddle.net/ozm2x9zx/