html - 如何使用 HTML、CSS 使背景 div 内部 flex ?

标签 html css css-shapes

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 5 年前

我希望我的部分背景看起来像最后的图片所示,我该如何使用 css 实现?

.bg{
  width: 400px;
  height: 200px;
  padding: 20px;
  text-align: center;
  border: 1px solid #000;
  background: red;
  color: #fff;
  display: flex;
}
.bg p{
  text-align: center;
  margin: auto;
  font-size: 36px;
}
<div class="bg">
  <p>Section Content</p>
</div>

Desired background

最佳答案

您可以像下面这样使用 linear-gradient 考虑多个背景,而不需要额外的元素:

.bg{
  width: 400px;
  height: 220px;
  padding: 50px 0;
  box-sizing:border-box;
  text-align: center;
  background:
    linear-gradient(to bottom left , red 50%,transparent 51%) bottom left,
    linear-gradient(to bottom left , transparent 49%,red 50%) top    left,
    linear-gradient(to bottom right, red 50%,transparent 51%) bottom right,
    linear-gradient(to bottom right, transparent 49%,red 50%) top    right,
    red content-box;
  background-size:50% 50px;
  background-repeat:no-repeat;
  color: #fff;
  display: flex;
}
.bg p{
  text-align: center;
  margin: auto;
  font-size: 36px;
}
<div class="bg">
  <p>Section Content</p>
</div>

但如果你需要边框,我会考虑这样的伪元素:

.bg{
  width: 400px;
  height: 200px;
  padding: 20px;
  text-align: center;
  position:relative;
  color: #fff;
  display: flex;
  z-index:0;
}
.bg:before,
.bg:after{
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  background:red;
  border:2px solid #000;
  z-index:-1;
}
.bg:before {
  right:50%;
  left:0;
  transform:skewY(15deg);
  transform-origin:top left;
  border-right:none;
}
.bg:after {
  left:50%;
  right:0;
  transform:skewY(-15deg);
  transform-origin:top right;
  border-left:none;
}
.bg p{
  text-align: center;
  margin: auto;
  font-size: 36px;
}
<div class="bg">
  <p>Section Content</p>
</div>

关于html - 如何使用 HTML、CSS 使背景 div 内部 flex ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404544/

上一篇:javascript - 根据html中的颜色名称更改列背景颜色

下一篇:html - 如何使用 flexbox 为一个元素设置 66% 的区域,为另外两个元素设置 33% 和 33% 的区域?

相关文章:

html - 如何使用 CSS 设置 HTML 段落的样式以防止它们之间出现空行?

html - 如何动态更改 Jekyll _config.yml 中的变量?

css - 使用 CSS 设置 Thesis 下拉菜单的样式

html - 如何更改此导航栏颜色?

html - 带透明居中箭头的边框

css - 使用 CSS 绘制斜边/平行四边形

html - CSS3 eclipse 形状

java - 无法加载资源 : the server responded with a status of 404 ()

html - 向右浮动不使用与向左浮动相同的底部边距

javascript - Flash 横幅周围的超链接