【问题标题】:Flexbox with rotated text带有旋转文本的 Flexbox
【发布时间】:2020-07-03 20:05:26
【问题描述】:

我正在尝试使用 flexbox 创建一个 CSS 小部件组件。

组件有一个标题(应该有旋转的文本)和一个内容区域。

标题和内容都应填充父容器的高度,标题应为 15px 宽度,然后内容拉伸以填充剩余部分。

到目前为止,我让内容 100% 正常工作,但由于某种原因,标题没有填满父容器的高度。

到目前为止我有这个:

.widget {
  height: 200px;
  width: 200px;
  border: 1px solid #ddd;
  background-color: #eee;
  display: flex;
}
.widget-header {
  background-color: #1976D2;
  color: #fff;
  transform: rotate(-90deg);
  border: 1px solid red;
  font-size: 10px;
  flex: 0;
  height: 15px;
  align-self: center;
  text-align: center;
}
.widget-content {
  border: 1px solid blue;
  flex: 1;
}
<div class="widget">
  <div class="widget-header">order summary</div>
  <div class="widget-content">
    <p>some text here</p>
    <p>some more text ...</p>
  </div>
</div>

代码笔:http://codepen.io/go4cas/pen/XpryQR

【问题讨论】:

    标签: html css flexbox css-transforms


    【解决方案1】:

    旋转文本,但不旋转较大的容器。标题的宽度为 15px,如指定的那样。

    .widget {
      height: 200px;
      width: 200px;
      border: 1px solid #ddd;
      background-color: #eee;
      display: flex;
    }
    .widget-header {
      background-color: #1976d2;
      flex: 0 0 15px;
      min-width: 0;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .widget-header > span {
      color: #fff;
      font-size: 10px;
      transform: rotate(-90deg);
      white-space: nowrap;
    }
    .widget-content {
      border: 1px solid blue;
      flex: 1;
    }
    <div class="widget">
      <div class="widget-header">
        <span>order summary</span>
      </div>
      <div class="widget-content">
        <p>some text here</p>
        <p>some more text ...</p>
      </div>
    </div>

    revised codepen

    【讨论】:

      【解决方案2】:

      您需要在旋转文本周围再添加一个环绕。那么这个新插入的 wrapper 应该被旋转而不是 flex child。

      HTML:

      <div class="widget">
        <div class="widget-header">
          <div class="header-content">
            order summary
          </div>
        </div>
        <div class="widget-content">
          <p>some text here</p>
          <p>some more text ...</p>
        </div>
      </div>
      

      必要的 CSS:

      .widget {
        position: relative;
        display: flex;
      }
      .widget-header {
        background-color: #1976D2;
        border: 1px solid red;
        text-align: center;
        color: #fff;
        width: 25px;
      }
      .header-content {
        transform: rotate(-90deg);
        transform-origin: 0 100%;
        position: absolute;
        line-height: 25px;
        font-size: 14px;
        height: 25px;
        width: 100%;
        left: 25px;
        bottom: 0;
      }
      

      * {box-sizing: border-box;}
      .widget {
        height: 200px;
        width: 200px;
        border: 1px solid #ddd;
        background-color: #eee;
        position: relative;
        display: flex;
      }
      .widget-header {
        background-color: #1976D2;
        color: #fff;
        border: 1px solid red;
        font-size: 10px;
        width: 27px;
        text-align: center;
        height: 100%;
      }
      .header-content {
        transform: rotate(-90deg);
        transform-origin: 0 100%;
        position: absolute;
        line-height: 25px;
        font-size: 14px;
        z-index: 10;
        height: 25px;
        width: 100%;
        left: 25px;
        bottom: 0;
      }
      .widget-content {
        border: 1px solid blue;
        flex: 1;
      }
      <div class="widget">
        <div class="widget-header">
          <div class="header-content">
            order summary
          </div>
        </div>
        <div class="widget-content">
          <p>some text here</p>
          <p>some more text ...</p>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2019-02-19
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 2019-11-25
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多