0

so I'm trying to vertically align a and element within grid cells. Was wondering if there is any way to do this without using padding, as I was the elements to be centered regardless of the size of the grid cell

I've tried using "vertical-align: middle" but that doesn't seem to do anything

HTML:

<!DOCTYPE html>
<html>
  <head>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div class = "grid">
      <label class = "grid_item">
        <input type = "checkbox" name = "check">
        <span>text</span>
      </label>
      <label class = "grid_item">
        <input type = "checkbox" name = "check">
        <span>text</span>
      </label>
    </div>
  </body>
</html>

CSS:

.grid {
  display: grid;
  border-top: 1px black solid;
}

.grid_item {
  border: 1px black solid;
  border-top: none;
  height: 50px;
}

.grid_item span, .grid_item input {
  position: relative;
  vertical-align: middle;
}

I expect this to center the elements vertically but it seems to have no effect

Rohit Suthar
  • 967
  • 9
  • 22
Peter Jordanson
  • 61
  • 2
  • 16

4 Answers4

2

try using the display flex property to align items

.grid_item {
border: 1px black solid;
border-top: none;
height: 50px;
display: flex;
align-items: center;

}

Rajesh
  • 254
  • 1
  • 6
1

For that you can use line-height:50px It will center the text with relative to your parent height. Used 50px beacause height is 50px;

.grid {
  display: grid;
  border-top: 1px black solid;
}

.grid_item {
  border: 1px black solid;
  border-top: none;
  height: 50px;
  line-height:50px
}

.grid_item span, .grid_item input {
  position: relative;
  vertical-align: middle;
}
<!DOCTYPE html>
<html>
  <head>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div class = "grid">
      <label class = "grid_item">
        <input type = "checkbox" name = "check">
        <span>text</span>
      </label>
      <label class = "grid_item">
        <input type = "checkbox" name = "check">
        <span>text</span>
      </label>
    </div>
  </body>
</html>
Sagar Kodte
  • 3,723
  • 4
  • 23
  • 52
1

.grid {
  display: grid;
  border-top: 1px black solid;
}

.grid_item {
  border: 1px black solid;
  border-top: none;
  height: 50px;
  display:flex;
  align-items:center;
}

.grid_item span, .grid_item input {
  position: relative;
  vertical-align: middle;
}
<div class = "grid">
      <label class = "grid_item">
        <input type = "checkbox" name = "check">
        <span>text</span>
      </label>
      <label class = "grid_item">
        <input type = "checkbox" name = "check">
        <span>text</span>
      </label>
    </div>

you can use display:flex and align-items:center on grid_item to make it center hope it helps

Piyush Verma
  • 285
  • 1
  • 8
0

You could set display: flex; on the .grid_item parent, and set margin top and bottom to auto on the .grid_item input and .grid_item span :

.grid_item {
  border: 1px black solid;
  border-top: none;
  height: 50px;
  display: flex;
}

.grid_item input, .grid_item span {
  margin-top:auto;
  margin-bottom:auto;
}
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26