Make element same width as dynamically sized image?

后端 未结 5 1321
逝去的感伤
逝去的感伤 2021-02-18 23:59

I have a responsive slideshow-type layout with captions below each image.

I\'m attempting to get the caption to be the same width as the image. The problem is that the i

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 00:18

    Try utitlizing border property

    #big_container {
      display: block;
      position: relative;
      width: 100%;
      padding-bottom: 40%;
      white-space: nowrap;
      overflow-x: scroll;
      overflow-y: hidden;
    }
    #big_container>div {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    .little_container {
      display: inline-block;
      height: 100%;
      width: 100%;
      text-align: center;
    }
    #big_container figure {
      display: block;
      height: 100%;
      margin: 0;
    }
    /* 
         set `border` to `45px` as background for `figacption`
         set `border-left`, `border-right`, `border-top` to `0px`  
    */
    img {
      max-height: calc(100% - 40px);
      border: 45px solid #ffffd;
      border-left: 0px;
      border-right: 0px;
      border-top: 0px;
    }
    /*
         set `figcaption` elements
         set `top` to `-50px` to center `content`
         in vertical middle of `border`,
         set `width` to `calc(100% / 3)` : number of `figure` elements
         set `left` to `calc(100% / 3)` : position `content` at 
         horizontal center of `:after` , `border`
    */
    figcaption {
      top: -50px;
      display: block;
      position: relative;
      background-color: transparent;
      padding: 10px;
      line-height: 20px;
      box-sizing: border-box;
      width: calc(100% / 3);
      left: calc(100% / 3);
    }
    have a kitty!!1
    moar kitty!
    too many kitty..

    jsfiddle https://jsfiddle.net/601a5uqv/11/ , plnkr http://plnkr.co/edit/kHnA3GUTiGu3jm4tE8l4?p=preview

提交回复
热议问题