我有一个居中的div,里面有文字,背景模糊.当文本的宽度小于div%时,我想要填充(基本上只是模糊的背景),比如说5em.如果文本足够宽,可以换行,我希望填充减少到零,因此换行被推迟到绝对必要的时候.我try 过的min/maxclamp都没有达到预期的效果.这个是可能的吗?

.outer {
  background-color: red;
  display: flex;
  justify-content: center;
}

.inner {
  padding-left: 5em;
  padding-right: 5em;
  backdrop-filter: blur(6px);
  background-color: blue;
}
<div class="outer">
  <div class="inner">
    This should have padding of 5em
  </div>
</div>
<br>
<div class="outer">
  <div class="inner">
    This text should have no padding. This text should have no padding. This text should have no padding. This text should have no padding. This text should have no padding.
  </div>
</div>

推荐答案

您可以在Flexbox中添加两个人造元素,而不是填充.这些元素充当弹性项,允许您指定flexmax-width:

.outer {
  background-color: red;
  display: flex;
  justify-content: center;
}

.outer::before,
.outer::after {
  content: "";
  flex: 1 1 auto;
  max-width: 5em;
}

.outer::before,
.outer::after,
.inner {
  backdrop-filter: blur(6px);
  background-color: gray;
}
<div class="outer">
  <div class="inner">
    This should have padding of 5em
  </div>
</div>
<br>
<div class="outer">
  <div class="inner">
    This text should have no padding. This text should have no padding. This text should have no padding. This text should have no padding. This text should have no padding.
  </div>
</div>

Html相关问答推荐

简化指标与Delta保持一致

注册组件不显示当我导航到注册站点

CSS/添加margin—top到嵌入式facebook帖子

如何在用css使用网格视图时设置宽度?

我无法使用背景图像css,因为我在VS代码中将照片放在不同的文件夹中

如何使用CSS在NSAttributedString中为HTML文本中的图像制作覆盖图

如何用css在右侧创建多个半圆

为什么 Select 元素在带有数据绑定的Blazor上行为怪异?

如何防止弹性项目溢出容器?

网格中的图像zoom 不正确

如何使用 Tailwind CSS 分隔导航栏中的元素?

如何在黑暗模式切换中添加图标

从触发元素移动到有间隙的内容时 CSS 工具提示关闭

使用修饰键微调 HTML 输入范围中的值

在DIV上应用梯度模糊未如预期工作

如何将两个平行 div 的页面内的表格居中?

将表格标题和过滤器调整到表格的顶部边缘

如何将 img 元素定位到特定位置?

不理解 CSS 中的 General sibling combinator (~)

在中心而不是边界处填充 svg 的背景