响应式45deg倒置矩形背景覆盖图像

时间:2018-12-19 05:35:23

标签: html css responsive

在方形容器内,我想要一个可以旋转45度的方形,它可以响应。这是到目前为止我可以做的代码笔:

<div class="container">
  <figure>
      <img src="https://placekitten.com/500/500"/>
    <figcaption class="caption-1"><span class="caption-1-text">Best Kitty</span></figcaption>
  </figure>
</div>


.container {
  display: flex;
}

img {
  width: 100%;
  height: 100%;
}

figure {
  position: relative;
  max-width: 500px;
  height: 500px;
}

.caption-1 {
  font-size: 1.25em;
  position: absolute;
  top: 18%;
  right: 25%;
  background-color: white;
  width: 200px;
  height: 200px;
  transform: rotate(45deg);
}

.caption-1-text {
  display:block;
  transform: rotate(-45deg);
}

https://codepen.io/khanharis87/pen/KbNNYb

我认为这完全没有反应。使用媒体查询的最佳解决方案是什么? SVG倒方形或?

3 个答案:

答案 0 :(得分:2)

您可以考虑渐变和多个背景:

.box {
   display:inline-block;
   width:200px;
   height:200px;
   padding:10px; /*control the space around the rotated square*/
   border:1px solid;
   box-sizing:border-box;
   background:
      linear-gradient(to top    left, #fff1f2 49.8%,transparent 50%) top    left/50% 50% content-box,
      linear-gradient(to top    right,#fff1f2 49.8%,transparent 50%) top    right/50% 50% content-box,
      linear-gradient(to bottom left, #fff1f2 49.8%,transparent 50%) bottom left/50% 50% content-box,
      linear-gradient(to bottom right,#fff1f2 49.8%,transparent 50%) bottom right/50% 50% content-box,
      var(--i,url(https://picsum.photos/200/300?image=0));
    background-repeat:no-repeat;
   
}
<div class="box">
</div>

<div class="box" style="--i:url(https://picsum.photos/200/300?image=1069);width:150px;height:150px;">
</div>
<div class="box" style="--i:url(https://picsum.photos/200/300?image=1069);width:150px;height:150px;padding:20px;">
</div>

您也可以对代码执行相同操作:

figure {
  display:inline-block;
   position:relative;
}

figure figcaption {
   position:absolute;
   top:10px;
   left:10px;
   right:10px;
   bottom:10px;
   box-sizing:border-box;
   background:
      linear-gradient(to top    left, #fff1f2 49.8%,transparent 50%) top    left,
      linear-gradient(to top    right,#fff1f2 49.8%,transparent 50%) top    right,
      linear-gradient(to bottom left, #fff1f2 49.8%,transparent 50%) bottom left,
      linear-gradient(to bottom right,#fff1f2 49.8%,transparent 50%) bottom right;
    background-size:50% 50%;
    background-repeat:no-repeat;
}
<figure>
  <img src="https://placekitten.com/150/150" />
  <figcaption class="caption-1"></figcaption>
</figure>

<figure>
  <img src="https://placekitten.com/200/200" />
  <figcaption class="caption-1"></figcaption>
</figure>

答案 1 :(得分:0)

您可以使用百分比而不是像素来使其响应:

figure {
  position: relative;
  width: 50%;
  height: 50%;
}

Exmaple

答案 2 :(得分:-2)

    <!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"> 
</script>
<title>Untitled</title>
<style>
.container {
  display: flex;
}
figure{
  position: relative;
  max-width: 500px;
  height: 500px;
  background-image: url("https://placekitten.com/500/500");
  background-size: cover;
  background-color:#dfdfdf;
  width:100%;
  display: flex;
  margin: 0;
}
img {
  width: 100%;
  height: 100%;
}

figure {

}

.caption-1 {
  font-size: 1.25em;
  position: relative;
/*   top: 18%;
  right: 25%; */
  background-color: white;
  width: 200px;
  height: 200px;
  transform: rotate(45deg);
  margin: auto;
  align-items: center;
  justify-content: center;
  display: flex;
}

.caption-1-text {
  display:block;
  transform: rotate(-45deg);
}
</style>
</head>
<body>
    <div class="container">
  <figure>
<!--       <img src="https://placekitten.com/500/500"/> -->
    <figcaption class="caption-1"><span class="caption-1-text">Best Kitty</span></figcaption>
  </figure>
</div>
</body>
</html>