html - 将背景图像放入带有伪元素的形状中

标签 html css background-image pseudo-element

我有一个倾斜的 div,我想知道如何在 div 中获得一张可以完全填满它的图像。例如,它是一个矩形,但在每一端都添加了一个三 Angular 形以创建形状的“倾斜”部分。我想为每个 div 单独的图片(以便填充整个紫色空间),以便能够有不同的。提前感谢您的帮助。

/*1st set*/
.rrcontainer {
    padding-bottom: 265px;
}
.rr h2{
    font-family: montserrat;
    color: white;
    font-size: 3vw;
    text-align: center;
     position: relative;
     text-align: center;
}
.rr > div {
  text-align: center;
}

.rr {
  position: relative;
  height: 250px;
  background: purple;
}
.rr.rr-left {
  z-index: 1;
  float: left;
  width: 55%;
}
.rr.rr-right {
  z-index: 2;
  float: right;
  width: 44%;
}

.rr:after,
.rr:before {
  content: "";
  position: absolute;
  top: 0;
  width: 0;
  height: 0;
}

.rr-left:after {
  right: 0;
  border-left: 100px solid purple;
  border-bottom: 250px solid white;
}

.rr-right:before {
  left: -100px;
  border-right: 100px solid purple;
  border-top: 250px solid transparent;
}

.rr-left > div {
  margin-right: 100px;
  margin-left: 50px;
}

.rr-right > div {
  margin-right: 50px;
  margin-left: 25px;
}

.rr:hover {
  background: pink;
}

.rr-left:hover:after {
  border-left-color: pink;
}

.rr-right:hover:before {
  border-right-color: pink;
}

/*2nd set*/
.llcontainer {
    padding-bottom: 265px;
}

.ll h2{
    font-family: montserrat;
    color: white;
    font-size: 3vw;
    text-align: center;
     position: relative;

  text-align: center;
}
}
.ll > div {
  text-align: center;
}

.ll {
  position: relative;
  height: 250px;
  background: purple;
}
.ll.ll-left {
  z-index: 1;
  float: left;
  width: 55%;
}
.ll.ll-right {
  z-index: 2;
  float: right;
  width: 44%;
}

.ll:after,
.ll:before {
  content: "";
  position: absolute;
  top: 0;
  width: 0;
  height: 0;
}

.ll-left:after {
  right: 0;
  border-left: 100px solid purple;
  border-bottom: 250px solid white;
}

.ll-right:before {
  left: -100px;
  border-right: 100px solid purple;
  border-top: 250px solid transparent;
}

.ll-left > div {
  margin-right: 100px;
  margin-left: 50px;
}

.ll-right > div {
  margin-right: 50px;
  margin-left: 25px;
}

.ll:hover {
  background: pink;
}

.ll-left:hover:after {
  border-left-color: pink;
}

.ll-right:hover:before {
  border-right-color: pink;
}
<div class="rrcontainer">
 <div class="rr rr-left">
	<div>
		<h2>TITLE HERE</h2>
	</div>
</div>
<div class="rr rr-right">
	<div>
        <h2>TITLE HERE</h2>
	</div>
</div>
</div>
<!--     ----------------------------   -->
<div class="llcontainer">
<div class="ll ll-left">
	<div>
		<h2>TITLE HERE</h2>
	</div>
</div>
<div class="ll ll-right">
	<div>
        <h2>TITLE HERE</h2>
	</div>
</div>
</div>

最佳答案

我只留下了一个。请注意,我向“主要”div 元素(<div class="rr rr-left">)添加了背景图像,对于三 Angular 形部分,我使用了 transparent 而不是紫色。 .希望这就是您所需要的。

/*1st set*/
.rrcontainer {
    padding-bottom: 265px;
}
.rr h2{
    font-family: montserrat;
    color: white;
    font-size: 3vw;
    text-align: center;
     position: relative;
     text-align: center;
}
.rr > div {
  text-align: center;
}

.rr {
  position: relative;
  height: 250px;
  background: purple;
}
.rr.rr-left {
  z-index: 1;
  float: left;
  width: 55%;
  background-image: url(http://lorempixel.com/400/200/);
}
.rr.rr-right {
  z-index: 2;
  float: right;
  width: 44%;
}

.rr:after,
.rr:before {
  content: "";
  position: absolute;
  top: 0;
  width: 0;
  height: 0;
}

.rr-left:after {
  right: 0;
  border-left: 100px solid transparent;
  border-bottom: 250px solid white;
}

.rr-right:before {
  left: -100px;
  border-right: 100px solid purple;
  border-top: 250px solid transparent;
}

.rr-left > div {
  margin-right: 100px;
  margin-left: 50px;
}

.rr-right > div {
  margin-right: 50px;
  margin-left: 25px;
}

.rr:hover {
  background: pink;
}

.rr-left:hover:after {
  border-left-color: pink;
}

.rr-right:hover:before {
  border-right-color: pink;
}

/*2nd set*/
.llcontainer {
    padding-bottom: 265px;
}

.ll h2{
    font-family: montserrat;
    color: white;
    font-size: 3vw;
    text-align: center;
     position: relative;

  text-align: center;
}
}
.ll > div {
  text-align: center;
}

.ll {
  position: relative;
  height: 250px;
  background: purple;
}
.ll.ll-left {
  z-index: 1;
  float: left;
  width: 55%;
}
.ll.ll-right {
  z-index: 2;
  float: right;
  width: 44%;
}

.ll:after,
.ll:before {
  content: "";
  position: absolute;
  top: 0;
  width: 0;
  height: 0;
}

.ll-left:after {
  right: 0;
  border-left: 100px solid purple;
  border-bottom: 250px solid white;
}

.ll-right:before {
  left: -100px;
  border-right: 100px solid purple;
  border-top: 250px solid transparent;
}

.ll-left > div {
  margin-right: 100px;
  margin-left: 50px;
}

.ll-right > div {
  margin-right: 50px;
  margin-left: 25px;
}

.ll:hover {
  background: pink;
}

.ll-left:hover:after {
  border-left-color: pink;
}

.ll-right:hover:before {
  border-right-color: pink;
}
<div class="rrcontainer">
 <div class="rr rr-left">
	<div>
		<h2>TITLE HERE</h2>
	</div>
</div>
<div class="rr rr-right">
	<div>
        <h2>TITLE HERE</h2>
	</div>
</div>
</div>
<!--     ----------------------------   -->
<div class="llcontainer">
<div class="ll ll-left">
	<div>
		<h2>TITLE HERE</h2>
	</div>
</div>
<div class="ll ll-right">
	<div>
        <h2>TITLE HERE</h2>
	</div>
</div>
</div>

关于html - 将背景图像放入带有伪元素的形状中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47600766/

相关文章:

html - 无法让视频标题适合屏幕大小?

jquery - 从 AJAX 页面中已有的内联链接重新加载 AJAX

javascript - css代码如何更改事件导航按钮的颜色

javascript - 浏览器默认缩放的 CSS 问题

html - 在 css 中声明 html 主体

CSS:透明与不透明度:0

html - Bootstrap 高度

html - 使用 DIV 时得到错误的输出

Javascript动画背景不覆盖整个屏幕

html - 如何使用 CSS 在文本上方放置背景图像?