Image overlay on responsive sized images bootstrap

前端 未结 5 1624
Happy的楠姐
Happy的楠姐 2020-12-04 22:36

I am trying to create a responsive grid of images (with descriptions) that when moused over will have a color overlay (just the image and not the text). Because of the respo

相关标签:
5条回答
  • 2020-12-04 23:05

    If i understand your question you want to have the overlay just over the image and not cover everything?

    I'd set the parent DIV (i renamed in content in the jsfiddle) position to relative, as the overlay should be positioned relative to this div not the window.

    .content
    {
      position: relative;
    }
    

    I did some pocking around and updated your fiddle to just have the overlay sized to the img which (I think) is what you want, let me know anyway :) http://jsfiddle.net/b9Vyw/

    0 讨论(0)
  • 2020-12-04 23:11

    I had a bit of trouble getting this to work as well. Using brouxhaha's answer got me 90% of the way to what I was looking for. But the padding adjust wouldn't allow me to put the text anywhere I wanted. Using top and left seemed to work better for my purposes.

    .project-overlay {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        color: #fff;
        top: 80%;
        left: 20%;
    }
    

    http://jsfiddle.net/1rz0b7d8/1/

    0 讨论(0)
  • 2020-12-04 23:12

    Add a class to the containing div, then set the following css on it:

    .img-overlay {
        position: relative;
        max-width: 500px; //whatever your max-width should be 
    }
    

    position: relative is required on a parent element of children with position: absolute for the children to be positioned in relation to that parent.

    DEMO

    0 讨论(0)
  • 2020-12-04 23:16
    <div class="col-md-4 py-3 pic-card">
              <div class="card ">
              <div class="pic-overlay"></div>
              <img class="img-fluid " src="images/Site Images/Health & Fitness-01.png" alt="">
              <div class="centeredcard">
                <h3>
                  <span class="card-headings">HEALTH & FITNESS</span>
                </h3>
                <div class="content-inner mt-5">
                  <p class="lead  p-overlay">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae ipsam nemo quasi quo quae voluptate.</p>
                </div>
              </div>
              </div>
            </div>
    
    
    .pic-card{
         position: relative;
    
     }
     .pic-overlay{
    
        top: 0;
        left: 0;
        right:0;
        bottom:0;
        width: 100%;
        height: 100%;
        position: absolute;
        transition: background-color 0.5s ease;
    
     }
     .content-inner{
        position: relative;
         display: none;
     }
    
     .pic-card:hover{
         .pic-overlay{
            background-color: $dark-overlay;
    
         }
    
         .content-inner{
             display: block;
             cursor: pointer;
         }
    
         .card-headings{
            font-size: 15px;
            padding: 0;
         }
    
         .card-headings::after{
            content: '';
            width: 80%;
            border-bottom: solid 2px  rgb(52, 178, 179);
            position: absolute;
            left: 5%;
            top: 25%;
            z-index: 1;
         }
         .p-overlay{
             font-size: 15px;
         }
     }
    
    
    
    enter code here
    
    0 讨论(0)
  • 2020-12-04 23:25

    When you specify position:absolute it positions itself to the next-highest element with position:relative. In this case, that's the .project div.

    If you give the image's immediate parent div a style of position:relative, the overlay will key to that instead of the div which includes the text. For example: http://jsfiddle.net/7gYUU/1/

     <div class="parent">
        <img src="http://placehold.it/500x500" class="img-responsive"/>
        <div class="fa fa-plus project-overlay"></div>
     </div>
    
    .parent {
       position: relative;
    }
    
    0 讨论(0)
提交回复
热议问题