简体   繁体   English

在div内添加边框

[英]Adding borders inside div

I'm working on a form editor page where users can add custom elements.我正在处理一个表单编辑器页面,用户可以在其中添加自定义元素。 With this, I'm working on a div that can have child column divs inside it.有了这个,我正在处理一个可以在其中包含子列 div 的 div。 Now, the user can set the border of the parent div.现在,用户可以设置父 div 的边框。 Is it possible to have a vertical border between each child column div when they set the parent div's border?设置父 div 的边框时,是否可以在每个子列 div 之间设置垂直边框?

Current behavior when setting the border:设置边框时的当前行为:

在此处输入图片说明

What we want when user set the border:当用户设置边框时我们想要什么:

在此处输入图片说明

What I've tried:我试过的:

  • Adding box-shadow: 5px 0px 0px 0px black;添加box-shadow: 5px 0px 0px 0px black; on column divs except last one but this can't be applied when border is set to dotted/dashed在除最后一个之外的列 div 上,但是当边框设置为点/虚线时无法应用
  • Setting outline: 1px solid black for each column divs, but the outline will go outside the parent div because of the column div paddings设置outline: 1px solid black每列 div 为outline: 1px solid black ,但由于列 div 填充,轮廓将超出父 div
  • Tried @Rory's solution which makes it closer to the goal, however when contents are added on a single column, it breaks the display of making it look like a single div with inside and outside borders尝试了@Rory 的解决方案,使其更接近目标,但是当将内容添加到单个列上时,它会破坏使其看起来像具有内外边框的单个 div 的显示

在此处输入图片说明

Need to consider:需要考虑:

  • Number of column divs can be from 1 up to 6列 div 的数量可以从 1 到 6
  • inner divs can have dynamic content, meaning the height of other inner div can be higher than others but the column divs should each have equal heights内部 div 可以有动态内容,这意味着其他内部 div 的高度可以高于其他 div,但列 div 应该每个具有相同的高度
  • I can user jQuery if needed如果需要,我可以使用 jQuery

HTML File HTML文件

<div class="main">
    <div class="content">
        <div class="column">
            <div class="inner"></div>
        </div>
        <div class="column">
            <div class="inner"></div>
        </div>
        <div class="column">
            <div class="inner"></div>
        </div>
    </div>
</div>

CSS CSS

.main {
  padding-top: 10px;
  padding-bottom: 10px;
  margin: 0;
  width: 100%;
  border: 1px solid black;
}

.main:after {
  clear: both;
  display: table;
  content: " ";
}

.content {
  margin-left: -15px;
  margin-right: -15px;
}

.column {
  float: left;
  padding-right: 15px;
  padding-left: 15px;
  width: 33.33%;
  min-height: 1px;
  box-sizing: border-box;
  background-color: red;
  background-clip: content-box;
}

.inner {
  min-height: 50px;
}

Any help or advise is very much appreciated!非常感谢任何帮助或建议!

To achieve your goal you simply need to move the border , padding-top and padding-bottom styles from .main to .column .为了实现您的目标,您只需将borderpadding-toppadding-bottom样式从.main.column To keep the column heights the same, regardless of content, you can use display: flex on the container, then height: 100% on each .column .为了保持列高相同,无论内容如何,​​您都可以在容器上使用display: flex ,然后在每个.column上使用height: 100%

Also note that you can use the shorthand margin and padding rules to set all 4 dimensions at once.另请注意,您可以使用速记marginpadding规则一次设置所有 4 个维度。

 .main { margin: 0; width: 100%; } .content { margin: 0 -15px; display: flex; } .column { float: left; padding: 10px 15px; width: 33.33%; min-height: 1px; box-sizing: border-box; background-color: red; background-clip: content-box; border: 1px solid black; } .inner { min-height: 50px; }
 <div class="main"> <div class="content"> <div class="column"> Lorem ipsum dolor sit amet consectetur adipiscing elit. Lorem ipsum dolor sit amet consectetur adipiscing elit. Lorem ipsum dolor sit amet consectetur adipiscing elit. Lorem ipsum dolor sit amet consectetur adipiscing elit. <div class="inner"></div> </div> <div class="column"> <div class="inner"></div> </div> <div class="column"> <div class="inner"></div> </div> </div> </div>

Could you set the background color of the main DIV to solid black and then have a 1px margin-left and margin-right for the inner DIV's that have white backgrounds?您能否将主 DIV 的背景颜色设置为纯黑色,然后为具有白色背景的内部 DIV 设置 1px 的左边距和右边距? This would create the illusion of black borders.这会造成黑色边框的错觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM