Let's asume you have the following HTML structure to overlay an image with a header:
<div> <p>You could be tempted to position the header absolute:<br> </p> <pre class="brush:php;toolbar:false">.card { position: relative; width: 300px; } .card > * { margin: 0; } .card header { position: absolute; width: 100%; height: 100%; background-color: #fff3; }
...but then you loose layout on the header. Use grid instead:
.card { display: grid; width: 300px; } .card > * { grid-area: 1/1; margin: 0; } .card header { background-color: #fff3; }
Here is a codepen link with the full example.
Happy coding!
The above is the detailed content of Dont use CSS position: absolute to overlay two elements. For more information, please follow other related articles on the PHP Chinese website!