html - Tricky horizontal center in css : floating elements only -


i can't find way align button in middle of floating elements.

button {    float:left;  }      header {    overflow: hidden;    background: #222;  }    header a, header label {    display: block;    padding: 20px;    color: #fff;    text-decoration: none;    line-height: 20px;  }    header a:hover, header label:hover { color: #aaa; }    header label {    float: right;    padding: 18px 20px;    cursor: pointer;  }    header label:after {    content: "\2261";    font-size: 1.8em;  }    .logo {    float: left;    font-weight: bold;    font-size: 1.5em;  }      nav {    float: right;    max-height: 0;    width: 100%;    -webkit-transition: max-height 0.3s;        -moz-transition: max-height 0.3s;         -o-transition: max-height 0.3s;            transition: max-height 0.3s;  }    nav ul {    margin: 0;    padding: 0;     padding-bottom: 10px;  }      nav li {    display: block;    text-align: center;  }      nav {    padding: 10px;    width: 100%;  }    #nav { display: none; }    #nav:checked ~ nav {    max-height: 200px; /* can bigger nav height. transition duration works */  }    @media screen , (min-width: 700px) {        header label { display: none; }        nav {      width: auto;      max-height: none;    }        nav ul {      padding: 0;      padding-right: 10px;    }        nav li {      display: inline-block;      text-align: left;    }        header nav {      display: inline-block;      padding: 20px 10px;      width: auto;    }      }
<header>      <a class="logo" href="http://minimaldev.com">minimal menu</a>        <input id="nav" type="checkbox">    <label for="nav"></label>        <button>centered button ?</button>        <nav>      <ul>        <li><a href="#">one</a></li>        <li><a href="#">two</a></li>        <li><a href="#">three</a></li>        <li><a href="#">four</a></li>      </ul>    </nav>     </header>

see example on codepen

i tried display-table trick, breaks navbar behaviour on small device (responsive).

i tried this technique , have same problem.

any idea ? thought using calc(), can't find right formula.

i made couple of edits:

  • added text-align: center parent element of button
  • bumped button off top edge manual pixel adjustments (assuming design stays rigid in terms of logo size, not responsive solution because of non-responsive nature of font , padding sizing).

here in codepen

html:

<header>    <a class="logo" href="http://minimaldev.com">minimal menu</a>    <input id="nav" type="checkbox">   <label for="nav"></label>    <button>centered button</button>    <nav>     <ul>       <li><a href="#">one</a></li>       <li><a href="#">two</a></li>       <li><a href="#">three</a></li>       <li><a href="#">four</a></li>     </ul>   </nav>   </header> 

css:

button{   margin-top: 18px; }  header {   text-align: center;   overflow: hidden;   background: #222; }  header a, header label {   display: block;   padding: 20px;   color: #fff;   text-decoration: none;   line-height: 20px; }  header a:hover, header label:hover { color: #aaa; }  header label {   float: right;   padding: 18px 20px;   cursor: pointer; }  header label:after {   content: "\2261";   font-size: 1.8em; }  .logo {   float: left;   font-weight: bold;   font-size: 1.5em; }  nav {   float: right;   max-height: 0;   width: 100%;   -webkit-transition: max-height 0.3s;       -moz-transition: max-height 0.3s;        -o-transition: max-height 0.3s;           transition: max-height 0.3s; }  nav ul {   margin: 0;   padding: 0;    padding-bottom: 10px; }  nav li {   display: block;   text-align: center; }  nav {   padding: 10px;   width: 100%; }  #nav { display: none; }  #nav:checked ~ nav {   max-height: 200px; /* can bigger nav height. transition duration works */ }  @media screen , (min-width: 700px) {    header label { display: none; }    nav {     width: auto;     max-height: none;   }    nav ul {     padding: 0;     padding-right: 10px;   }    nav li {     display: inline-block;     text-align: left;   }    header nav {     display: inline-block;     padding: 20px 10px;     width: auto;   }  } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -