【问题标题】:How do I create a border over div with content?如何使用内容在 div 上创建边框?
【发布时间】:2020-12-16 05:38:39
【问题描述】:

我必须创建一个有 3px 边框的 div,并且这个边界位于 div 中的内容之上,我怎么能在不知道块大小的情况下做到这一点? 下图是一个例子:

我的代码:https://codepen.io/pen/yLObXvv

HTML:

<div class="container">
  <div class="row justify-content-center">
    <div class="col-12 col-md-4">
      <div class="case-study">
        <div class="case-study-image">
          <img src="https://images.unsplash.com/photo-1551434678-e076c223a692?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="img-fluid" alt="Intro image"/>
        </div>
        <div class="case-study-content">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
        </div>
      </div>
    </div>
  </div>
</div>

CSS:

body {
    background-color: #04142d;
}
.case-study {
    color: #fff;
    display: flex;
    margin-top: 2rem;
    flex-direction: row;
    background-color: #0E53DD;
    border-radius: 1rem;
    overflow: hidden;
}
.case-study-image {
    flex: 0 0 50%;
    width: 50%;
}
.case-study-image img {
    height: 100%;
    max-height: 20rem;
    object-fit: cover;
    oject-position: 0 0;
}
.case-study-content {
    flex: 0 0 50%;
    padding: 1rem;
}

【问题讨论】:

  • 您有一些代码可以帮助我们回答您的问题吗?
  • @sven 是的,我在这里添加了一个示例:codepen.io/pen/yLObXvv

标签: html css sass flexbox


【解决方案1】:

请使用 CSS ::after Selector with position: absolute

绝对定位元素的坐标是相对于其父元素的。它会自动定位到其父元素的起点(左上角)。

body {
    background-color: #04142d;
}
.case-study {
    color: #fff;
    display: flex;
    margin-top: 2rem;
    flex-direction: row;
    background-color: #0E53DD;
    border-radius: 1rem;
    position:relative;
    max-width:500px;
}
img {
    max-width:100%;
}
.case-study-image {
    flex: 0 0 50%;
    width: 50%;
}
.case-study-image img {
    height: 100%;
    max-height: 20rem;
    object-fit: cover;
    oject-position: 0 0;
}
.case-study-content {
    flex: 0 0 50%;
    padding: 1rem;
}
.case-study:after {
    content: '';
    border: 3px solid yellow;
    position: absolute;
    width: calc(100% - 6px);
    height: calc(100% - 6px);
    border-radius: 5px;
    right: -10px;
    top: -10px;
}
<div class="case-study">
  <div class="case-study-image"> <img src="https://images.unsplash.com/photo-1551434678-e076c223a692?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="img-fluid" alt="Intro image"/> </div>
  <div class="case-study-content">
    <p>Lorem ipsum dolor sit amet</p>
  </div>
</div>

【讨论】:

    【解决方案2】:
        <!DOCTYPE html>
    <html>
    <head>
        <title>style</title>
        <style>
            div {
                width: 200px;
                height: 200px
                background-color:white;
            }
    
            .move {
                transform: (30px, 180px)
                background: transparent;
                border: 3px solid red;
            }
        </style>
    
    </head>
    <body>
    <div class="move"></div>
    <div></div>
    </body>
    </html>
    

    您可以使用它并尝试按照您想要的方式设置样式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-15
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多