I cannot place two <div>
s next to each other because they overlap. I want to have two divs with a separator in the middle of them.
html, body {
max-width: 100%;
overflow-x: hidden;
margin: 0;
background-color: #333;
color: white;
}
#games {
position: absolute;
}
@keyframes rotate {
100% {
transform: rotate(1turn);
}
}
.game-card, .game-hub-card, .game-card::before, .game-hub-card::before, .game-card::after, .game-hub-card::after {
box-sizing: border-box;
}
.game-card, .game-hub-card {
z-index: 0;
position: absolute;
width: 300px;
height: 200px;
border-radius: 10px;
overflow: hidden;
padding: 2rem;
background-color: #000;
background-repeat: no-repeat;
background-position: 0 0;
}
.game-card:hover, .game-hub-card:hover {
&::before {
position: absolute;
content: '';
z-index: -2;
left: -50%;
top: -50%;
width: 200px;
height: 200px;
background-color: #000;
background-repeat: no-repeat;
background-position: 0 0;
background-image: conic-gradient(transparent, purple, transparent 30%);
animation: rotate 4s linear infinite;
}
&::after {
position: absolute;
content: '';
z-index: -1;
left: 6px;
top: 6px;
width: calc(100% - 12px);
height: calc(100% - 12px);
background: #000;
border-radius: 5px;
}
}
.game-list {
padding-right: 5px;
border-right: 1px solid black;
}
.game-hub-list {
}
.game-list, .game-hub-list {
display: inline-block;
}
<div class="page" id="games">
<div class="game-list">
<h3>Games</h3>
<div class="game-card">
<img class="game-card-img" src="" alt="Game Icon"></img>
<h4 class="game-card-title">Coming soon</h4>
<p class="game-card-description">Lorem Ipsum</p>
</div>
</div>
<div class="game-type-seperator">
</div>
<div class="game-hub-list">
<h3>Game Hubs</h3>
<div class="game-hub-card">
<img class="game-hub-card-img" src="" alt="Game Hub Icon"></img>
<h4 class="game-hub-card-title">Coming soon</h4>
<p class="game-hub-card-description">Lorem Ipsum</p>
</div>
</div>
</div>
I tried display: inline-block
but that is the issue, and the div’s positions HAVE to be absolute.
2
Answers
I mean this is just an awkward way of doing this, but if they really need to be positioned absolute then you need to either know and set the surrounding div’s width and calculate the width of the children + position them and/or just have the outer div full width and play with the width, top and left/right values of the children.
Ps. padding counts for the width of the element hence why the
game-list
andgame-hub-list
has the width 236px.Hope this helps you in the right path!
If an element is
position: absolute
, it will position itself to the closest positioned containing element which is#games
. Since they both share the same container they are stacked on top of each other. Each card should haveposition: relative
so that theposition: absolute
pseudo-elements::before
and::after
stay within the.card
andcard
itself references it’s original position instead of#games
. In the example below, the classNames have been consolidated into simpler forms and details are commented within the source. Also, the OP styles are SCSS and a compiler is needed to convert it into CSS. The example below is simple CSS.