【问题标题】:What is the best way to position these elements within a div?将这些元素放置在 div 中的最佳方法是什么?
【发布时间】:2016-09-18 17:05:09
【问题描述】:

我目前正在尝试创建一个看起来像这样的结构:

如您所见,这涉及一个相对于页面主体居中的主 div (div 1)。然后它包括第二个 div (div 2),它与 ​​div 1 的左侧对齐,该 div 用黑色填充并包含垂直文本。

为此,我尝试使用以下 HTML 以及如下所示的 css 代码:

.div1 {
  margin-left: 10%;
  margin-right: 10%;
}
.div2 {
  align: left;
  width: 25px;
  background-color: black;
}
.sidetext {
  color: white;
  transform: rotate(270deg);
  transform-origin: left bottom 0;
}
<div class="div1">
  <div class="div2">
    <h3 class="sidetext">Post #NUM</h3>
  </div>
  <p>The main content (text) for this div is written here</p>
</div>   

谁能明白为什么这可能没有给出预期的结果?

【问题讨论】:

    标签: html css


    【解决方案1】:

    试试这个: 添加浮动:左;到您的侧边栏。

    .div1 {
      margin-left: 10%;
      margin-right: 10%;
    }
    .div2 {
      width: 50px;
      float: left;
      background-color: black;
      margin-right: 10px
    }
    .sidetext {
      color: white;
      float: left;
      transform: rotate(270deg);
      transform-origin: 22px center 0;
    }
    <div class="div1">
      <div class="div2">
        <h3 class="sidetext">Post #NUM</h3>
      </div>
      <p>The main content (text) for this div is written here</p>
    </div>

    您可以在这里查看:https://jsfiddle.net/xmmjrspn/

    【讨论】:

    • 谢谢。这个解决方案成功了一半,但它没有使 div2 的高度与 div1 的高度相同。知道该怎么做吗?
    • 对于相同高度的列,我建议使用 flexbox 或 display:table;两者都运行良好
    【解决方案2】:

    align: left 语法错误,改成float: left 就可以了。

    第二种方式:

    .div1 {
      position: relative;
      ...
    }
    .div2 {
      position: absolute;
      left: 0;
      ...
    }
    

    【讨论】:

      【解决方案3】:

      我认为您需要在 div2 上 float: left 并在 div1 上拥有 overflow: autooverflow: auto 将确保您的 div1 围绕 div2。还有position: relative 在带有左值的sidetext 上,以确保您可以看到垂直文本。 https://jsfiddle.net/sx6e0971/2/

      .div1 {
        margin-left: 10%;
        margin-right: 10%;
        overflow: auto;
      }
      .div2 {
        float: left;
        width: 25px;
        background-color: black;
      }
      .sidetext {
        position: relative;
        left: 45px;
        color: white;
        transform: rotate(270deg);
        transform-origin: left bottom 0;
      }
      

      【讨论】:

        【解决方案4】:

        我会选择 flex 和 writing-mode,列高会自行调整。

        https://www.w3.org/TR/css3-writing-modes/

        .div1 {
          border: solid;
          margin: 1em 10%;
          display: flex;
        }
        .div2 {
          text-align:center;
          width: 3.25em;
          padding: 1em;
          white-space: nowrap;
          background: gray;
          -webkit-writing-mode: vertical-lr;
          /* old Win safari */
          writing-mode: vertical-rl;
          writing-mode: tb-lr;
          writing-mode:sideways-lr;/* should be the one */  
          /* eventually IF SIDEWAYS NOT ENOUGH IMPLEMENTED 
          transform: scale(-1, -1); */
        }
        p {
          border-left: solid;
          margin: 0;
          padding:0.5em;
          flex: 1;
        }
        /* eventually center text in p */
        
        p {
          display: flex;
          align-items: center;
          justify-content: center;
        }
        <div class="div1">
          <div class="div2">
            <h3 class="sidetext">Post #NUM</h3>
          </div>
          <p>The main content (text) for this div is written here</p>
        </div>
        <div class="div1">
          <div class="div2">
            <h3 class="sidetext">Post #NUM</h3>
          </div>
          <p>The main content (text) for this div is written here
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line
            <br/>here comes a line</p>
        </div>

        DEMO

        【讨论】:

          【解决方案5】:

          您需要浮动来让sidetext 和p 并排放置。此外,如果您将旋转应用于 div2 而不是其中的文本,并删除 transform-origin css 它将起作用:

          .div1{
            width:90%;
            margin-left: 10%;
            margin-right: 10%;
          }
          
          .div2{
            transform: rotate(270deg);
            background-color: black;
            float: left;
          }
          
          .sidetext{
            color: white;
            float: left;
          }
          
          p {
            float: left;
          }
          

          【讨论】:

            【解决方案6】:

            您实际上已经接近解决方案,这就是您错过的地方:

            • align:left; 应该是 float:left;
            • transform-origin: left bottom 0; 将标题从黑色 div 中旋转出来,最好只围绕中心旋转,translate 将其旋转到正确的位置
            • 添加white-space: nowrap以确保标题在一行
            • 将您的内容包装在一个 div 中,以防您的段落不止一个。

            .div1 {
              margin-left: 10%;
              margin-right: 10%;
            }
            .div2 {
              float: left;
              width: 25px;
              background-color: black;
              min-height: 15em;
            }
            .sidetext {
              white-space: nowrap;
              color: white;
              transform: rotate(-90deg) translateX(-10em);
            }
            <div class="div1">
              <div class="div2">
                <h3 class="sidetext">Post #NUM</h3>
              </div>
              <div class="content">
                <p>The main content (text) for this div is written here</p>
              </div>
            </div>

            还有什么要做: 在此示例中,.div2.content 的高度不同。此外,您还必须根据需要调整 min-heighttranslateX 的数字。

            【讨论】:

            • 效果很好,除了如果 div1 的内容大于它不会增加 div2 的高度。我该如何解决这个问题?
            • 我会尽快更新我的答案,这是简短版本:对于相同高度的列,我建议使用 flexbox。当您使用它时,请考虑@GCyrillus 的答案,恕我直言,这是最好的解决方案,即使浏览器支持有点低。如果你真的不想使用 flexbox,还有其他选择,display:table 可能是最好的,但是关于相同高度列的技术的完整讨论超出了这里的范围。
            • 谢谢,这将是一个很大的帮助。
            【解决方案7】:

            试试这个

                .div1 {
                  border: 1px solid #000;
                  width: 100%;
                  height: 400px;
                  text-align: center;
                  padding: 25px 0;
                }
                .div2 {
                  background-color: black;
                  display: inline-block;
                  color: white;
                }
                p {
                  display: inline-block;
                }
                <div class="div1">
                    <div class="div2">
                        <h3 class="sidetext">Post #NUM</h3>
                    </div>
                    <p>The main content (text) for this div is written here</p>
                </div>

            这里是检查https://jsfiddle.net/ont9sxzx/1/的jsfieddle链接

            【讨论】:

            • QararUlHassan,你能解释一下你的代码吗?
            • @IgorIvancha 他想将 div 和 p 标签并行集中,因此我创建了一个主 div(1) 并使用 text-align: center 我将对象集中在主 div 中,然后使用 display: inline-块我使 div(2) 和 p 平行
            【解决方案8】:

            尝试使用 flexbox 将侧边栏与左侧对齐,然后将书写模式的文本旋转 180 度。

              .vertical {
                writing-mode: vertical-rl;
                white-space: nowrap;
                transform: rotate(180deg);
              }
            

            如果您希望侧边栏高度取决于内容高度,也可以使用text-overflow: ellipsis

            codepen

            【讨论】:

            • ??写作模式+旋转,一个太多了? (见 sideways-rl )。你注意到我的答案和与 sn-p codepen.io/gc-nomade/pen/zqVgMM 一起出现的笔了吗?
            • @GCyrillus 是的,喜欢你的 sn-p,但我认为旋转后的文字看起来更好
            • 好吧 writing-mode:sideways-lr; 不需要轮换 :)
            • 您应该在答案中指定这一点,并添加旧版 webkit/opera 和 IE 浏览器的代码;)(并不是每个人都知道自动前缀在后台工作)
            猜你喜欢
            • 2012-04-23
            • 2021-11-28
            • 2010-09-28
            • 2012-03-21
            • 1970-01-01
            • 2012-05-22
            • 2017-11-29
            • 2014-05-12
            • 1970-01-01
            相关资源
            最近更新 更多