Search code examples
htmlcsspositioncenter

Cannot Centre a Div to be in the Middle of Page


I am trying to get my Div to be centered in the page, however I cannot get it to work properly.

Despite using Margin-left: auto and Margin-Right:auto it still doesn't center in the middle of the page.

Any help would be appreciated!

HTML:

<div class="OuterService">
            <div class="service">
                <div class="virus" style="width:250px; height:218px;">
                    <center><h3 style="width:200px; text-align:center">Computer Virus</h3>
                    <img src="virus.jpg" alt="virus" height="140px"/></center>
                </div>

                <div class="screenRepair" style="width:250px;">
                    <center><h3 style="width:auto; text-align:center">Screen Replacement</h3>
                        <img src="smashedScreen.jpg" alt="BrokenScreen" height="160px"/></center>
                </div>

                <div class="hardwareRepair" style="height:218px;">
                    <center><h3>Hardware Replacement</h3>
                        <img src="hardwareRepair.jpg" alt="hardwareRepair" height="130px"/></center>

                </div>

                <div class="WindowsReinstall" style="height:218px;">
                    <center>
                        <h3>OS Reinstallation</h3>
                        <img src="windowsInstall.jpg" alt="OS Reinstallation" height="150px;" width="220px;"/>
                    </center>
                </div>

                <div class="maintenance" style="width:250px;">
                    <center>
                        <h3>System Maintenance</h3>
                        <img src="SystemMaintenance.jpeg" alt="System Maintenance" height=150px;/>
                    </center>
                </div>
                <div class="SoftwareRepair" style="width:250px">
                    <center>
                        <h3>Software Repair</h3>
                        <img src="SoftwareRepair.png" alt="Software Repair" height="150px;" width="220px;"/>
                    </center>
                </div>
                <div class="MemoryUpgrades" style="width:250px; height:208px;">
                    <center>
                        <h3>Memory Upgrades</h3>
                        <img src="MemoryUpgrades.jpg" alt="Memory Upgrades" height="140px;"/>
                    </center>
                </div>

                <div class="DataRecovery" style="width:250px;">
                    <center>
                        <h3>Data Recovery</h3>
                        <img src="DataRecovery.jpg" alt="Data Recovery" height="150px;"/>
                    </center>
                </div>
            </div>
        </div>

CSS:

.outerService {
                width: auto;
                margin-left: auto;
                margin-right: auto;
            }

            .service {
                max-width: 1100px;
                display: table;
                margin-bottom: 20px;
                margin-left: auto;
                margin-right: auto;
            }
            .virus, .screenRepair, .hardwareRepair, .WindowsReinstall, .maintenance, .SoftwareRepair, .MemoryUpgrades, .DataRecovery {
                width:250px;
                float:left;
                margin-left: auto;
                margin-right: auto;
                margin-top: 0;
                border:1px solid #000;
            }

Solution

  • You need to specify a width for your class outerService

    .outerService
    {
        width: 70%;
        margin-left: auto;
        margin-right: auto;
    }
    

    Also, if you want outerService to not specify a size you can use flexbox. Assuming that the container of outerService is the body..

    body
    {
        display: flex;
        justify-content: center;
    }
    .outerService
    {
        width: auto;
    }
    

    Flex is currently supported on all major browsers in their current versions but if you're supporting older versions, look here.