将两个 div 之一垂直居中在同一行?

发布于 2024-10-01 10:47:31 字数 456 浏览 0 评论 0原文

替代文字 如何让文本“创建新位置”垂直居中?

HTML/CSS 如下。

margin-top:5px 添加到“Create new..”div 会有所帮助,但看起来很hacky。

<div style="margin-top:5px">
  <div style="float:left">Create new position</div>
  <div style="float:right"><input type="submit" id="x" value="Save"></div>
  <div style="clear: both;"></div>
</div>

alt text
How can we have the text "Create New Position" be vertically centered?

HTML/CSS is below.

Adding margin-top:5px to the "Create new.." div helps but it seems hacky.

<div style="margin-top:5px">
  <div style="float:left">Create new position</div>
  <div style="float:right"><input type="submit" id="x" value="Save"></div>
  <div style="clear: both;"></div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

没有伤那来痛 2024-10-08 10:47:32

这会起作用......

<table style="height:500px;width:100%">
<tr>
<td>
<div style="margin-top:px;vertical-alignment: middle;height: 30px; line-height: 30px;">
              <div style="float:left;">Create new position</div>
              <div style="float:right"><input type="submit" id="x" value="Save"></div>
              <div style="clear: both;"></div>
            </div>
            </td>
            </tr>
            </table>

This will work.....

<table style="height:500px;width:100%">
<tr>
<td>
<div style="margin-top:px;vertical-alignment: middle;height: 30px; line-height: 30px;">
              <div style="float:left;">Create new position</div>
              <div style="float:right"><input type="submit" id="x" value="Save"></div>
              <div style="clear: both;"></div>
            </div>
            </td>
            </tr>
            </table>
混浊又暗下来 2024-10-08 10:47:31

以下内容将基于创建两个项目等效的行高来工作。

HTML

<div class="row">
  <span class="left">Create new position</span>
  <span class="right"><input type="button" value="Save" />
</div>

CSS

/* REMOVE THIS PORTION FOR YOUR IMPLEMENTATION, IT IS EXAMPLE ONLY */
* { font-family: Tahoma, sans-serif; font-size: 12px; }
.row { border: 1px solid #ccc; }
/* END EXAMPLE ONLY PORTION */

.row { height: 24px; }
.row > span { line-height: 24px; }
.left { float: left; }
.right { float: right; }

技巧是将包含DIV.row设置为24px high,并将包含的 SPAN 元素设置为 line-height24px。通过这样做,您可以告诉浏览器该文本行占用多少垂直空间。

但请注意,24px 不是重要的部分 - 重要的部分是确定您的 button 的高度,这取决于您的 CSS 和为按钮选择的字体本身。

我给你的例子在这种情况下垂直居中的原因是基于我在顶部放置的EXAMPLE ONLY CSS - 它表示字体大小应该是12px< /代码>。默认的浏览器大小(至少在 Chrome 中)将在按钮周围提供一点额外的边距和填充以及边框 - 这导致总高度大约为 24px,这外观:

Example

边框也是由示例 CSS 创建的,只是为了向您显示垂直对齐是正确的。一旦删除 .row { border: 1px Solid #ccc; },它就会消失。

这是一个 JSBin,显示它的工作原理:

http://jsbin.com/otekil/1/edit

The following will work based on creating a line-height which is equivalent for both items.

HTML:

<div class="row">
  <span class="left">Create new position</span>
  <span class="right"><input type="button" value="Save" />
</div>

CSS:

/* REMOVE THIS PORTION FOR YOUR IMPLEMENTATION, IT IS EXAMPLE ONLY */
* { font-family: Tahoma, sans-serif; font-size: 12px; }
.row { border: 1px solid #ccc; }
/* END EXAMPLE ONLY PORTION */

.row { height: 24px; }
.row > span { line-height: 24px; }
.left { float: left; }
.right { float: right; }

The trick is to set the .row containing DIV to be 24px tall, and also set the contained SPAN elements to have a line-height of 24px. By doing this, you tell the browser how much vertical space to take up for the line of text.

Note, however, that 24px is not the important part - the important part is identifying how tall your button is, which is based on your CSS and your selected font for the button itself.

The reason the example I'm giving you works to vertically center in this case is based on the EXAMPLE ONLY CSS I put in at the top - which says the font-size should be 12px. The default browser sizing (at least in Chrome) is then going to provide a little extra margin and padding around the button, as well as a border - which results in a total height of roughly 24px, and this appearance:

Example

The border is created by the example CSS also, and is only there to show you that the vertical alignment is correct. Once you remove .row { border: 1px solid #ccc; }, it will disappear.

Here is a JSBin which shows it working:

http://jsbin.com/otekil/1/edit

风苍溪 2024-10-08 10:47:31

下面的内容也许能帮到你。

<div align="center">

</div>

所以它可能看起来像这样:

<div align="center">
<div style="float:left">Create new position</div>
<div style="float:right"><input type="submit" id="x" value="Save"></div>
<div style="clear: both;"></div>

The below may help you.

<div align="center">

</div>

So it would look like this maybe:

<div align="center">
<div style="float:left">Create new position</div>
<div style="float:right"><input type="submit" id="x" value="Save"></div>
<div style="clear: both;"></div>

抠脚大汉 2024-10-08 10:47:31

使内部 div 的行高与外部 div 的高度相同。

例子:

<div style="margin-top:5px; height: 100px;">
          <div style="float:left; line-height: 100px;">Create new position</div>
          <div style="float:right"><input type="submit" id="x" value="Save"></div>
          <div style="clear: both;"></div>
        </div>

Make the line-height of the interior div the same height as the height of the exterior div.

Example:

<div style="margin-top:5px; height: 100px;">
          <div style="float:left; line-height: 100px;">Create new position</div>
          <div style="float:right"><input type="submit" id="x" value="Save"></div>
          <div style="clear: both;"></div>
        </div>
嘿咻 2024-10-08 10:47:31

方法略有不同,但您不需要浮动,垂直对齐在这种情况下应该可以正常工作(IMO):

<div>
  Create new position:
  <input type="submit" id="x" value="Save" style="vertical-align:baseline;" />
</div>

Slightly different approach but you don't need the floats, vertical-align should work fine in this instance IMO:

<div>
  Create new position:
  <input type="submit" id="x" value="Save" style="vertical-align:baseline;" />
</div>
陪你到最终 2024-10-08 10:47:31

此方法应该适用于所有浏览器,并且稳定,并且允许您轻松选择您希望内容居中的对象。空容器是我使用此方法时遇到的唯一问题,但在比较其他方法的优缺点时,我很容易忽略它。

解决方案
您使用父级高度一半的 div 来推送内容块(push_to_center 类),然后将其减少内容高度的一半(内容类)。

内联样式声明

<div style="float:left; height:50%; margin-bottom:-55px;"></div>
<div style="clear:both; height:110px; position:relative; width:200px;">
    <div style="float:left">Create new position</div>
    <div style="float:right"><input type="submit" id="x" value="Save"></div>
</div>

完整的 HTML 页面参见它工作):

<html><head><title>Test Center Div</title>
<style>
  .push_to_center {float:left; height:50%; margin-bottom:-55px;}
  .content {clear:both; height:110px; position:relative;}
</style>
</head>
<body>
    <div class="push_to_center"></div>
    <div class="content" style="width:200px; height:110px;">   
      <div style="float:left;">Create new position</div>
      <div style="float:right;"><input type="submit" id="x" value="Save"></div>
   </div>
</body>
</html>

要排除“保存”按钮居中,只需将其移出内容类 div(如果您希望将其放在页面上方,则将其放在页面流的前面,如果您想要将其放在页面下方)它在底部):

This method should work in all browsers, is stable, and allows you to easily choose the object to which you want your content centered. The empty container is the only problem I have with this method, but I can easily overlook it when comparing the pros/cons of other methods.

Solution:
You use a div at half of the parent's height to push your content block (push_to_center class), then reduce it by half the content height (content class).

In-line style declaration

<div style="float:left; height:50%; margin-bottom:-55px;"></div>
<div style="clear:both; height:110px; position:relative; width:200px;">
    <div style="float:left">Create new position</div>
    <div style="float:right"><input type="submit" id="x" value="Save"></div>
</div>

Complete HTML page (see it working):

<html><head><title>Test Center Div</title>
<style>
  .push_to_center {float:left; height:50%; margin-bottom:-55px;}
  .content {clear:both; height:110px; position:relative;}
</style>
</head>
<body>
    <div class="push_to_center"></div>
    <div class="content" style="width:200px; height:110px;">   
      <div style="float:left;">Create new position</div>
      <div style="float:right;"><input type="submit" id="x" value="Save"></div>
   </div>
</body>
</html>

To exclude the Save button from centering simply move it out of the Content classed div (put it earlier in the page flow if you want it above, and below in the page if you want it at bottom):

深海夜未眠 2024-10-08 10:47:31

我总是使用这样的技巧:

style="height: 30px; line-height: 30px; vertical-alignment: middle;"

固定高度加上与行高相同的高度加上中间垂直对齐。

也许上面是更好的答案,我发布这个是因为它很简单并且对我有用。 :)

I always used such trick:

style="height: 30px; line-height: 30px; vertical-alignment: middle;"

Having fixed height plus the same height as line height plus middle vertical alignment.

Maybe the above are better answers, I'm posting this because it's simple and worked for me. :)

抱猫软卧 2024-10-08 10:47:31

通过将 margin-top:4px 添加到“Create Position”div 解决了这个问题。这是我可以开始工作的唯一解决方案!

Addressed this by adding margin-top:4px to the "Create Position" div. This was the only solution I could get to work!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文