简体   繁体   English

使用垂直对齐将div中的按钮垂直居中

[英]Using vertical-align to vertically center a button within a div

Goal is to center a button within a div, both horizontally and vertically. 目标是使div内的按钮水平和垂直居中。 Horizontal has been dealt with, but having trouble with vertical align. 已经解决了水平问题,但是在垂直对齐方面遇到了麻烦。

From the w3schools page on vertical-align it states that using middle has the following result: w3schools页面上, vertical-align指出使用middle具有以下结果:

"The element is placed in the middle of the parent element" “元素放置在父元素的中间”

In my problem ( see jsFiddle here ) I have set the CSS of a button within the parent element (to the best of my knowledge) to be vertically aligned. 在我的问题中( 请参阅jsFiddle ),我已经将父元素内的按钮的CSS(据我所知)设置为垂直对齐。

HTML: HTML:

<div id='titleSection'>
  <div class='title-inner right quarter-width'>
    <form action='destroy.php' method='POST'>
        <button>Log Out</button>
    </form>
  </div>
  <div class='title-inner left quarter-width'>
    <!--nothing-->
  </div>
  <div class='title-inner center half-width'>
        <h1 class='centered-text'>Title</h1>
  </div>
</div>

CSS: CSS:

* {
font-family:'Arial', Arial, sans-serif;
padding: 0;
margin: 0;
}

h1 {
    padding: 2.1vh 0;
    font-size: 6vmin;
}

div#titleSection {
    width: 100%;
    height: 12vh;
    border: 2.5px solid #ff0fff;
}

div.title-inner {
    height: 100%;
    text-align: center;
    display:inline-block;
}

div.quarter-width { width: 25%; }
div.third-width { width: 33.3%; }
div.half-width { width: 50%; }

div.left {
    float: left;
    background-color: rgba(255, 0, 0, 0.5);
}

div.center {
    float: center;
    background-color: rgba(0, 255, 0, 0.5);
}

div.right {
    float:right;
    background-color: rgba(0, 0, 255, 0.5);
}

.title-inner button {
    vertical-align: middle;
}

Am I wrong to think that the parent element is the title-inner div? 我是否以为父元素是title-inner div是错误的吗?

I have also tried setting the CSS to: 我也尝试将CS​​S设置为:

.title-inner form { vertical-align: middle; }

to no avail. 无济于事。

To recap: All extra CSS and html shown in jsFiddle just to give most accurate idea of what I am trying to do - apologies if this is unnecessary but I'd rather leave it in to avoid losing track of where I am. 回顾一下:jsFiddle中显示的所有其他CSS和html只是为了最准确地说明我要执行的操作-如果没有必要,我深表歉意,但我宁愿将其保留,以免迷失自己的位置。 End goal is simply to (vertically) center the Log Out button within the right-hand (blue) div. 最终目标只是要将“ 注销”按钮居中(垂直)在右侧(蓝色)div中。

The vertical-align property is pretty hard to work with and just a bit painful to be honest. vertical-align属性很难使用,说实话有点痛苦。 A much more reliable way to center is the method shown here: http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/ 一种更可靠的居中方法是此处显示的方法:http: //zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/

I have implemented this below. 我在下面实现了这一点。

Or use flexbox as Luis said if you can support it. 如果可以支持,也可以使用Luis所说的flexbox。

 * { font-family:'Arial', Arial, sans-serif; padding: 0; margin: 0; } h1 { padding: 2.1vh 0; font-size: 6vmin; } div#titleSection { width: 100%; height: 12vh; border: 2.5px solid #ff0fff; } div.title-inner { height: 100%; text-align: center; display:inline-block; } div.quarter-width { width: 25%; } div.third-width { width: 33.3%; } div.half-width { width: 50%; } div.left { float: left; background-color: rgba(255, 0, 0, 0.5); } div.center { float: center; background-color: rgba(0, 255, 0, 0.5); } div.right { float:right; position:relative; background-color: rgba(0, 0, 255, 0.5); } .title-inner button { position:absolute; top:50%; left:50%; transform: translate(-50%, -50%); } 
 <div id='titleSection'> <div class='title-inner right quarter-width'> <form action='destroy.php' method='POST'> <button>Log Out</button> </form> </div> <div class='title-inner left quarter-width'> <!--nothing--> </div> <div class='title-inner center half-width'> <h1 class='centered-text'>Title</h1> </div> </div> 

I usually use JS to vertically center, it seems less complicated to me than CSS workarounds. 我通常使用JS垂直居中,这似乎比CSS解决方法复杂。 I've updated your fiddle with my method: https://jsfiddle.net/bhcoLg9h/3/ 我已经用我的方法更新了您的提琴: https : //jsfiddle.net/bhcoLg9h/3/

HTML: HTML:

<div id='titleSection'>
  <div class='title-inner right quarter-width'>
    <form action='destroy.php' method='POST' id="button1">
        <button>Log Out</button>
    </form>
  </div>
  <div class='title-inner left quarter-width'>
    <!--nothing-->
  </div>
  <div class='title-inner center half-width'>
        <h1 class='centered-text'>Title</h1>
  </div>
</div>

JS: JS:

$(document).ready(function() {
    var OuterHeight = $('#titleSection').height();
    var InnerHeight = $('#button1').height();
    var TopMargin = (OuterHeight-InnerHeight)/2;
    $('#button1').css('margin-top', TopMargin);   
});

You should set display:table for parent div 您应该为父div设置display:table

div.title-inner {
height: 100%;
text-align: center;
display: table;}

And set 并设置

    display: table-cell;
    vertical-align: middle;

for .title-inner form 用于.title-inner form

Credit to @andy-furniss for reaching the answer to the vertical alignment problem by editing the CSS of the child element as follows: 感谢@ andy-furniss通过如下编辑子元素的CSS来解决垂直对齐问题:

.title-inner button {
    position:absolute;
    top:50%;
    transform: translateY(-50%);
}

However the element no longer remained horizontally centered with this method. 但是,此方法不再使元素保持水平居中。 After some playing around with this method I realised that the left edge of the button is the point being used for horizontal central alignment. 在使用这种方法进行一些尝试之后,我意识到按钮的左边缘是用于水平中心对齐的点。 By adding a transformation in the x-direction, the element was shifted to the correct position: 通过在x方向上添加变换,该元素被移动到正确的位置:

.title-inner button {
    position: absolute;
    top:50%;
    transform: translate(-50%, -50%);
}

See snippet below: 请参见下面的代码段:

 * { font-family:'Arial', Arial, sans-serif; padding: 0; margin: 0; } h1 { padding: 2.1vh 0; font-size: 6vmin; } div#titleSection { width: 100%; height: 12vh; border: 2.5px solid #ff0fff; } div.title-inner { height: 100%; text-align: center; display:inline-block; } div.quarter-width { width: 25%; } div.third-width { width: 33.3%; } div.half-width { width: 50%; } div.left { float: left; background-color: rgba(255, 0, 0, 0.5); } div.center { float: center; background-color: rgba(0, 255, 0, 0.5); } div.right { float:right; position:relative; background-color: rgba(0, 0, 255, 0.5); } .title-inner button { position:absolute; top:50%; transform: translate(-50%, -50%); } 
 <div id='titleSection'> <div class='title-inner right quarter-width'> <form action='destroy.php' method='POST'> <button>Log Out</button> </form> </div> <div class='title-inner left quarter-width'> <!--nothing--> </div> <div class='title-inner center half-width'> <h1 class='centered-text'>Title</h1> </div> </div> 

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

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