Will Marchesi
Will Marchesi

Reputation: 149

How to align an element to the right while staying vertically centered?

I want the 'William Marchesi' and 'Contact' Text centered vertically with the 'Contact' Button on the right of the header. When I try a float: right, the contact text moves so that it is higher than the 'William Marchesi' text.

html {
  height: 100%;
  background-repeat: no-repeat;
  background-color: green;
}

body {
  font-size: 87.5%;
  /* Base font size on 14px */
  font-family: sans-serif;
  line-height: 1.5;
  text-align: justify;
  margin: 0 auto;
  padding: 0;
}

a {
  text-decoration: none;
  color: inherit;
}

.header div {
  margin: 0 auto;
  width: 70%;
  clear: both;
  padding: 0;
}

.header {
  background-color: #FCFCFC;
  width: 100%;
  padding: 0;
  margin: 0 auto;
  /*height: 5em;*/
}

.heading,
.contactButton {
  display: inline-block;
  padding: 0;
  margin: 0;
  vertical-align: middle;
  line-height: 3em;
  height: 3em;
  font-family: 'Nunito', sans-serif;
}

.heading {
  font-size: 3em;
  padding: 0;
  margin: 0;
}

.contactButton {
  font-size: 2em;
  text-align: right;
  padding: 0;
  margin: 0;
  float: right;
}
<body class="body">
  <div class="header">
    <div>
      <h1 class="heading">William Marchesi</h1>
      <a class="contactButton" href="#contact">Contact</a>
    </div>
  </div>
</body>

Upvotes: 1

Views: 47

Answers (4)

icecold_bluesea
icecold_bluesea

Reputation: 46

You can do this using flexbox (display: flex) quite easily.

Try my jsFiddle.

Upvotes: 0

Michael Benjamin
Michael Benjamin

Reputation: 372109

Here's a flexbox solution that makes centering simple and easy:

body {
  background-color: green;
  font-size: 87.5%;
  margin: 0;
  padding: 0;
}

.header {
  display: flex;
  justify-content: space-around; /* horizontal alignment of children */
  align-items: center;           /* vertical alignment of children */
  height: 6rem;
  background-color: #FCFCFC;
}

.heading {
  font-size: 3em;
  font-family: 'Nunito', sans-serif;
}

.contactButton {
  font-size: 2em;
  font-family: 'Nunito', sans-serif;
}

a {
  text-decoration: none;
  color: inherit;
}
<body class="body">
  <div class="header">
    <h1 class="heading">William Marchesi</h1>
    <a class="contactButton" href="#contact">Contact</a>
  </div>
</body>

jsFiddle

More about flex centering here:

Browser support: Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.

Upvotes: 0

D. Cosentino
D. Cosentino

Reputation: 32

You can use margin-left instead of float

html {
    height: 100%;
    background-repeat: no-repeat;
    background-color: green;
}

body {
    font-size: 87.5%; /* Base font size on 14px */
    font-family: sans-serif;
    line-height: 1.5;
    text-align: justify;
    margin: 0 auto;
    padding: 0;
}

a {
    text-decoration: none;
    color: inherit;
}

.header div {
    margin: 0 auto;
    width: 70%;
    clear: both;
    padding: 0;
}
.header {

    background-color: #FCFCFC;
    width:100%;
    padding: 0;
    margin: 0 auto;

    /*height: 5em;*/
}

.heading, .contactButton {
    display: inline-block;
    padding: 0;
    margin: 0;
    vertical-align: middle;
    line-height: 3em;
    height: 3em;
    font-family: 'Nunito', sans-serif;
}
.heading {
    font-size: 3em;
    padding: 0;
    margin: 0;
}

.contactButton {
    font-size: 2em;
    text-align: right;
    padding: 0;
    margin-left: 59%;
}

Upvotes: 0

Julio Feferman
Julio Feferman

Reputation: 2664

Here's one of many possible solutions, using position:absolute on the contact button.

html {
    height: 100%;
    background-repeat: no-repeat;
    background-color: green;
}

body {
    font-size: 87.5%; /* Base font size on 14px */
    font-family: sans-serif;
    line-height: 1.5;
    text-align: justify;
    margin: 0 auto;
    padding: 0;
}

a {
    text-decoration: none;
    color: inherit;
}

.header div {
    margin: 0 auto;
    padding: 0;
    text-align:center;
}
.header {
    background-color: #FCFCFC;
    padding: 0;
}

.heading, .contactButton {
    padding: 0;
    margin: 0;
    vertical-align: middle;
    line-height: 3em;
    font-family: 'Nunito', sans-serif;
}
.heading {
    font-size: 3em;
    padding: 0;
    margin: 0;
}

.contactButton {
    font-size: 2em;
    text-align: right;
    padding: 0;
    margin: 0;
    position:absolute;
    top:0.8em;
    right:1em;
}
<div class="header">
      <div>
          <h1 class="heading">William Marchesi</h1>
          <a class="contactButton" href="#contact">Contact</a>
      </div>
  </div>

Upvotes: 1

Related Questions