-1

My question is "how to center a span class on button?". See this, arrow-up is behaving in it's own way and not at the center of button. If the code works correctly, An equilateral trinagle will appear in the left of button following with the words Go back to top. And my code has errors which is limiting that to appear in the above mentioned manner. This is running example, https://codepen.io/vkdatta27/pen/XWdvMQL

.arrow-up {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #101010;
}
<button><span class="arrow-up"></span>Go back to top</button>
Ori Drori
  • 145,770
  • 24
  • 170
  • 162

6 Answers6

1

There are different ways to do it, one way to do it is using position: absolute; and adjusting its position, see the code below.

button {
padding-left: 20px;
}
.arrow-up {
  position: absolute;
  left: 15px;
  top: 15px;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent; 
  border-bottom: 5px solid #101010;
 }
<button><span class="arrow-up"></span>Go back to top</button> 
<!-- Arrow up isn't in center like other text -->
Jaocampooo
  • 509
  • 2
  • 10
1

Dont make it so complicated espaically with CSS if there is no reason to it. Simply remove the Span and isnert the unicode of the up-triangle &#9650;.

.arrow-up {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #101010;
}
<button>&#9650; Go back to top</button>
tacoshy
  • 3,056
  • 2
  • 7
  • 21
0

Just make the span display:inline-block:

.arrow-up {
  width: 0;
  height: 0;
  display:inline-block;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #101010;
}
<button><span class="arrow-up"></span>Go back to top</button>

then if you want you can add margin or padding to adjust .

user1601324
  • 263
  • 1
  • 9
0

You can use flexbox to the button container to achieve that result (codepen).

CSS:

.arrow-up {
  width: 0; 
  height: 0; 
  border-left: 5px solid transparent;
  border-right: 5px solid transparent; 
  border-bottom: 5px solid #101010;
 }

.container {
  display: flex;
  align-items: center;
}

HTML:

<button class="container"><span class="arrow-up"></span>Go back to top</button> 
<!-- Arrow up isn't in center like other text -->
Ava_Katushka
  • 145
  • 8
0

There are different ways. This will be one of those ways:

.btn-container {
  display: flex;
}

.arrow-up {
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #101010;
  margin-top: 5px;
}
 <button class="btn-container">
      <span class="arrow-up"></span
      ><span class="textOnBtn"> Go back to top</span>
</button>
Alex Yepes
  • 702
  • 4
  • 15
0

Another possible solution without absolute positioning. Three lines are added to the arrow-up:

.arrow-up {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #101010;

  /* new lines */
  display: inline-block;
  vertical-align: middle;
  margin-right: 5px;
}
<button><span class="arrow-up"></span>Go back to top</button>
cpt.oneeye
  • 492
  • 6
  • 20