SVG inside span isn't on the same line as the text

Viewed 8297

I have an SVG file inside a span with text at the same time. The text and the SVG are the same height. However, the SVG isn't on the same line as the text.

Relevant jsfiddle: https://jsfiddle.net/tcrnjd53/

As you can see, the facebook logo needs to be on the red dotted line, just like the sample text.

span {
  font-size: 1em;
  border-bottom: 1px dotted red;
  zoom: 3; /* for easier readability */
}

span svg {
  fill: #3b5998;
  height: 1em;
}
<span>Sample Text <svg viewBox="0 0 24 24"><path d="M22.676 0H1.324C.593 0 0 .593 0 1.324v21.352C0 23.408.593 24 1.324 24h11.494v-9.294H9.689v-3.621h3.129V8.41c0-3.099 1.894-4.785 4.659-4.785 1.325 0 2.464.097 2.796.141v3.24h-1.921c-1.5 0-1.792.721-1.792 1.771v2.311h3.584l-.465 3.63H16.56V24h6.115c.733 0 1.325-.592 1.325-1.324V1.324C24 .593 23.408 0 22.676 0"></path></svg></span>

3 Answers

The default vertical-align property is baseline - change it to bottom. See demo below:

span {
  font-size: 1em;
  border-bottom: 1px dotted red;
  zoom: 3; /* for easier readability */
}

span svg {
  fill: #3b5998;
  height: 1em;
  vertical-align: bottom; /* ADDED */
}
<span>Sample Text <svg viewBox="0 0 24 24"><path d="M22.676 0H1.324C.593 0 0 .593 0 1.324v21.352C0 23.408.593 24 1.324 24h11.494v-9.294H9.689v-3.621h3.129V8.41c0-3.099 1.894-4.785 4.659-4.785 1.325 0 2.464.097 2.796.141v3.24h-1.921c-1.5 0-1.792.721-1.792 1.771v2.311h3.584l-.465 3.63H16.56V24h6.115c.733 0 1.325-.592 1.325-1.324V1.324C24 .593 23.408 0 22.676 0"></path></svg></span>

CSS vertical-align property could help as shown in the snippet. Choosing the appropriate value is up to you. To better understand what i mean do try the following values top,text-top,middle,bottom,text-bottom and see the differences. You could apply a fixed or percentage value if is best suited.

span {
  font-size: 1em;
  border-bottom: 1px dotted red;
  zoom: 3; /* for easier readability */
}

span svg {
  fill: #3b5998;
  height: 1em;
  /*
  vertical-align:text-top;
  */
  vertical-align:-0.1875em;
}
<span>Sample Text <svg viewBox="0 0 24 24"><path d="M22.676 0H1.324C.593 0 0 .593 0 1.324v21.352C0 23.408.593 24 1.324 24h11.494v-9.294H9.689v-3.621h3.129V8.41c0-3.099 1.894-4.785 4.659-4.785 1.325 0 2.464.097 2.796.141v3.24h-1.921c-1.5 0-1.792.721-1.792 1.771v2.311h3.584l-.465 3.63H16.56V24h6.115c.733 0 1.325-.592 1.325-1.324V1.324C24 .593 23.408 0 22.676 0"></path></svg></span>

The previous solutions don't work when the line-height is different than the font-size. In order to handle that cases properly, use text-bottom instead of bottom.

span {
  font-size: 20px;
  line-height: 30px;
  border-bottom: 1px dotted red;
  zoom: 3; /* for easier readability */
}

span svg {
  fill: #3b5998;
  height: 1em;
  vertical-align: text-bottom;
}
<span>Sample Text <svg viewBox="0 0 24 24"><path d="M22.676 0H1.324C.593 0 0 .593 0 1.324v21.352C0 23.408.593 24 1.324 24h11.494v-9.294H9.689v-3.621h3.129V8.41c0-3.099 1.894-4.785 4.659-4.785 1.325 0 2.464.097 2.796.141v3.24h-1.921c-1.5 0-1.792.721-1.792 1.771v2.311h3.584l-.465 3.63H16.56V24h6.115c.733 0 1.325-.592 1.325-1.324V1.324C24 .593 23.408 0 22.676 0"></path></svg></span>

Related