0

I am trying to understand the following example: https://bootstrapcreative.com/pattern/image-gradient-overlay/

I didn't understand why .img-gradient needed position: relative, so I removed it. To my surprise, removing it caused .img-gradient::after to use the width of the entire column instead of .img-gradient's width.

MDN's documentation on relative positioning implies that on its own (without top, left or other positioning attributes), relative positioning is similar to static positioning:

this doesn't do anything on its own, so we also add top and left properties.

https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Introduction

What am I missing?

.img-gradient{
    position: relative; /* THE LINE IN QUESTION*/
    display: inline-block;
}

.img-gradient::after {
  content:'';
  position:absolute;
  left:0; top:0;
  width:100%; height:100%;
  display:inline-block;
  background: -moz-linear-gradient(top, rgba(0,47,75,0.5) 0%, rgba(220, 66, 37, 0.5) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(220, 66, 37, 0.5)), color-stop(100%,rgba(0,47,75,0.5))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0,47,75,0.5) 0%,rgba(220, 66, 37, 0.5) 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, rgba(0,47,75,0.5) 0%,rgba(220, 66, 37, 0.5) 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, rgba(0,47,75,0.5) 0%,rgba(220, 66, 37, 0.5) 100%); /* IE10+ */
  background: linear-gradient(to bottom, rgba(0,47,75,0.5) 0%,rgba(220, 66, 37, 0.5) 100%); /* W3C */
}

.img-gradient img{
  display:block;
}
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <h2>Before</h2>
                <img src="https://bootstrapcreative.com/wp-bc/wp-content/uploads/2016/11/pablo.png" width="400" />
            </div>
            <!-- /.col-md-6 -->
            <div class="col-md-6">
                <h2>After</h2>
                <div class="img-gradient">
                    <img src="https://bootstrapcreative.com/wp-bc/wp-content/uploads/2016/11/pablo.png" width="400" />
                </div>
                <h2>After</h2>
            </div>
            <!-- /.col-md-6 -->
        </div>
        <!-- /.row -->
    </div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • *If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed'* https://www.w3.org/TR/CSS2/visudet.html#containing-block-details – Temani Afif Apr 06 '20 at 14:07
  • Thank you very much! – Kinan Dira Apr 06 '20 at 14:11

0 Answers0