【问题标题】:How to center vertically div inside div [duplicate]如何在div内垂直居中div [重复]
【发布时间】:2017-09-25 11:18:12
【问题描述】:

首先需要告诉你我正在使用这个代码:

https://jsfiddle.net/hbahar95/27/

  .text.ellipsis {
  position: relative;
  font-size: 14px;
  color: black;
  font-family: sans-serif;
  width: 250px; /* Could be anything you like. */
}

.text-concat {
  position: relative;
  display: inline-block;
  word-wrap: break-word;
  overflow: hidden;
  max-height: 3.6em; /* (Number of lines you want visible) * (line-height) */
  line-height: 1.2em;
  text-align:justify;
}

.text.ellipsis::after {
  content: "...";
  position: absolute;
  right: -12px; 
  bottom: 5px;
}

因为我需要为标题设置多行椭圆。这部分代码有效。

但现在我需要在 div 中垂直居中 div,而这部分代码部分工作......我真的尝试了所有方法但仍然无法解决问题。

您可以在此处查看实时示例:http://phpbb32.majordroid.com/index.php

在蓝色 div 里面一切都很好,但是在白色 div 里面的标题由于某种原因下沉了,或者换句话说没有居中。

所以我希望你能帮助我在div中垂直居中div,它需要在IE8+中工作

谢谢

【问题讨论】:

  • 添加浮动:左;到您的 .list-inner { } 类,然后清除下一个类元素:)
  • 如果我这样做,那么它就没有居中。你可以通过检查员试一试,你会看到它向上移动。
  • 使用 css flexbox 技术,非常简单。查看本指南css-tricks.com/snippets/css/a-guide-to-flexbox
  • 但这不适用于 IE8+
  • 好吧,你可以使用这个很酷的被遗忘的技术 :D -> add display: table;在父 div 上然后使用 display: table-cell;和垂直对齐:中间;在子 div 上,它会在大多数浏览器版本中发挥作用:)。

标签: html css


【解决方案1】:

.box {
  position: relative;
  width: 250px;
  height: 250px;
  background-color: #E4E4E4;
  margin: 30px;
}
.box .text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 150px;
}
.text.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="box">
  <div class="text ellipsis">lorem ipsum dolor sit amet</div>
</div>

【讨论】:

    【解决方案2】:

    Flexbox 助你一臂之力:

    .outer { 
      border:3px solid black; 
      height:300px;
      
      /* Just set up the container element to use flexbox */
      display:flex;
      justify-content:center;
      align-items:center;
    }
    .inner { border:3px dashed green; height:100px; width:100px; }
    <div class="outer">
      <div class="inner"></div>
    </div>

    【讨论】:

    • 尝试将您的关注点从父节点分离到子节点,特别是当子节点是块级元素时 - jsfiddle.net/88q8nwut(与 text-align: center 相反,我的意思是关注点与父节点相关)跨度>
    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 2023-04-09
    • 2014-02-15
    • 1970-01-01
    • 2011-05-20
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多