Papipone Papipone - 1 year ago 49
CSS Question

Render a div on the right and keep the nav centered

I would like to render a

nav
and a
div
containing several buttons on the same "line" by getting the
nav
centered. However, I can not get a satisfying result. If I put my
div
after the
nav
, the
div
is displayed on the right side but under the
nav
, in part due to the following
CSS
statements:

header {
position: relative;
width: 100%
background: #333;
}

nav {
position: relative;
margin: 0 auto;
}


If I remove the
header
width
attribute and make the
nav
float
to the
left
, then I lose the centered position of my
nav
.

Here is the HTML code:

<div class='header'>
<div class='header-content'>
<div class='welcome-div'>
<h1>
Hello
</h1>
<header>
<nav id='drop-down-menu'>
<div class="button"></div>
<ul>
<li class='active'><a href='#'>HOME</a></li>
<li><a href='#'>ABOUT</a></li>
<li><a href='#'>PRODUCTS</a>
<ul>
<li><a href='#'>Product 1</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
<li><a href='#'>Product 2</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>BIO</a></li>
<li><a href='#'>VIDEO</a></li>
<li><a href='#'>GALLERY</a></li>
<li><a href='#'>CONTACT</a></li>
</ul>
</nav>
<div class='lang'>
<a class='hover-fade'>Français</a>
<a class='hover-fade'>English</a>
<a class='hover-fade'>日本語</a>
</div>
</header>
</div>
</div>
</div>
<div class='clear'/>


The
CSS
one:

* {
margin: 0;
padding: 0;
text-decoration: none;
}

header {
position: relative;
width: 100%
background: #333;
}

nav {
position: relative;
margin: 0 auto;
}

#drop-down-menu,
#drop-down-menu ul,
#drop-down-menu ul li,
#drop-down-menu ul li a,
#drop-down-menu
#head-mobile {
border: 0;
list-style: none;
line-height: 1;
display: block;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
z-index: 1;
}

#drop-down-menu:after,
#drop-down-menu > ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0
}

#drop-down-menu
#head-mobile {
display: none
}

#drop-down-menu {
font-family: sans-serif;
background: #333
}

#drop-down-menu > ul > li {
display:inline-block;
position: relative;
}

#drop-down-menu > ul {
text-align: center;
}

#drop-down-menu > ul > li > a {
padding: 17px;
font-size: 12px;
letter-spacing: 1px;
text-decoration: none;
color: #ddd;
font-weight: 700;
}

#drop-down-menu > ul > li:hover > a,
#drop-down-menu ul li.active a {
color: #fff
}

#drop-down-menu > ul > li:hover,
#drop-down-menu ul li.active:hover,
#drop-down-menu ul li.active,
#drop-down-menu ul li.has-sub.active:hover {
background: #448D00 !important;
-webkit-transition: background .3s ease;
-ms-transition: background .3s ease;
transition: background .3s ease;
}

#drop-down-menu > ul > li.has-sub > a {
padding-right: 30px
}

#drop-down-menu > ul > li.has-sub > a:after {
position: absolute;
top: 22px;
right: 11px;
width: 8px;
height: 2px;
display: block;
background: #ddd;
content: ''
}

#drop-down-menu > ul > li.has-sub > a:before {
position: absolute;
top: 19px;
right: 14px;
display: block;
width: 2px;
height: 8px;
background: #ddd;
content: '';
-webkit-transition: all .25s ease;
-ms-transition: all .25s ease;
transition: all .25s ease
}

#drop-down-menu > ul > li.has-sub:hover > a:before {
top: 23px;
height: 0
}

#drop-down-menu ul ul {
position: absolute;
left: -9999px
}

#drop-down-menu ul ul li {
height: 0;
-webkit-transition: all .25s ease;
-ms-transition: all .25s ease;
background: #333;
transition: all .25s ease
}

#drop-down-menu ul ul li:hover {

}

#drop-down-menu li:hover > ul {
left: auto
}

#drop-down-menu li:hover > ul > li {
height: 35px
}

#drop-down-menu ul ul ul {
margin-left: 100%;
top: 0
}

#drop-down-menu ul ul li a {
border-bottom: 1px solid rgba(150,150,150,0.15);
padding: 11px 15px;
width: 170px;
font-size: 12px;
text-decoration: none;
color: #ddd;
font-weight: 400;
}

#drop-down-menu ul ul li:last-child > a,
#drop-down-menu ul ul li.last-item > a {
border-bottom: 0
}

#drop-down-menu ul ul li:hover > a,
#drop-down-menu ul ul li a:hover {
color: #fff
}

#drop-down-menu ul ul li.has-sub > a:after {
position: absolute;
top: 16px;
right: 11px;
width: 8px;
height: 2px;
display: block;
background: #ddd;
content: ''
}

#drop-down-menu ul ul li.has-sub > a:before {
position: absolute;
top: 13px;
right: 14px;
display: block;
width: 2px;
height: 8px;
background: #ddd;
content: '';
-webkit-transition: all .25s ease;
-ms-transition: all .25s ease;
transition: all .25s ease
}

#drop-down-menu ul ul > li.has-sub:hover > a:before {
top: 17px;
height: 0
}

#drop-down-menu ul ul li.has-sub:hover,
#drop-down-menu ul li.has-sub ul li.has-sub ul li:hover {
background: #363636;
}

#drop-down-menu ul ul ul li.active a {
border-left: 1px solid #333
}

#drop-down-menu > ul > li.has-sub > ul > li.active > a,
#drop-down-menu > ul ul > li.has-sub > ul > li.active > a {
border-top: 1px solid #333
}

.hover-fade:hover, .hover-fade:focus, .hover-fade:active {
background-color: #2098D1;
color: white;
}

.hover-fade {
display: inline-block;
vertical-align: middle;
font-family: 'Roboto', sans-serif;
-webkit-transform: perspective(1px) translateZ(0);
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px transparent;
overflow: hidden;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: color, background-color;
transition-property: color, background-color;
margin: .1em;
padding: .5em;
cursor: pointer;
background: #e1e1e1;
background-color: rgb(225, 225, 225);
text-decoration: none;
color: #666;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

body {
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
width: 100%;
margin: 0;
padding: 0;
min-width: 1200px;
background-color: #111;
}

.header {
float: left;
min-width: 100%;
background-color: #333;
}

.header-content {
color: #ffffff;
}

.welcome-div {
text-align: center;
}

.welcome-div h1 {
padding: 1.5em;
vertical-align: middle;
font-family: 'Helvetica Neue', sans-serif;
font-size: 46px;
font-weight: 500;
line-height: 10px;
letter-spacing: 1px;
}

.lang {
float: right;
padding: 0.5em;
}

.content {
padding: 2em 0 2em 0;
margin-left: auto;
margin-right: auto;
width: 60%;
}

.box {
position: relative;
width: 80%;
margin: 0 auto;
color: #ffffff;
}

.box h1 {
padding: 0.5em 0 0.5em 0;
font-family: 'Graduate', sans-serif;
font-size: 36px;
font-weight: 600;
letter-spacing: 1px;
text-align: center;
background-color: #448D00;
}

.clear {
clear: both;
}


And a JSFiddle sample.

I would like to keep the same layout, but with the 3 buttons aligned with the
nav
.

Thanks for your answers.

Answer Source

Absolutely position your lang block in top right of header:

* {
    margin: 0;
    padding: 0;
    text-decoration: none;
}

header {
    position: relative;
    width: 100%
    background: #333;
}

nav {
    position: relative;
    margin: 0 auto;
}

#drop-down-menu, 
#drop-down-menu ul, 
#drop-down-menu ul li, 
#drop-down-menu ul li a, 
#drop-down-menu 
#head-mobile {
    border: 0;
    list-style: none;
    line-height: 1;
    display: block;
    position: relative;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    z-index: 1;
}

#drop-down-menu:after, 
#drop-down-menu > ul:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0
}

#drop-down-menu 
#head-mobile {
    display: none
}

#drop-down-menu {
    font-family: sans-serif;
    background: #333
}

#drop-down-menu > ul > li {
    display:inline-block;
    position: relative;
}

#drop-down-menu > ul {
    text-align: center;
}

#drop-down-menu > ul > li > a {
    padding: 17px;
    font-size: 12px;
    letter-spacing: 1px;
    text-decoration: none;
    color: #ddd;
    font-weight: 700;
}

#drop-down-menu > ul > li:hover > a, 
#drop-down-menu ul li.active a {
    color: #fff
}

#drop-down-menu > ul > li:hover, 
#drop-down-menu ul li.active:hover, 
#drop-down-menu ul li.active, 
#drop-down-menu ul li.has-sub.active:hover {
    background: #448D00 !important;
    -webkit-transition: background .3s ease;
    -ms-transition: background .3s ease;
    transition: background .3s ease;
}

#drop-down-menu > ul > li.has-sub > a {
    padding-right: 30px
}

#drop-down-menu > ul > li.has-sub > a:after {
    position: absolute;
    top: 22px;
    right: 11px;
    width: 8px;
    height: 2px;
    display: block;
    background: #ddd;
    content: ''
}

#drop-down-menu > ul > li.has-sub > a:before {
    position: absolute;
    top: 19px;
    right: 14px;
    display: block;
    width: 2px;
    height: 8px;
    background: #ddd;
    content: '';
    -webkit-transition: all .25s ease;
    -ms-transition: all .25s ease;
    transition: all .25s ease
}

#drop-down-menu > ul > li.has-sub:hover > a:before {
    top: 23px;
    height: 0
}

#drop-down-menu ul ul {
    position: absolute;
    left: -9999px
}

#drop-down-menu ul ul li {
    height: 0;
    -webkit-transition: all .25s ease;
    -ms-transition: all .25s ease;
    background: #333;
    transition: all .25s ease
}

#drop-down-menu ul ul li:hover {

}

#drop-down-menu li:hover > ul {
    left: auto
}

#drop-down-menu li:hover > ul > li {
    height: 35px
}

#drop-down-menu ul ul ul {
    margin-left: 100%;
    top: 0
}

#drop-down-menu ul ul li a {
    border-bottom: 1px solid rgba(150,150,150,0.15);
    padding: 11px 15px;
    width: 170px;
    font-size: 12px;
    text-decoration: none;
    color: #ddd;
    font-weight: 400;
}

#drop-down-menu ul ul li:last-child > a, 
#drop-down-menu ul ul li.last-item > a {
    border-bottom: 0
}

#drop-down-menu ul ul li:hover > a, 
#drop-down-menu ul ul li a:hover {
    color: #fff
}

#drop-down-menu ul ul li.has-sub > a:after {
    position: absolute;
    top: 16px;
    right: 11px;
    width: 8px;
    height: 2px;
    display: block;
    background: #ddd;
    content: ''
}

#drop-down-menu ul ul li.has-sub > a:before {
    position: absolute;
    top: 13px;
    right: 14px;
    display: block;
    width: 2px;
    height: 8px;
    background: #ddd;
    content: '';
    -webkit-transition: all .25s ease;
    -ms-transition: all .25s ease;
    transition: all .25s ease
}

#drop-down-menu ul ul > li.has-sub:hover > a:before {
    top: 17px;
    height: 0
}

#drop-down-menu ul ul li.has-sub:hover, 
#drop-down-menu ul li.has-sub ul li.has-sub ul li:hover {
    background: #363636;
}

#drop-down-menu ul ul ul li.active a {
    border-left: 1px solid #333
}

#drop-down-menu > ul > li.has-sub > ul > li.active > a, 
#drop-down-menu > ul ul > li.has-sub > ul > li.active > a {
    border-top: 1px solid #333
}

.hover-fade:hover, .hover-fade:focus, .hover-fade:active {
    background-color: #2098D1;
    color: white;
}

.hover-fade {
    display: inline-block;
    vertical-align: middle;
    font-family: 'Roboto', sans-serif;
    -webkit-transform: perspective(1px) translateZ(0);
    transform: perspective(1px) translateZ(0);
    box-shadow: 0 0 1px transparent;
    overflow: hidden;
    -webkit-transition-duration: 0.3s;
    transition-duration: 0.3s;
    -webkit-transition-property: color, background-color;
    transition-property: color, background-color;
    margin: .1em;
    padding: .5em;
    cursor: pointer;
    background: #e1e1e1;
    background-color: rgb(225, 225, 225);
    text-decoration: none;
    color: #666;
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
    width: 100%;
    margin: 0;
    padding: 0;
    min-width: 1200px;
    background-color: #111;
}

.header {
    float: left;
    min-width: 100%;
    background-color: #333;
}

.header-content {
    color: #ffffff;
}

.welcome-div {
    text-align: center;
}

.welcome-div h1 {
    padding: 1.5em;
    vertical-align: middle;
    font-family: 'Helvetica Neue', sans-serif;
    font-size: 46px;
    font-weight: 500;
    line-height: 10px;
    letter-spacing: 1px;
}

.content {
    padding: 2em 0 2em 0;
    margin-left: auto;
    margin-right: auto;
    width: 60%;
}

.box {
    position: relative;
    width: 80%;
    margin: 0 auto;
    color: #ffffff;
}

.box h1 {
    padding: 0.5em 0 0.5em 0;
    font-family: 'Graduate', sans-serif;
    font-size: 36px;
    font-weight: 600;
    letter-spacing: 1px;
    text-align: center;
    background-color: #448D00;  
}

.clear {
    clear: both;
}

.lang {
    padding: 0.5em;
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1000;
}
<div class='header'>
  <div class='header-content'>
    <div class='welcome-div'>
      <h1>
        Hello
      </h1>
      <header>
        <nav id='drop-down-menu'>
          <div class="button"></div>
          <ul>
            <li class='active'><a href='#'>HOME</a></li>
            <li><a href='#'>ABOUT</a></li>
            <li><a href='#'>PRODUCTS</a>
            <ul>
              <li><a href='#'>Product 1</a>
                <ul>
                  <li><a href='#'>Sub Product</a></li>
                  <li><a href='#'>Sub Product</a></li>
                </ul>
              </li>
              <li><a href='#'>Product 2</a>
                <ul>
                  <li><a href='#'>Sub Product</a></li>
                  <li><a href='#'>Sub Product</a></li>
                </ul>
              </li>
            </ul>
          </li>
          <li><a href='#'>BIO</a></li>
          <li><a href='#'>VIDEO</a></li>
          <li><a href='#'>GALLERY</a></li>
          <li><a href='#'>CONTACT</a></li>
        </ul>
      </nav>
      <div class='lang'>
        <a class='hover-fade'>Français</a> 
        <a class='hover-fade'>English</a> 
        <a class='hover-fade'>日本語</a> 
      </div>
    </header>
  </div>
</div>
</div>
<div class='clear'/>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download