【问题标题】:How to stick image to div block in CSS?如何将图像粘贴到 CSS 中的 div 块?
【发布时间】:2017-09-16 23:18:51
【问题描述】:

我正在尝试将图像粘贴到 CSS 中的 div 块。我无法使用边距移动“图像”……我该怎么办?建议表示赞赏。谢谢。

我想实现什么

    .bottalk {
    background-color: white;
    height: 100px;
    width: 200px;
    margin-top: 20px;
    margin-left: 280px; 
    border-radius: 1.5em;
    }

    .bottalk p {
    padding-top: 5px;
    padding-left: 15px;
    }

    .bot .bottalkwhite {
    height: 40px;
    margin-left: 250px;
    margin-top: 0px;
    }

    .bottalk button {
    background-color: yellow;
    color: purple;
    padding: 5px 5px;
    border: none;
    margin-left: 50px;
    box-shadow: 3px 3px 2px #666666;
    }
    <div class="col-6 bot">
    <div class="bottalk">
    <p>Ready to get started?</p>
    <button>Let's talk</button>
    </div>
    <img src="./img/bottalk.png" alt="bottalk" class="bottalkwhite" />
    </div> </div> 

当前视图

【问题讨论】:

    标签: html css twitter-bootstrap image


    【解决方案1】:

    请忽略背景颜色:我从第二张图片中截取的!

    我已经在bottalk类的div中移动了图片的位置,然后我绝对定位了图片,那么你需要做的就是根据图片设置顶部和左侧的位置,( Cropped the image online so please ignore the quality of the output),所以现在你可以把它放在任何地方。我还添加了background-color:pinkbody 以显示图像,请忽略这个。

    所以总结一下。我将类bottalk 的父div 元素设置为position:relative,并将bottalkwhite 类的子图像设置为position:absolute,以便它可以定位在父元素内部。绝对位置将采用position:relative 相对于直接父级的位置,我希望我的总结清楚。

    body{
        background-color:pink;
    }
    
    .bottalk {
        position: relative;
        background-color: white;
        height: 100px;
        width: 200px;
        margin-top: 20px;
        margin-left: 280px; 
        border-radius: 1.5em;
        }
    
        .bottalk p {
        padding-top: 5px;
        padding-left: 15px;
        }
    
        .bot .bottalkwhite {
        height: 40px;
        position: absolute;
        top: 80%;
        left: -30px;
        }
    
        .bottalk button {
        background-color: yellow;
        color: purple;
        padding: 5px 5px;
        border: none;
        margin-left: 50px;
        box-shadow: 3px 3px 2px #666666;
        }
    <div class="col-6 bot">
        <div class="bottalk">
        <p>Ready to get started?</p>
        <button>Let's talk</button>
        <img src="https://i.stack.imgur.com/7i9bY.gif" alt="bottalk" class="bottalkwhite" />
        </div>
        </div> </div>

    【讨论】:

      【解决方案2】:

      你可以使用position: relative;调整top和left属性的值,如下代码:

          .bottalk {
          position: relative;
          left: -5px;
          top: 10px;
          background-color: white;
          height: 100px;
          width: 200px;
          margin-top: 20px;
          margin-left: 280px; 
          border-radius: 1.5em;
          }
      
          .bottalk p {
          padding-top: 5px;
          padding-left: 15px;
          }
      
          .bot .bottalkwhite {
          height: 40px;
          margin-left: 250px;
          margin-top: 0px;
          }
      
          .bottalk button {
          background-color: yellow;
          color: purple;
          padding: 5px 5px;
          border: none;
          margin-left: 50px;
          box-shadow: 3px 3px 2px #666666;
          }
          <div class="col-6 bot">
          <div class="bottalk">
          <p>Ready to get started?</p>
          <button>Let's talk</button>
          </div>
          <img src="./img/bottalk.png" alt="bottalk" class="bottalkwhite" />
          </div> </div> 

      【讨论】:

        【解决方案3】:

        为了将图像放置在相对于其祖先的确切位置,您可以将位置属性设置为绝对,然后使用左-右-上-下属性,您可以确定其确切位置。像这样:

        .bottalkwhite{
            position: absolute;
            left: 250px;
            top: 0px;
            }
        

        虽然在这样一个特定的 css 规则定义中使用 id 选择器而不是类选择器听起来更合适。

        【讨论】:

          【解决方案4】:

          在图像的包装元素上使用position:relative,并通过左下角的position: absoluteleft: 0bottom: 0 定位图像。然后通过transform: translate调整它的位置,得到想要的效果。

          注意:我将图像移动到 div.botttalk 容器中以相对于其父级定位它。

          像这样:

          body {
            background: #715886;
            font-family: Open Sans, Arial, sans-serif;
          }
          .bottalk {
            background-color: white;
            width: 200px;
            margin-top: 20px;
            margin-left: 100px; 
            border-radius: 8px;
            padding: 24px 16px;
            position: relative;
            text-align: center;
            color: #715886;
          }
          
          .bottalk .bottalkwhite {
            position: absolute;
            left: 0;
            bottom: 0;
            height: 40px;
            color: white;
            transform: translate(-100%, 100%) translate(16px, -16px);
          }
          
          .bottalk h4 {
            line-height: 1;
            margin: 0 0 24px 0;
          }
          
          .bottalk button {
            cursor: pointer;
            color: #715886;
            display: inline-block;
            background-color: #fbcb33;
            font-weight: bold;
            padding: 0 32px;
            border: none;
            line-height: 40px;
            font-size: 14px;
            box-shadow: 0px 1px 2px #666666;
          }
          <div class="col-6 bot">
            <div class="bottalk">
              <h4>Ready to get started?</h4>
              <button>Let's talk</button>
              <img src="https://i.imgur.com/oeUdlld.png" alt="bottalk" class="bottalkwhite" />
            </div>
          </div>

          【讨论】:

          • 这是做什么的?它如何回答这个问题?不要只是呕吐标记。
          • 我在写解释。
          猜你喜欢
          • 2015-06-22
          • 1970-01-01
          • 2018-01-19
          • 2014-10-01
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多