【问题标题】:Align div vertically in the middle [duplicate]在中间垂直对齐 div [重复]
【发布时间】:2015-10-19 23:30:02
【问题描述】:

我正在尝试对齐一些 DIV,但未能成功。

<div class="row">
    <div class="name"><h3>Some long name in here</h3></div>
    <div class="info">
        <div class="delete">X</div>
        <div class="price">1000.00</div>    
      </div>
</div>

这是我到目前为止所做的https://jsfiddle.net/uvo2xdxk/

如何使 .info div 在行内垂直对齐?

【问题讨论】:

  • 哇,老旧的 CSS 垂直居中问题。他们还没有让事情变得简单吗?

标签: css


【解决方案1】:

您可以通过display: table-cell; 实现此目的 从.name.info 中删除float 并将display: table-cell; vertical-align: middle; 分配给它们:)

.row {
  display: table;
  width: 450px;
  background-color: yellow;
  border: 1px solid blue;
}
.name {
  background-color: red;
  display: table-cell;
  vertical-align: middle;
}
.delete {
  background-color: green;
  display: inline;
  margin-right: 25px;
}
.price {
  background-color: blue;
  display: inline;
  margin-right: 5px;
}
.info {
  display: table-cell;
  width: 150px;
  vertical-align: middle;
  background-color: ccc;
  border: 1px solid green;
  text-align: right;
}
<div class="row">
  <div class="name">
    <h3>Some long name in here</h3>
  </div>
  <div class="info">
    <div class="delete">X</div>
    <div class="price">1000.00</div>
  </div>
</div>

【讨论】:

  • 信息框也要右对齐,价格和X要贴在右边。
  • @Eugen 现在检查,我已经更新了答案
  • 是的,现在完美了!谢谢
【解决方案2】:

https://jsfiddle.net/uvo2xdxk/5/

试试这个

.row {
    width: 450px;
    background-color: yellow;
    border: 1px solid blue;
    display:table;
}
.row {
    width: 450px;
    background-color: yellow;
    border: 1px solid blue;
    display:table;
}

分配行显示:表格;属性及其子显示:table-cell;和垂直对齐:中间;并删除浮动:对;来自孩子

【讨论】:

  • 不,价格距离右侧应有 5px。 Price 和 X 都必须右对齐。
  • 只要把 text-align: right;你现在可以查看jsfiddle.net/uvo2xdxk/9
【解决方案3】:

添加到您的 .info:

.info{
//...
    position: absolute;              
    top: 50%;                        
    transform: translate(0, -50%)
}

你的 .row 应该设置:

position: relative;

我已经更新了你的jsfiddle

【讨论】:

  • 不行,看这里:i.stack.imgur.com/8NOxL.png
  • 他关于在 .row 类中间创建 .info 类的问题。为什么你说这行不通? @@
  • 看看我附上的截图。红色和蓝色框与黄色框垂直对齐,但不与红色框垂直对齐(这是基本的OP意图恕我直言)。
  • 红色是 .name 类,不是 .row 类!请再次检查。黄色框是 .row 类。
  • 你说得对。但是 OP 显然想要垂直对齐黄色、红色、蓝色和绿色的盒子!
【解决方案4】:

我修改了你的css,请查看here

.name {
background-color: red;
display: inline;
float: left;
}

.delete {
background-color: green;
display: inline;
 margin-right: 25px;
}

.price {
background-color: blue;
display: inline;
margin-right: 5px;
}

.row {
width: 450px;
background-color: yellow;
border: 1px solid blue;
height: auto;
vertical-align: middle;
overflow:hidden;
position:relative;
}

.info {
float: right;
display: inline-block;
background-color: ccc;
border: 1px solid green;
vertical-align: middle;
position:absolute;
right:0;
transform:translateY(-50%)   ;
top:50%;
}

请检查它是否适合您。

【讨论】:

    【解决方案5】:

    您可以通过以下链接查看。

    https://jsfiddle.net/uvo2xdxk/7/

          .name {
        background-color: red;
        display: inline;
        float: left;
    }
    
    .delete {
        background-color: green;
        display: inline;
         margin-right: 0px;
    }
    
    .price {
        background-color: blue;
        display: inline;
        margin-right: 5px;
    }
    
    .row {
        width: 450px;
        background-color: yellow;
        border: 1px solid blue;
        display:table;
    }
    
    .info {
    
    
        height:57px;
        display: table-cell;
        vertical-align:middle;
        background-color: ccc;
        border: 1px solid green;
        top: 25%;
    }
    

    【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2017-03-03
    • 2018-10-24
    • 2018-10-29
    • 1970-01-01
    • 2018-01-02
    • 2012-06-13
    • 2018-01-11
    • 2021-12-13
    相关资源
    最近更新 更多