代码之家  ›  专栏  ›  技术社区  ›  user4298677

在div内垂直居中输入

  •  7
  • user4298677  · 技术社区  · 11 年前

    我想要 input 字段在每个div内垂直居中,即。 color-1 color-2 color-3
    使用 vertical-align: middle 在每个div或 输入 不工作。

    html, body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    
    input[type="text"]  {
      width:150px;
      height: 30px;
      text-align: center;
    }
    
    #color-1 {
    
      height: 33%;
      background: {{yourName}};
      text-align: center; 
      vertical-align: middle;
    }
    
    #color-2{
      height: 33%;
      background: {{color_2}};
      text-align: center; 
      vertical-align: middle;
    }
    
    #color-3{
      height: 33%;
      background: {{color_3}};
      text-align: center; 
      vertical-align: middle;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    
    <div id="color-1">
      <input type="text" ng-model="color_1" placeholder="Enter Color #1">
      <h1>{{color_1}}</h1>
    </div>
    
    <div id="color-2">
      <input type="text" ng-model="color_2" placeholder="Enter Color #2">
      <h1>{{color_2}}</h1>
    </div>
    
    <div id="color-3">
      <input type="text" ng-model="color_3" placeholder="Enter Color #3">
      <h1>{{color_3}}</h1>
    </div>
    2 回复  |  直到 11 年前
        1
  •  13
  •   VamsiKaja    11 年前

    您可以通过两种方式使用 display:flex 或使用 display:table,display:table-row and display:table-cell 你需要用另一个div。

    使用display:flex更好,因为您不需要在另一个div中包装模板

    添加类 color 对于所有颜色分区

    “color”类的样式

    .color{
       height:33%;
       display: flex;
       justify-content: center;
       align-items:center;
    }
    

    有关更多信息,请参阅 https://css-tricks.com/snippets/css/a-guide-to-flexbox/

        2
  •  0
  •   Amanjot Kaur    11 年前

    我做了 demo

     #color-1 
    {
        height: 33%;
        background: {{yourName}};
        vertical-align: top;
        background: red;
        display: flex;
        justify-content: center;
        flex-direction: column;
    
    }
    

    并制作divs

    align=“中心”