【问题标题】:Fading a div overflow into adjacent div将 div 溢出淡入相邻的 div
【发布时间】:2017-11-13 09:37:10
【问题描述】:

我正在尝试创建卡片 UI:https://codepen.io/sarimabbas/pen/qjZYvr

.book_left {
  width: 35%;
  height: 300px;
  float: left;
  overflow-x: hidden;
  background: transparent;
}
.book_left img {
  width: auto;
  height: 100%;
  border-radius: 10px 0 0 10px;
  -webkit-border-radius: 10px 0 0 10px;
  -moz-border-radius: 10px 0 0 10px;
  position: relative;
}
.book_right {
  width: 65%;
  height: 300px;
  float: left;
  background: #000000;
  border-radius: 0 10px 10px 0;
  -webkit-border-radius: 0 10px 10px 0;
  -moz-border-radius: 0 10px 10px 0;
}

我遇到的问题是卡片的左侧(包含图像)可能会溢出到右侧。而不是隐藏这个溢出,我想把它混合到右边的div中,这样文本不会被隐藏并且可以阅读。

这样的事情可能吗?我曾尝试研究浮动、背景图像淡入淡出和 div 的组合,但没有成功。

在相关说明中,使此类卡片具有响应性需要哪些步骤?

【问题讨论】:

    标签: html css user-interface responsive-design


    【解决方案1】:

    我不确定我是否完全理解,但使用下面的代码可以提供透明度,允许查看溢出图像顶部的文本。使用完全黑色的背景,这不是一个选项。

    .book_right {
      width: 65%;
      height: 300px;
      float: left;
      background: rgba(0,0,0, 0.5);
      border-radius: 0 10px 10px 0;
      -webkit-border-radius: 0 10px 10px 0;
      -moz-border-radius: 0 10px 10px 0;
      position: relative;
    }
    

    关于响应性,我会选择弹性框而不是浮动,并使用百分比而不是像素来表示宽度和高度。

    @import url(https://fonts.googleapis.com/css?family=Montserrat:400,700);
    @import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
    .book {
      width: 450px;
      height: 300px;
      background: transparent;
      position: static;
      left: 0;
      right: 0;
      margin: auto;
      top: 0;
      bottom: 0;
      border-radius: 10px;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      box-shadow: 0 2px 1px 0 #777;
    }
    .book_left {
      width: 35%;
      height: 300px;
      float: left;
      overflow-x: visible;
      background: transparent;
    }
    .book_left img {
      width: auto;
      height: 100%;
      border-radius: 10px 0 0 10px;
      -webkit-border-radius: 10px 0 0 10px;
      -moz-border-radius: 10px 0 0 10px;
      position: relative;
    }
    .book_right {
      width: 65%;
      height: 300px;
      float: left;
      background: rgba(0,0,0, 0.5);
      border-radius: 0 10px 10px 0;
      -webkit-border-radius: 0 10px 10px 0;
      -moz-border-radius: 0 10px 10px 0;
      position: relative;
    }
    .book_right h1 {
      color: white;
      font-family: 'Montserrat', sans-serif;
      font-weight: 400;
      text-align: left;
      font-size: 20px;
      margin: 30px 0 0 0;
      padding: 0 0 0 40px;
      letter-spacing: 1px;
    }
    .book_right_details ul {
      list-style-type: none;
      padding: 0 0 0 40px;
      margin: 10px 0 0 0;
    }
    .book_right_details ul li {
      display: inline;
      color: #e3e3e3;
      font-family: 'Montserrat', sans-serif;
      font-weight: 400;
      font-size: 14px;
      padding: 0 40px 0 0;
    }
    .book_right_blurb p {
      color: white;
      font-family: 'Montserrat', sans-serif;
      font-size: 12px;
      padding: 0 40px 0 40px;
      letter-spacing: 1px;
      margin: 10px 0 10px 0;
      line-height: 20px;
    }
    .book_right_blurb a {
      text-decoration: none;
      font-family: 'Montserrat', sans-serif;
      font-size: 14px;
      padding: 0 0 0 40px;
      color: #2ecc71;
      margin: 0;
    }
    .book_right_button {
      padding: 0 0 0 40px;
      margin: 15px 0 0 0;
    }
    .book_right_button a {
      color: #2ecc71;
      text-decoration: none;
      font-family: 'Montserrat', sans-serif;
      border: 2px solid #2ecc71;
      padding: 5px 5px 5px 5px;
      font-size: 12px;
      border-radius: 5px;
      -webkit-transition-property: all;
      transition-property: all;
      -webkit-transition-duration: .5s;
      transition-duration: .5s;
    }
    .book_right_button a:hover {
      color: #000000;
      background-color: #2ecc71;
      cursor: pointer;
      -webkit-transition-property: all;
      transition-property: all;
      -webkit-transition-duration: .5s;
      transition-duration: .5s;
    }
      <div class='book'>
            <div class='book_left'>
              <img src='http://images.gr-assets.com/books/1474171184l/136251.jpg'>
            </div>
            <div class='book_right'>
              <h1>Harry Potter and the Deathly Hallows</h1>
              <div class='book_right_details'>
                <ul>
                  <li>JK Rowling</li>
                  <li>Fiction</li>
                </ul>
                <div class='book_right_blurb'>
                  <p>Harry meets his destiny in the final book of Rowling's acclaimed series.</p>
                </div>
                <div class='book_right_button'>
                <a href='https://www.youtube.com/watch?v=ot6C1ZKyiME' target='_blank'>READ BOOK</a>
                </div>
              </div>
            </div>
          </div>

    【讨论】:

      【解决方案2】:

      有几种方法可以解决这个问题,但我能想到的最简单的方法是将渐变背景应用于右侧框并将.book 的背景设置为黑色。所以像下面这样的东西(当然需要一些润色)

      @import url(https://fonts.googleapis.com/css?family=Montserrat:400,700);
      @import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
      .book {
        width: 450px;
        height: 300px;
        margin: auto;
        border-radius: 10px;
        -webkit-border-radius: 10px;
        -moz-border-radius: 10px;
        box-shadow: 0 2px 1px 0 #777;
        background: #000;
      }
      .book_left {
        width: 35%;
        height: 300px;
        float: left;
        }
      .book_left img {
        width: auto;
        height: 100%;
        border-radius: 10px 0 0 10px;
        -webkit-border-radius: 10px 0 0 10px;
        -moz-border-radius: 10px 0 0 10px;
        /* No need for relative or z-indexes if our layers are in order (later in markup = "higher" layer for position: static) */
      }
      .book_right {
        width: 65%;
        height: 300px;
        float: left;
        border-radius: 0 10px 10px 0;
        -webkit-border-radius: 0 10px 10px 0;
        -moz-border-radius: 0 10px 10px 0;
        /* Gradient that sits on left of right panel - black background has also been applied to .book so that if the image doesn't fit the width we won't end up with weird chunks of missing background */
        /* Generated gradient via: http://colorzilla.com/gradient-editor/ then tweaked a little */
      background: -moz-linear-gradient(left, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 10px, rgba(0,0,0,1) 100%); /* FF3.6-15 */
      background: -webkit-linear-gradient(left, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 10px,rgba(0,0,0,1) 100%); /* Chrome10-25,Safari5.1-6 */
      background: linear-gradient(to right, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 10px,rgba(0,0,0,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', endColorstr='#000000',GradientType=1 ); /* IE6-9 */
      }
      .book_right h1 {
        color: white;
        font-family: 'Montserrat', sans-serif;
        font-weight: 400;
        text-align: left;
        font-size: 20px;
        margin: 30px 0 0 0;
        padding: 0 0 0 40px;
        letter-spacing: 1px;
      }
      .book_right_details ul {
        list-style-type: none;
        padding: 0 0 0 40px;
        margin: 10px 0 0 0;
      }
      .book_right_details ul li {
        display: inline;
        color: #e3e3e3;
        font-family: 'Montserrat', sans-serif;
        font-weight: 400;
        font-size: 14px;
        padding: 0 40px 0 0;
      }
      .book_right_blurb p {
        color: white;
        font-family: 'Montserrat', sans-serif;
        font-size: 12px;
        padding: 0 40px 0 40px;
        letter-spacing: 1px;
        margin: 10px 0 10px 0;
        line-height: 20px;
      }
      .book_right_blurb a {
        text-decoration: none;
        font-family: 'Montserrat', sans-serif;
        font-size: 14px;
        padding: 0 0 0 40px;
        color: #2ecc71;
        margin: 0;
      }
      .book_right_button {
        padding: 0 0 0 40px;
        margin: 15px 0 0 0;
      }
      .book_right_button a {
        color: #2ecc71;
        text-decoration: none;
        font-family: 'Montserrat', sans-serif;
        border: 2px solid #2ecc71;
        padding: 5px 5px 5px 5px;
        font-size: 12px;
        border-radius: 5px;
        -webkit-transition-property: all;
        transition-property: all;
        -webkit-transition-duration: .5s;
        transition-duration: .5s;
      }
      .book_right_button a:hover {
        color: #000000;
        background-color: #2ecc71;
        cursor: pointer;
        -webkit-transition-property: all;
        transition-property: all;
        -webkit-transition-duration: .5s;
        transition-duration: .5s;
      }
      <div class='book'>
              <div class='book_left'>
                <img src='http://images.gr-assets.com/books/1474171184l/136251.jpg'>
              </div>
              <div class='book_right'>
                <h1>Harry Potter and the Deathly Hallows</h1>
                <div class='book_right_details'>
                  <ul>
                    <li>JK Rowling</li>
                    <li>Fiction</li>
                  </ul>
                  <div class='book_right_blurb'>
                    <p>Harry meets his destiny in the final book of Rowling's acclaimed series.</p>
                  </div>
                  <div class='book_right_button'>
                  <a href='https://www.youtube.com/watch?v=ot6C1ZKyiME' target='_blank'>READ BOOK</a>
                  </div>
                </div>
              </div>
            </div>

      让它响应,你可以在.book 上设置一个 % 宽度并可能浮动它。 我的方法需要注意的是,如果图像没有溢出,它将有一个硬边,所以在没有溢出的图像旁边可能看起来很奇怪。您也可以通过在图像上设置百分比宽度来解决此问题,但您需要注意比例差异很大的图像,并确保它们始终覆盖 300px 高度。或者,您可以将图像设置为.book_left 上的背景图像并设置background-size: cover

      在这种情况下,我通常建议将图像裁剪成一致的比例,以避免需要淡化溢出,因为从长远来看,这会让你的生活更轻松。

      另一种可能更一致的淡入淡出方法是相对定位.book_left,然后在其中放置一个绝对定位的div,渐变背景分层在图像顶部,因此类似于具有以下内容的div在.book_left 中添加的属性

      position: absolute;
      right: 0;
      z-index: 1;
      width: 10px; 
      background: -moz-linear-gradient .....
      

      如果你想要淡入淡出,这与填充容器的图像相结合应该会给你一个更一致的外观

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 2019-08-04
        • 1970-01-01
        • 2014-04-25
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多