figcaption垂直高度的数字?

时间:2016-04-11 10:13:29

标签: html css

我有一个figure标记,其中有imgfigcaption

我试图让figcaption的宽度与figure的高度相匹配(根据窗口大小是可变的,你可能会使用vh匹配浏览器高度。原因是我正在旋转figcaption 90度,并希望figcaption文本居中于图的高度。

Fiddle

HTML

<figure>
  <img src="http://placehold.it/350x150">
  <figcaption>Title</figcaption>
</figure>

CSS

figure {
  padding:20px;
  display:block;
  background:#ffa2df;
  position: relative;
}

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

figcaption {
  transform: rotate(-90deg);
  transform-origin: 0 0;
  text-align: center;
  background: red;
  position: absolute;
  width: 100%;
  height: 20px;
  display:flex;
  flex-direction: column; 
  float:left; 
}

1 个答案:

答案 0 :(得分:3)

这可以使用绝对定位并且内部旋转元素也绝对定位。

可以说这也是一个kludge,虽然文本没有破坏,但是高度是有响应的......那里需要媒体查询。

&#13;
&#13;
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
figure {
  padding: 20px;
  display: block;
  background: #ffa2df;
  position: relative;
}
figure img {
  width: 100%;
  height: auto;
}
figcaption {
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 2em;
}
figcaption span {
  position: absolute;
  bottom: 0;
  left: 0;
  white-space: nowrap;
  padding: 0 1em;
  line-height: 2em;
  transform-origin: top left;
  transform: translate(0%, 100%) rotate(-90deg);
  color: white;
}
&#13;
<figure>
  <img src="http://placehold.it/350x150" />
  <figcaption><span>Lorem ipsum dolor sit amet</span>
  </figcaption>
</figure>
&#13;
&#13;
&#13;

替代方案:实验属性 - Writing Mode @ MDN

  

写入模式属性定义文本行是水平放置还是垂直放置以及块进展的方向。

这个是响应式的。

&#13;
&#13;
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
figure {
  padding: 20px;
  display: block;
  background: #ffa2df;
  position: relative;
}
figure img {
  width: 100%;
  height: auto;
}
figcaption {
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  padding: .5em;
  writing-mode: vertical-lr;
  text-align: center;
  color: white;
}
&#13;
<figure>
  <img src="http://placehold.it/350x150" />
  <figcaption>Lorem ipsum dolor sit amet
  </figcaption>
</figure>
&#13;
&#13;
&#13;