methuselah methuselah - 6 months ago 8
HTML Question

Centre one element and one to the right

I am trying to get my div elements lined up as follows:

enter image description here

But currently it appears like this:

enter image description here

How would I resolve this issue, I'd like the

Member since June 2015
text to be centred and the icon to be positioned to the right.

I've mocked up a jsfiddle below which shows this.

HTML

<div class="profile-header">
<div class="profile-rating">
<i class="icon ion-star energized" ng-repeat="i in [1, 2, 3, 4]"></i><i class="icon ion-star dark" ng-repeat="i in [1]"></i>
</div>
<div class="ee">
<div class="profile-join-date">Member since June 2015</div>
<div class="ddd">&Theta;</div>
</div>
</div>


CSS

.profile-header {
padding-top: 2em;
padding-bottom: 1.75em;
background-color: #fff;
}

.profile-name {
padding-top: 0.75em;
padding-bottom: 0.75em;
font-size: 1.75em;
text-align: center;
font-weight: bold;
}

.profile-rating {
text-align: center;
font-size: 1.5em;
padding-bottom: 0.75em;
}

.profile-join-date {
text-align: center;
display: inline;
width: 50%;
margin: 0 auto;
}

.ddd {
float: right;
display: inline;
font-size: 2em;
padding-right: 0.5em;
}

.profile-join-date {
width: 50%;
margin: 0 auto;
}

.ee {
width: 100%;
}


Fiddle: https://jsfiddle.net/8ybz6z13/

Answer

I've made few changes to the fiddle - my fork can be found at https://jsfiddle.net/z7vhq158/

I made it center the text exactly.

HTML (no changes):

<div class="profile-header">
  <div class="profile-rating">
    <i class="icon ion-star energized" ng-repeat="i in [1, 2, 3, 4]"></i><i class="icon ion-star dark" ng-repeat="i in [1]"></i>
  </div>
  <div class="ee">
    <div class="profile-join-date">Member since June 2015</div>
    <div class="ddd">&Theta;</div>
  </div>
</div>

CSS:

Changes:

  • changed .profile-join-date's display to inline-block and set its width to 100%
  • made pseudo-overlay from the button by setting .ddd's position to absolute with right: 0; top: 0 and .ee's (container's) position to relative

Full code:

.profile-header {
  padding-top: 2em;
  padding-bottom: 1.75em;
  background-color: #fff;
}

.profile-name {
  padding-top: 0.75em;
  padding-bottom: 0.75em;
  font-size: 1.75em;
  text-align: center;
  font-weight: bold;
}

.profile-rating {
  text-align: center;
  font-size: 1.5em;
  padding-bottom: 0.75em;
}

.profile-join-date {
  text-align: center;
  display: inline-block;
  width: 100%;
  margin: 0 auto;
}

.ddd {
  float: right;
  display: inline-block;
  font-size: 2em;
  padding-right: 0.5em;
  position: absolute;
  right: 0;
  top: 0;
}

.ee {
  width: 100%;
  position: relative;
}

Please be aware it's not going to display correctly on smaller screens. IMO it's better to sacrifice exact text centering and make it's grid-like (see comments and other answers or make the button child of text element or something like this)

Comments