Search code examples
htmlcssbackgroundcenter

Center a background image with css


I'm trying to center a background image with margin: 0 auto, but is not working, I've already put display: block;

:root {
    background-color: black;
    margin: 0 auto;
}
header  {
    background-image: url('../imagens/fundoheader.png');
    background-repeat: no-repeat;
    height: 100%;
    display: block;
    margin: 0 auto;
}
ul.menu {
    text-align: center;
}
.menu li {
    display: inline;
    margin-left: 10px;
    color: #f8841c;
}
li img {
    width: 171px;
    height: 52px;
    position: relative;
    top: 10px;
}
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>My Site</title>
        <link rel="stylesheet" type="text/css" href="css/reset.css">
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css">
        <script type="text/javascript"></script>
    </head>
    <body>
        <header>
                <ul class="menu">
                    <li><img src="imagens/logo.png"></li>
                    <li>O QUE É?</li>
                    <li>QUERO PARTICIPAR</li>
                    <li>COMO CHEGAR?</li>
                    <li>PROGRAMAÇÃO SEMANAL</li>
                    <li>EVENTOS</li>
                    <li>RESERVAS</li>
                </ul>
        </header>
        <section>




        </section>
    </body>
</html>


Solution

  • I think you are looking for the background-position property

    background-position: center;
    

    You could also combine your background properties into one line like this:

    background: url('.../imagens/fundoheader.png') no-repeat center;