yevg555
yevg555

Reputation: 25

How to align an svg next to text in react

I'm trying to align a Celcius sign SVG next to the temperature in the center of the parent div, but for some reason, I can't do it by using flexbox or block with vertical-align.

I tried searching the forum, youtube, etc.. with no luck.

I am either able to align them on the same line but they have huge margins between them, which "margin: 0" doesn't solve, or I am able to align them in the center but not on the same line. Any help would be appreciated!!

here is the best result I was able to get: enter image description here

HTML:

<div className={classes.App}>
  <div className={classNames(classes.Container, {
    [classes.Dark]: isDark,
    [classes.Light]: !isDark
  })} >
    {apiCity ?
      <div className={classes.Headers}>
        <h1>{apiCity}</h1>
        <p>October 26, 2021</p>
      </div>
      : ''
    }

    <div className={classes.Inner}>
      {!apiCity ? <p>please enter your location: </p> : ''}
      <Box
        component="form"
        sx={{
          '& > :not(style)': { m: 1, width: '25ch' },
        }}
        noValidate
        autoComplete="off"
        onChange={handleChange}
        onSubmit={handleSubmit}

      >
        <TextField id="outlined-basic" label="Location" variant="outlined" value={newCity} />
      </Box>
      {temp ?
        <Celcius temp={temp} classNameSpan={classes.Celcius} classNameSVG={classes.SVG} />
        : null}
    </div>
    <div className={classes.BottomMenu}></div>
  </div >
</div >

CSS:

    const styles = {
  App: {
    paddingTop: '2rem',
    display: 'flex',
    justifyContent: 'center',
  },
  Container: {
    height: '90vh',
    width: '80%',
    textAlign: 'center',
    borderRadius: '10px',
    color: '#FFFFFF',
    boxShadow: '0px 4px 4px rgba(0, 0, 0, 0.25)',
    borderRadius: '10px 10px 0px 0px',
    "& p": {
      filter: 'drop-shadow(0px 4px 4px rgba(0, 0, 0, 0.25))',
    }
  },
  Headers: {
    filter: 'drop-shadow(0px 4px 4px rgba(0, 0, 0, 0.25))',
    padding: 0,

    "& h1": {
      fontSize: '30px',
      fontStyle: 'normal',
      fontWeight: 500,
      marginTop: '2rem',
      marginBottom: '0px',
    },
    "& p": {
      fontSize: '15px',
      fontStyle: 'normal',
      fontWeight: 300,
      marginTop: '0px',
      marginBotom: '30px',
    }
  },
  Span: {
    display: 'flex',
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'center',
    alignContent: 'center',
    alignItems: 'center',

    "&p": {
      display: 'flex',
      alignSelf: 'baseline',
      margin: 0
    },
  },
  SVG: {
    display: 'flex',
    transform: 'scale(0.1)',
    alignSelf: 'baseline',
    margin: 0
  }

SVG:

    const Celcius = ({ classNameSpan, classNameSVG, temp }) => {

    return (
        <span className={classNameSpan}>
            <p>{temp}</p>
            <svg className={classNameSVG} width="191" height="273" viewBox="0 0 191 273" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M166.8 89C162.8 83.8 157.733 79.8 151.6 77C145.467 74.2 139.133 72.8 132.6 72.8C124.6 72.8 117.333 74.3333 110.8 77.4C104.4 80.3333 98.8667 84.4 94.2 89.6C89.6667 94.8 86.1333 100.933 83.6 108C81.0667 114.933 79.8 122.4 79.8 130.4C79.8 137.867 81 144.933 83.4 151.6C85.8 158.267 89.2667 164.133 93.8 169.2C98.3333 174.267 103.867 178.267 110.4 181.2C116.933 184.133 124.333 185.6 132.6 185.6C140.733 185.6 147.867 183.933 154 180.6C160.133 177.267 165.333 172.6 169.6 166.6L185.8 178.8C184.733 180.267 182.867 182.4 180.2 185.2C177.533 187.867 174 190.6 169.6 193.4C165.2 196.067 159.867 198.4 153.6 200.4C147.467 202.533 140.333 203.6 132.2 203.6C121 203.6 110.867 201.467 101.8 197.2C92.8667 192.933 85.2 187.333 78.8 180.4C72.5333 173.467 67.7333 165.667 64.4 157C61.0667 148.2 59.4 139.333 59.4 130.4C59.4 119.467 61.2 109.4 64.8 100.2C68.4 90.8667 73.4 82.8667 79.8 76.2C86.3333 69.4 94.1333 64.1333 103.2 60.4C112.267 56.6667 122.267 54.8 133.2 54.8C142.533 54.8 151.667 56.6 160.6 60.2C169.667 63.8 177.067 69.3333 182.8 76.8L166.8 89Z" fill="white" />
                <path d="M24.72 96.02C17.22 96.02 11.58 89.96 11.58 82.28C11.58 74.6 17.22 68.54 24.72 68.54C32.22 68.54 37.86 74.6 37.86 82.28C37.86 89.96 32.22 96.02 24.72 96.02ZM24.72 104.66C37.62 104.66 47.4 94.94 47.4 82.28C47.4 69.62 37.62 59.9 24.72 59.9C11.82 59.9 2.04 69.62 2.04 82.28C2.04 94.94 11.82 104.66 24.72 104.66Z" fill="white" />
            </svg>
        </span>
    )
}

Upvotes: 1

Views: 3830

Answers (2)

herrstrietzel
herrstrietzel

Reputation: 17334

Pretty sure you're applyling too many display:flex css properties in your components – display:inline-block might be the better approach.

SVG: {
 display: 'flex',
 transform: 'scale(0.1)',
 alignSelf: 'baseline',
 margin: 0
}

should rather be something like this:

SVG{
 display:inline-block;
 height:1em;
 line-height:1em;
 /* just for vertical alignment as svg don't have descenders like fonts */
 vertical-align:-0.2em;
 }

Flex is supposed to be applied to parent elements to distribute child elements within the parent's layout concept. When you apply it to every child node you'll actually forcing visual line breaks (unless you use display: inline-flex ).

.layout{
    width: 50%;
    margin: 0 auto;
    border: 1px solid #ccc;
}

.svg-inline{
    height:1em;
}

.svg-inline svg{
    display:inline-block;
    height:1em;
    line-height:1em;
    /* just for vertical alignment */
    vertical-align:-0.2em;
}

.p-celsius{
    text-align:center;
}
<div class="layout">
    <p class="p-celsius">
    27.87
        <span class="svg-inline">
            <svg class="classNameSVG" viewBox="0 0 191 273" xmlns="http://www.w3.org/2000/svg">
                <path d="M166.8 89C162.8 83.8 157.733 79.8 151.6 77C145.467 74.2 139.133 72.8 132.6 72.8C124.6 72.8 117.333 74.3333 110.8 77.4C104.4 80.3333 98.8667 84.4 94.2 89.6C89.6667 94.8 86.1333 100.933 83.6 108C81.0667 114.933 79.8 122.4 79.8 130.4C79.8 137.867 81 144.933 83.4 151.6C85.8 158.267 89.2667 164.133 93.8 169.2C98.3333 174.267 103.867 178.267 110.4 181.2C116.933 184.133 124.333 185.6 132.6 185.6C140.733 185.6 147.867 183.933 154 180.6C160.133 177.267 165.333 172.6 169.6 166.6L185.8 178.8C184.733 180.267 182.867 182.4 180.2 185.2C177.533 187.867 174 190.6 169.6 193.4C165.2 196.067 159.867 198.4 153.6 200.4C147.467 202.533 140.333 203.6 132.2 203.6C121 203.6 110.867 201.467 101.8 197.2C92.8667 192.933 85.2 187.333 78.8 180.4C72.5333 173.467 67.7333 165.667 64.4 157C61.0667 148.2 59.4 139.333 59.4 130.4C59.4 119.467 61.2 109.4 64.8 100.2C68.4 90.8667 73.4 82.8667 79.8 76.2C86.3333 69.4 94.1333 64.1333 103.2 60.4C112.267 56.6667 122.267 54.8 133.2 54.8C142.533 54.8 151.667 56.6 160.6 60.2C169.667 63.8 177.067 69.3333 182.8 76.8L166.8 89Z" fill="#000" />
                <path d="M24.72 96.02C17.22 96.02 11.58 89.96 11.58 82.28C11.58 74.6 17.22 68.54 24.72 68.54C32.22 68.54 37.86 74.6 37.86 82.28C37.86 89.96 32.22 96.02 24.72 96.02ZM24.72 104.66C37.62 104.66 47.4 94.94 47.4 82.28C47.4 69.62 37.62 59.9 24.72 59.9C11.82 59.9 2.04 69.62 2.04 82.28C2.04 94.94 11.82 104.66 24.72 104.66Z" fill="#000" />
            </svg>
        </span>
    </p>
    
    <p class="p-celsius">
        27.87 &deg;C
    </p>
</div>

  1. SVG: Delete the width and height attribute in your svg but keep the viewBox attribute recommended whenever you want to inline svg – but keep your viewbox! (often ditched by svg optimizing services defaults)
  2. CSS wrap your svg asset in a span/div with a css class like .svg-inline to provide a relative height

Upvotes: 4

Matt
Matt

Reputation: 931

You can put the svg inside a span, inside a <p> and then space accordingly like so...

  <p style={{textAlign: "center"}}>
    {temp}
    <span>
       <svg style={{marginLeft: "10px"}} className={classNameSVG} width="191" height="273" viewBox="0 0 191 273" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M166.8 89C162.8 83.8 157.733 79.8 151.6 77C145.467 74.2 139.133 72.8 132.6 72.8C124.6 72.8 117.333 74.3333 110.8 77.4C104.4 80.3333 98.8667 84.4 94.2 89.6C89.6667 94.8 86.1333 100.933 83.6 108C81.0667 114.933 79.8 122.4 79.8 130.4C79.8 137.867 81 144.933 83.4 151.6C85.8 158.267 89.2667 164.133 93.8 169.2C98.3333 174.267 103.867 178.267 110.4 181.2C116.933 184.133 124.333 185.6 132.6 185.6C140.733 185.6 147.867 183.933 154 180.6C160.133 177.267 165.333 172.6 169.6 166.6L185.8 178.8C184.733 180.267 182.867 182.4 180.2 185.2C177.533 187.867 174 190.6 169.6 193.4C165.2 196.067 159.867 198.4 153.6 200.4C147.467 202.533 140.333 203.6 132.2 203.6C121 203.6 110.867 201.467 101.8 197.2C92.8667 192.933 85.2 187.333 78.8 180.4C72.5333 173.467 67.7333 165.667 64.4 157C61.0667 148.2 59.4 139.333 59.4 130.4C59.4 119.467 61.2 109.4 64.8 100.2C68.4 90.8667 73.4 82.8667 79.8 76.2C86.3333 69.4 94.1333 64.1333 103.2 60.4C112.267 56.6667 122.267 54.8 133.2 54.8C142.533 54.8 151.667 56.6 160.6 60.2C169.667 63.8 177.067 69.3333 182.8 76.8L166.8 89Z" fill="white" />
            <path d="M24.72 96.02C17.22 96.02 11.58 89.96 11.58 82.28C11.58 74.6 17.22 68.54 24.72 68.54C32.22 68.54 37.86 74.6 37.86 82.28C37.86 89.96 32.22 96.02 24.72 96.02ZM24.72 104.66C37.62 104.66 47.4 94.94 47.4 82.28C47.4 69.62 37.62 59.9 24.72 59.9C11.82 59.9 2.04 69.62 2.04 82.28C2.04 94.94 11.82 104.66 24.72 104.66Z" fill="white" />
        </svg>
    </span>
  </p>

Upvotes: 0

Related Questions