Я заменим 1920px
на меньшее значение, чтобы мы могли лучше увидеть результат
Легким решением было бы добавить position:relative
и настроить z-index
, как показано ниже:
.background-img-wrapper:before {
content: "";
top: 0;
left: 0;
position: absolute;
height: 100%;
width: 100%;
background: linear-gradient(to right, rgba(252, 252, 252, 1) 0%, rgba(255, 255, 255, 0) 20%, rgba(255, 255, 255, 0) 80%, rgba(252, 252, 252, 1) 100%);
}
.background-img-wrapper {
max-width: 400px;
position:relative; /* added */
}
.background:before {
content: "";
top: 0;
left: 0;
position: absolute;
z-index:1; /* added */
height: 100%;
width: 100%;
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0) 80%, rgba(252, 252, 252, 1) 100%);
}
.background {
width: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" >
<div class="row justify-content-center position-fixed background">
<div class="background-img-wrapper">
<img class="img-fluid" src="https://via.placeholder.com/400x300/000000/"/>
</div>
</div>
Или упростите свой код, как показано ниже:
.background:before {
content: "";
top: 0;
left: 0;
position: absolute;
height: 100%;
width: 100%;
background:
linear-gradient(to bottom, transparent 80%, #fff 100%),
linear-gradient(to right, #fff 0%, transparent 20% 80%, #fff 100%);
}
.background {
max-width: 400px;
left:0;
right:0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" >
<div class="position-fixed background m-auto">
<img class="img-fluid" src="https://via.placeholder.com/400x300/000000/"/>
</div>
Если соотношение изображений всегда будет одинаковым, вы все равно можете упростить:
.background:before {
content: "";
padding-top:calc(300/400 * 100%); /* Height/width */
}
.background {
max-width: 400px;
left:0;
right:0;
background:
linear-gradient(to bottom, transparent 80%, #fff 100%),
linear-gradient(to right, #fff 0%, transparent 20% 80%, #fff 100%),
url(https://via.placeholder.com/400x300/000000/) center/cover;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" >
<div class="position-fixed background m-auto d-flex">
</div>