How can I center this button horizontally to my page?

I am using SharePoint, and I want to center this to the page horizontally. I am constrained by the limitations of SharePoint at my job. I am using a Scrip Editor in a Web Part. I have tried adding line-height: 0; & display:inline-block; margin:0 auto; width: 200px;

<html>
 <head>
  <style>
   .dropbtn { 
    background-color: transparent !important;
    padding: none;
    font-size: 0px;
    border: none;
    padding-bottom: 0;
    padding-top: 0;
    padding-Left: 0;
    padding-right: 0;
    }

   .dropdown {
    position: relative;
    display: inline-block;
    background: #ffffff;
    outline: none !important;
    }

   .dropdown-content {
    display: none;
    position: absolute;
   /* The following will affect sub menu color*/
    background-color: #78AB46   ;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    right: 0  !important;
    }

   .dropdown-content a {
    color: black;
    padding: 10px;
    text-decoration: none;
    display: block;
   }

   .dropdown-content a:hover {background-color: #C0D9AF;
   }
   .dropdown:hover .dropdown-content {display: block;}

   </style>
   </head>
    <body>

    <div class="dropdown">

    <button class="dropbtn"><img 
    src="https://assets.webiconspng.com/uploads/2017/09/Buttons-PNG-Image-36425.png" width="700"  /> 
    </button>

    <!-- Change link by placing inbetween >< -->
    <!-- Copy <a href="#">Link 1</a> to add a new line -->

    <div class="dropdown-content">
       <a href="#">Link 1</a>
       <a href="#">Link 2</a>
       <a href="#">Link 3</a>
    </div>
    </div>

    </body>
    </html>

2 answers

  • answered 2019-10-15 16:39 Moïze

    In your class .dropdown remove display:inline-block and add margin:auto and the width you want to set (based on your code it's 700) width:700px;

    .dropdown {
        position: relative;
        width:200px;
        margin:auto;
        background: #ffffff;
        outline: none !important;
    }
    

    Check this example (I changed your button width to 200px for demonstration): https://jsfiddle.net/uocwk4br/

  • answered 2019-10-16 02:02 LZ_MSFT

    Modify the code as below.

    <style>
    .dropbtn { 
        background-color: transparent !important;
        padding: none;
        font-size: 0px;
        border: none;
        padding-bottom: 0;
        padding-top: 0;
        padding-Left: 0;
        padding-right: 0;
    }
    
    .dropdown {
        position: relative;
        display: inline-block;
        background: #ffffff;
        outline: none !important;
    }
    
    .dropdown-content {
        display: none;
        position: absolute;
        /* The following will affect sub menu color*/
        background-color: #78AB46   ;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
        right: 0  !important;
    }
    
    .dropdown-content a {
        color: black;
        padding: 10px;
        text-decoration: none;
        display: block;
    }
    
    .dropdown-content a:hover {
        background-color: #C0D9AF;
    }
    .dropdown:hover .dropdown-content {display: block;}
    
    </style>
    <center>
    <div class="dropdown">
        <button class="dropbtn">
            <img src="https://assets.webiconspng.com/uploads/2017/09/Buttons-PNG-Image-36425.png" width="700"  /> 
        </button>
        <!-- Change link by placing inbetween >< -->
        <!-- Copy <a href="#">Link 1</a> to add a new line -->
        <div class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
            <a href="#">Link 3</a>
        </div>
    </div>
    </center>