css - 在纯 CSS 中,父级之上的子级具有动态间距

标签 css

我的 CSS 技能现在很低,我需要为我的网站做一些困难的事情。任何帮助将不胜感激。谢谢。

在图片“parent”div(宽度等于某个常量值)中可能有任意数量(这是我的问题的症结所在) child ,其中有一些长词。

.wrap{
   position: static;
}

.parent{
    position: absolute;
    width:100%;
}

.child{
    float: left;
    visibility: hidden;
}
<div class="wrap">
  <div class="parent">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
  </div>
</div>

children 必须“漂浮”在 parent 之上, child 之间的间距应该不会让太长的单词覆盖在每个 child 之上。

我不知道如何用纯 CSS 制作它。 这可能吗? enter image description here

附言我想强调的是: children 必须像图片中那样飞过 parent ,他们之间有一些间距。我知道“相对绝对”定位,但这还不足以让 child 像图中那样飞起来。

最佳答案

您可以将子元素设置为相对定位,然后将它们的顶部偏移量设置为某个负值,这会将它们拉到父元素之上。要在每个 child 之间创建空间,您可以将第一个和最后一个 child 的左右偏移设置为某个值。

请看下面的演示:

/*IGNORE STYLES FROM HERE.....*/

body {
  display: flex;
  height: 100vh;
}

.container {
  border: 5px solid navy;
  width: 500px;
  margin: auto;
}

.box {
  background: white;
  border: 5px solid navy;
  overflow: auto;
  padding: 40px;
  position: relative;
  top: -100px;
}

/*....UNITL HERE*/

.container {
  display: flex;
}

.box {
  overflow: auto;
  position: relative;
  top: -100px;
}
.box:first-child {
  left: -50px;
}
.box:last-child {
  right: -50px;
}
<div class="container">
  <div class="box">
    <h2 class="box__title">Child</h2>
    <p class="box__word">LooooooooooooooongWorrrrrrrrrrd</p>
  </div>
  <div class="box">
    <h2 class="box__title">Child</h2>
    <p class="box__word">LooooooooooooooongWorrrrrrrrrrd</p>
  </div>
  <div class="box">
    <h2 class="box__title">Child</h2>
    <p class="box__word">LooooooooooooooongWorrrrrrrrrrd</p>
  </div>
</div>

关于css - 在纯 CSS 中,父级之上的子级具有动态间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53159419/

相关文章:

html - 带有子菜单的简单菜单不起作用

html - 导航栏变得比 Bootstrap 列宽度更宽

html - 我的背景图像不适合我的视频元素

javascript - 不再使用 jQuery 悬停时隐藏

css - 两个div并排,右侧div固定宽度

javascript - 下拉菜单悬停类未删除

c++ - 显示时如何调整 QPlainTextEdit 小部件的大小

javascript - 使 HTML 内容不对搜索使用react (Ctrl+F)

jquery - Bootstrap 面板折叠创建间隙

css - 在 IE8 中影响 DIV 的负边距?