简体   繁体   English

div内不同按钮的垂直对齐

[英]Vertical align of different buttons inside a div

I have a bar with some buttons: 我有一个带有一些按钮的栏:

  .Languages { position: relative; padding-left: 40px; width: 100%; height: 62px; background-color: turquoise; margin-bottom: 10px; } .Type1 { position: relative; height: 100%; width: 100px; font-size: 18px; margin-right: 10px; line-height: 62px; box-sizing: border-box; background-color: tomato; border: 0; padding: 0; } .Type2 { position: relative; width: 10px; border-bottom: 1px solid green; margin-left: 5px; cursor: pointer; background-color: tomato; border: 0; padding: 0; vertical-align: middle; } .a { height: 5px; } .b { height: 25px; } .c { height: 45px; } .d { height: 60px; } 
 <div class="Languages"> <button class="Type1" data-id="1">English</button> <button class="Type1" data-id="1">French</button> <button class="Type2 a"></button> <button class="Type2 b"></button> <button class="Type2 c"></button> <button class="Type2 d"></button> </div> <div class="Languages"> <button class="Type1" data-id="1"></button> <button class="Type1" data-id="1"></button> <button class="Type2 a"></button> <button class="Type2 b"></button> <button class="Type2 c"></button> <button class="Type2 d"></button> </div> 

The first buttons fill the vertical space, and I want the second ones to be vertically centered. 第一个按钮填充垂直空间,我希望第二个按钮垂直居中。 But I can't. 但是我不能。

This has something to do with the text inside the buttons and the vertical align, with texts the buttons have a different vertical align that without them. 这与按钮内的文本和垂直对齐方式有关,对于文本,按钮具有不同的垂直对齐方式(没有文本时)。

Does anyone know how to center vertically the second ones, with or without text, and without using flexbox? 有谁知道如何在不使用flexbox的情况下将第二个文本垂直居中(带有或不带有文本)?

Just add vertical-align: middle; 只需添加vertical-align: middle; to all button elements: 所有button元素:

 .Languages { position: relative; padding-left: 40px; width: 100%; height: 62px; background-color: turquoise; } .Type1 { position: relative; height: 100%; width: 100px; font-size: 18px; margin-right: 10px; line-height: 62px; box-sizing: border-box; background-color: tomato; border: 0; padding: 0; } .Type2 { position: relative; width: 10px; border-bottom: 1px solid green; margin-left: 5px; cursor: pointer; background-color: tomato; border: 0; padding: 0; vertical-align: middle; } .a { height: 5px; } .b { height: 25px; } .c { height: 45px; } .d { height: 60px; } button { vertical-align: middle; } 
 <div class="Languages"> <button class="Type1" data-id="1">English</button> <button class="Type1" data-id="1">French</button> <button class="Type2 a"></button> <button class="Type2 b"></button> <button class="Type2 c"></button> <button class="Type2 d"></button> </div> 

Add vertical-align to Type1 class. 将垂直对齐添加到Type1类。

.Type1 {
  position: relative;
  height: 100%;
  width: 100px;
  font-size: 18px;
  margin-right: 10px;
  line-height: 62px;
  box-sizing: border-box;
  background-color: tomato;
  border: 0;
  padding: 0;
    vertical-align: middle;
}

Just use display: flex on container: 只需在容器上使用display: flex

.Languages {
  position: relative;
  padding-left: 40px;
  width: 100%;
  height: 62px;
  background-color: turquoise;

  /*Use flex and its property align-items to vertical-center*/
  display: flex;
  align-items: center;
}

I think this is one of my favourite flex guide. 我认为是我最喜欢的flex指南之一。

Add the following line to the code of type 1 class; 将以下行添加到类型1类的代码中;

vertical-align: middle;

CSS code should be like this; CSS代码应如下所示;

.Type1 {
  position: relative;
  height: 100%;
  width: 100px;
  font-size: 18px;
  margin-right: 10px;
  line-height: 62px;
  box-sizing: border-box;
  background-color: tomato;
  border: 0;
  padding: 0;
  vertical-align: middle;
}

  .Languages { position: relative; padding-left: 40px; width: 100%; height: 62px; background-color: turquoise; } .Type1 { position: relative; height: 100%; width: 100px; font-size: 18px; margin-right: 10px; line-height: 62px; box-sizing: border-box; background-color: tomato; border: 0; padding: 0; vertical-align: middle; } .Type2 { position: relative; width: 10px; border-bottom: 1px solid green; margin-left: 5px; cursor: pointer; background-color: tomato; border: 0; padding: 0; vertical-align: middle; } .a { height: 5px; } .b { height: 25px; } .c { height: 45px; } .d { height: 60px; } 
 <div class="Languages"> <button class="Type1" data-id="1">English</button> <button class="Type1" data-id="1">French</button> <button class="Type2 a"></button> <button class="Type2 b"></button> <button class="Type2 c"></button> <button class="Type2 d"></button> </div> 

The thing is you're not taking into account the line heights. 问题是您没有考虑行高。 Actually, I would do everything using em and %, this is cross-platform and multi-screen solution. 实际上,我将使用em和%进行所有操作,这是跨平台和多屏幕的解决方案。 Here's my result (every element that has position: relative; should also have float:left): 这是我的结果(每个具有position:relative;的元素也应具有float:left):

https://jsfiddle.net/525n53y8/ https://jsfiddle.net/525n53y8/

    .a {
  height: .45em;
  margin-top: 1.575em
}

.b {
  height: .9em;
  margin-top: 1.4em;
}

.c {
  height: 1.8em;
  margin-top: .9em;
}

.d {
  height: 3.6em;
}

every element that has css: "position: relative;" 每个具有css的元素:“ position:relative;” should also have float:left; 还应该有float:left; then the solution in em and % (see fiddle) would be for blocks: https://jsfiddle.net/k3e84v0x/ 那么em和%(请参见小提琴)中的解决方案将适用于块: https : //jsfiddle.net/k3e84v0x/

    .a {
  height: 5px;
  margin-top: 28.5px;
}

.b {
  height: 25px;
  margin-top: 18.5px;
}

.c {
  height: 45px;
  margin-top: 8.5px;
}

.d {
  height: 62px;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM