I'm trying to create a simple tic-tac-toe application using HTML, CSS, Javascript.
I've set the X and O images in HTML like so:
<body>
<div class="wrapper">
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
<div>
<img src="X.png" alt="X" id="X">
<img src="O.png" alt="O" id="O">
</div>
</div>
</body>
</html>
I'm using CSS grid to arrange each div in my HTML as a cell (inside the X and O will show up).
<style>
.wrapper{
display:grid;
grid-template-columns:100px 100px 100px;
grid-template-rows:100px 100px 100px;
grid-gap:10px;
}
.wrapper > div{
background:#eee;
padding:1em;
}
#X{
height: 50%;
width: 50%;
}
#O{
height: 50%;
width: 50%;
}
</style>
The resulting web page looked like this:
Now as you can see, the X and O aren't centered inside each of their respective boxes. My intention is to make X and O overlap on top of each other (so that I can use javascript to enable and disable their visibility later on). I initially thought I could solve this problem via margin or padding. So I set the left-padding for the X images like this:
#X{
height: 50%;
width: 50%;
padding-left: 16px
}
This successfully centered the X image in each box on the horizontal axis.
However, when I tried to add padding-top to X (to centre it vertically), X went back to its initial position (as seen in the first image).
So how would I centre the X (and eventually, the O) in their respective boxes?