1

I've tried to put a minimal solution together for this. I want to be able to center a span that has a background image (svg) within a div that has the border radius set to give a rounded icon effect.

This looks like the following:

current attempt

This has been put together with the following:

body {
  background-color: greenyellow;
}

.demo-icon {
  display: inline-block;
  border-radius: 50%;
  background-color: white;
  position: relative;
  overflow: hidden;
  height: 64px;
  width: 64px;
}

.demo-icon .icon {
  text-align: center;
  vertical-align: middle;
}

.icon {
  display: inline-block;
  background-size: cover;
  background-repeat: no-repeat;
  height: 48px;
  width: 48px;
  background-color: red;
  vertical-align: middle;
}

.icon-form {
  background-image: url(./form.svg);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Align you £!£!££</title>
</head>

<body>
  <div class="demo-icon">
    <span class="icon icon-form"></span>
  </div>
</body>

</html>

What do I need to do to get the span containing the svg to align in the center middle?

Makyen
  • 31,849
  • 12
  • 86
  • 121
andrewthedev
  • 167
  • 1
  • 8

2 Answers2

3

You can try with display:inline-flex;

body {
  background-color: greenyellow;
}

.demo-icon {
  /* added */
  display: inline-flex;
  justify-content: center;
  align-items: center;
  /****/
  border-radius: 50%;
  background-color: white;
  position: relative;
  overflow: hidden;
  height: 64px;
  width: 64px;
}

.demo-icon .icon {
  text-align: center;
  vertical-align: middle;
}

.icon {
  display: inline-block;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  height: 48px;
  width: 48px;
  background-color: red;
  vertical-align: middle;
}

.icon-form {
  background-image: url(./form.svg);
}
<div class="demo-icon">
  <span class="icon icon-form"></span>
</div>
doğukan
  • 23,073
  • 13
  • 57
  • 69
3

you can try flex box because it mostly used in layouts of element and make it easy

body {
  background-color: greenyellow;
}

.demo-icon {
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  background-color: white;
  position: relative;
  overflow: hidden;
  height: 64px;
  width: 64px;
}

.demo-icon .icon {
  text-align: center;
  vertical-align: middle;
}

.icon {
  display: inline-block;
  background-size: cover;
  background-repeat: no-repeat;
  height: 48px;
  width: 48px;
  background-color: red;
  vertical-align: middle;
}

.icon-form {
  background-image: url(./form.svg);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Align you £!£!££</title>
</head>

<body>
  <div class="demo-icon">
    <span class="icon icon-form"></span>
  </div>
</body>

</html>
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39