【问题标题】:Image with block overlapping design issue具有块重叠设计问题的图像
【发布时间】:2018-06-27 18:55:42
【问题描述】:

早上好,

我目前无法在具有背景图像的容器内对齐 div。目前它们并排坐着,我无法让它们正确对齐,我附上了我想要实现的目标的图像,它在那里,但我只需要对齐才能正常工作:( - 谁能指出我正确的方向?

提前致谢!

我想要达到的目标:

    .card {
        position: relative;
        display: flex;
        align-items: center;
        float: left;
        margin: (30px) 0;
        width: 45%;

        @media #{$BPD} {
            margin: 2.5%;
        }

        @media #{$MaxBPD} {
            width: 100%;
        }

        &:before {
            content: "";
            width: 1px;
            margin-left: -1px;
            float: left;
            height: 0;

            @media #{$BPD} {
                padding-top: 30px / 30px * 100%;
            }
            
        }

        &:after {
            content: "";
            display: table;
            clear: both;
        }
    }

    .card--cta {
        @extend .card;

        display: block;
        padding: 0 !important;

        .card--cta-block {
            display: block;
            padding: 49px 0;
            border-bottom: 1px solid;

            &:last-child {
                border-bottom: 0;
            }
        }

        .card--image {
            margin: 0;
        }
    }


    .card.card--quote {

        display: flex;
        background: $brand-white;
        padding: 48px 24px;
        border: 3px solid black;
        align-items: center;


        @media #{$MaxBPB} {
            padding: 50px 25px;
        }
        
        img {
            z-index: -1;
        }

        div {
            width: 100%;
            text-align: center;

            h2 {
                margin-bottom: 49px;
                line-height: 49px;
            }

            p {
                margin: 30px 0;
                line-height: 30px;
            }
        }
    }
    <div class="card--cta">
      <div class="card card--image">
        <picture>
          <img src="//picsum.photos/400/400/?random" alt="" />
        </picture>
      </div>
      <div class="card card--cta">
        <div class="card--cta-block">
          <h3>Block text 1</h3>
        </div>
        <div class="card--cta-block">
          <h3>Block text 2</h3>
        </div>
        <div class="card--cta-block">
          <h3>Block text 3</h3>
        </div>
      </div>
    </div>

【问题讨论】:

    标签: html css sass


    【解决方案1】:

    在下面显示的元素上添加2个(abc,def)类

    <div class="card--cta abc">
          <div class="card card--image">
            <picture>
              <img src="//picsum.photos/400/400/?random" alt="" />
            </picture>
          </div>
          <div class="card card--cta def">
            <div class="card--cta-block">
              <h3>Block text 1</h3>
            </div>
            <div class="card--cta-block">
              <h3>Block text 2</h3>
            </div>
            <div class="card--cta-block">
              <h3>Block text 3</h3>
            </div>
          </div>
        </div>
    

    并添加这两种样式

    .abc {
        position: relative;
    }
    .def {
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
      text-align: center;
    }
    

    【讨论】:

    • 感谢您的回复,由于某种原因这不起作用:(
    • 你能发布编译后的 CSS 吗?我不能用这个 scss 代码测试
    【解决方案2】:

    这是你想要的吗?

    .card--cta{
      position: relative;
    }
    .divbox{
      position:absolute;
      top: 0;
      left : 0;
      width : 100%;
      height: 100%;
      display : flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
    .divbox div{
      background-color: #fff;
      border: 1px solid black;
      padding: 20px;
    }
        <div class="card--cta">
          <div class="card card--image">
            <picture style="width: 100%;">
              <img style="width: inherit;" src="//picsum.photos/400/400/?random" alt="" />
            </picture>
          </div>
          <div class="card card--cta divbox">
            <div class="card--cta-block">
              <h3>Block text 1</h3>
            </div>
            <div class="card--cta-block">
              <h3>Block text 2</h3>
            </div>
            <div class="card--cta-block">
              <h3>Block text 3</h3>
            </div>
          </div>
        </div>

    【讨论】:

    • 感谢您的回复,一旦由于某种原因添加了它,它就会搞砸:(
    • 我刚刚为此编写了新的 CSS。您需要将此 CSS 嵌入到您现有的 SCSS 代码中。
    【解决方案3】:

    如果您愿意修改 HTML 结构。然后尝试使用以下样式:

    <div class="container ">
      <div style="font-size:36px; width:300px;margin: 0 auto;">
        <h3>Block text 1</h3>
        <h3>Block text 2</h3>
        <h3>Block text 3</h3>
      </div>
    </div>
    <style>
    .container {
        /* The image used */
        background-image: url("//picsum.photos/400/400/?random");
        /* Set a specific height */
        min-height: 500px; 
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
    }
    </style>
    

    【讨论】:

      【解决方案4】:

      这就是我想出的,我最终切换了很多值,但我基本上使用 flexbox 来居中。但是我不知道它是否可以与您编译的 CSS 一起使用,因为您只提供了 SCSS。

      我添加的相关CSS在底部。

      这里是:

      .card {
        position: relative;
        display: flex;
        align-items: center;
        float: left;
        margin: (30px) 0;
        @media #{$BPD} {
          margin: 2.5%;
        }
        @media #{$MaxBPD} {
          width: 100%;
        }
        &:before {
          content: "";
          width: 1px;
          margin-left: -1px;
          float: left;
          height: 0;
          @media #{$BPD} {
            padding-top: 30px / 30px * 100%;
          }
        }
        &:after {
          content: "";
          display: table;
          clear: both;
        }
      }
      
      .card--cta {
        @extend .card;
        display: block;
        padding: 0 !important;
        .card--cta-block {
          display: block;
          padding: 49px 0;
          border-bottom: 1px solid;
          &:last-child {
            border-bottom: 0;
          }
        }
        .card--image {
          margin: 0;
        }
      }
      
      .card.card--quote {
        display: flex;
        background: $brand-white;
        padding: 48px 24px;
        border: 3px solid black;
        align-items: center;
        @media #{$MaxBPB} {
          padding: 50px 25px;
        }
        img {
          z-index: -1;
        }
        div {
          width: 100%;
          text-align: center;
          h2 {
            margin-bottom: 49px;
            line-height: 49px;
          }
          p {
            margin: 30px 0;
            line-height: 30px;
          }
        }
      }
      
      .card--cta {
        align-items: center;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      
      .card--cta-block {
        align-items: center;
        display: flex;
        flex: 1;
        justify-content: center;
      }
      
      .card.card--cta {
        height: 516px;
        width: 516px;
        align-items: stretch;
        background: #fff;
        position: absolute;
      }
      <div class="card--cta">
        <div class="card card--image">
          <picture>
            <img src="//picsum.photos/600/600/?random" alt="" />
          </picture>
        </div>
        <div class="card card--cta">
          <div class="card--cta-block">
            <h3>Block text 1</h3>
          </div>
          <div class="card--cta-block">
            <h3>Block text 2</h3>
          </div>
          <div class="card--cta-block">
            <h3>Block text 3</h3>
          </div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2019-12-21
        • 2021-11-16
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 2018-01-20
        相关资源
        最近更新 更多