joostdelange
joostdelange

Reputation: 123

How to vertically center this span equal to h1

I got it this far:

Jsfiddle

How can I change the css of the span so it's vertically centered to the h1 on the left of it?

Hope it's not to complicated!

Upvotes: 0

Views: 101

Answers (3)

Lauren G
Lauren G

Reputation: 418

Your best bet is using Flex styles.

  1. Remove all styles for '.title i', '.title span', '.title h1'
  2. Edit title as below:

Flex title style:

.title {
    width: 100%;
    margin: 30px auto;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

justify-content: center; - This re-aligns your H1, dash, and span in the middle.

align-items: center; - This gives you that vertical alignment.

Upvotes: 1

fen1x
fen1x

Reputation: 5880

Add this code to .title

display: flex;
justify-content:center;
align-items:center;

Also, i removed margin: 65px auto from h1 so it wouldn't take all the place in flex-container.

/* Titles */
.title {
    width: 100%;
    margin: 30px auto;
    text-align: center;
    display: flex;
    justify-content:center;
    align-items:center;
}
.title i {
    color: var(--grey-500);
}
.title span {
    font-size: var(--caption);
    line-height: 40px;
    color: #9E9E9E;
}
.title h1 {
    color: var(--black);
    font-size: var(--h1);
    text-align: center;
    font-weight: 500;
}
:root {
    --black: #000000;
    --h1: 2.125em;
    --caption: 0.875em;
    --grey-500: #9E9E9E;
}
/* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: local('Material Icons'), local('MaterialIcons-Regular'), url(https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2) format('woff2');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -webkit-font-feature-settings: 'liga';
  -webkit-font-smoothing: antialiased;
}
body {
  font-family: 'Roboto', sans-serif;
  background-color: #fff;
}
<div class="title">
    <h1>Title 1</h1> <i class="material-icons separateTitleType">remove</i> <span>Page</span>
</div>

Upvotes: 0

Gerard
Gerard

Reputation: 15786

Add a wrapper around h1 and span, then set it as a inline-flex. The wrapper will be centered because of the text-align with title.

Fiddle

.title {
  width: 100%;
  margin: 30px auto;
  text-align: center;
}

.wrapper {
  display: inline-flex;
  justify-content: flex-start;
  align-items: center;
}

.title i {
  color: var(--grey-500);
}

.title span {
  font-size: var(--caption);
  line-height: 40px;
  color: #9E9E9E;
}

.title h1 {
  color: var(--black);
  font-size: var(--h1);
  text-align: center;
  font-weight: 500;
  margin: 65px auto;
}
<div class="title">
  <div class="wrapper">
    <h1>Title 1</h1> <i class="material-icons separateTitleType">remove</i> <span>Page</span>
  </div>
</div>

Upvotes: 0

Related Questions