我需要有一个按钮和一个文本框彼此相邻,并且它们必须完美对齐,就好像一个比另一个大,然后它会立即引人注目。
通常我不会担心浏览器之间的微小差异,但这两个输入位于每个页面上非常突出的位置,并且是该网站的一大功能,因此它们必须在所有主要浏览器和缩放级别中完美对齐。
有谁对如何实现这一目标有任何建议。
就像我想要实现的目标的一个简单例子:新的谷歌主页。开始输入后,自动完成功能启动,搜索框移动到顶部,蓝色按钮与文本框完美对齐。它可以完美地跨浏览器工作,但它在表格中进行了标记。这是否表明这可能是实现这一目标的最佳方式?
I need to have a button and a tex box sitting next to each other and they have to align up perfectly as if one is bigger than the other then it will be instantly noticeable.
Usually I wouldnt bother about small differences between browsers, but these two input are sitting in a very prominent position on every page and are a big feature of the site so they have to align perfectly in all major browsers and zoom levels.
Does anyone have any advice on how to achieve this.
Just as a quick example of what i am trying to achieve: the new google homepage. After starting to type the autocomplete kicks in and the search box is moved to the top with a blue button perfectly aligned to the text box. It works perfectly cross browser but its marked up in a table. Does that suggest that this may be the best way of achieving this?
发布评论
评论(14)
所有优秀的答案!但没有一个是我真正想要的......
我从更直接的方法中获得了更多满足:
All excellent answers! But none were really what I was looking for...
I got more satisfaction from more straightforward approach:
我建议尝试设置
input
/button
配对(在我的示例中为fieldset
)的父元素的样式,以便给出通用font-size
和font-family
,然后使用em
测量值来设计子元素的尺寸/填充/边距。理想情况下,使用相同的 CSS 来设计所有子元素的样式。给出以下标记:
我建议使用一些类似,但适合您的特定设计的CSS:
JS Fiddle 演示。
Following the clarification that this is to replicate/reproduce the style of Google's adjoined text-input/submit button:
JS Fiddle 演示。
尽管上述方法在 Chromium 12.x 和 Opera 11.5 (Ubuntu 11.04) 中运行良好,但 Firefox 似乎在后一个演示中显示了一两个额外的像素。
I'd suggest trying to style the parent element of the
input
/button
pairing (afieldset
, in my example) in order to give a commonfont-size
, andfont-family
, then usingem
measurements for styling dimensions/padding/margins for the child elements. Ideally styling all the child elements with the same CSS.Given the following mark-up:
I'd suggest something similar, but adapted to your particular design, to the following CSS:
JS Fiddle demo.
Following the clarification that this is to replicate/reproduce the style of Google's adjoined text-input/submit button:
JS Fiddle demo.
Albeit the above works fine in Chromium 12.x and Opera 11.5 (Ubuntu 11.04), Firefox seems to show an extra pixel or two in the latter demonstration.
我认为这是非常防弹的:
http://codepen.io/herrfischer/pen/pbkyoJ
这两个元素都只使用:
并使用“normalize.css” - 仅此而已。在 iPhone 上看起来也不错:
I think this is very bulletproof:
http://codepen.io/herrfischer/pen/pbkyoJ
On both elements just use:
And use "normalize.css" - that's all. Looks good on iPhone, too:
为了实现这一目标,我已经拉扯头发好几个小时了。经过大量的研究和实验,以下是在 Chrome、Firefox、IE、Opera 和 Safari(至少是当代版本)中解决 OP 问题的秘诀:
是的,仅此而已。这是产生所需结果的最简约的 CSS:
如果您需要进一步的样式,请随意添加颜色、填充和边距 - 一切都会很好。但是,无论您做什么,都不要明确指定高度。这是唯一的破坏因素,因为每个浏览器计算高度的方式都不同;有些会在选择上方而不是按钮上方留下一个或两个额外的像素,有些会将它们留在按钮上方而不是选择上方,有些会按预期工作。
根据记录,Chrome 似乎在这方面是最一致的(也就是说,如果你指定了高度)——但话又说回来,谁在乎呢。
I've been pulling my hair for hours trying to achieve this. After a lot of research and experimentation, here's the recipe for solving the OP's problem in (at least contemporary versions of) Chrome, Firefox, IE, Opera and Safari:
Yes, that's all there is to it. Here's the most minimalist CSS that produces the desired results:
If you need further styling, feel free to add color, padding and margins -- everything will work out fine. But, no matter what you do, do NOT specify the height explicitly. That's the only deal breaker, because each browser calculates the height differently; some will leave an extra pixel or two above the select and not the button, some will leave them above the button and not the select, and some will work as expected.
For the record, Chrome seems to be the most consistent in this regard (that is, if you do specify the height) -- but then again, who cares.
2019 年,您希望在表单中使用 CSS 网格,即使它们只有一个输入和一个按钮。
这可以节省很多痛苦。
我的工作示例有很多内容,因此我不会在这里发布工作示例。相反,这里是您需要了解的事情。
外部容器可以是
form
,CSS 是display: grid;网格模板行:2em; grid-template-columns: 1fr auto;
输入应该有一个标签,我假设它被设置为隐藏。因此输入的 CSS 为 grid-column: 1; height: 100%; 按钮的 grid-column: 2; height: 100%
你将用边距、背景、填充、边框、边框半径等做你自己的事情。
让按钮与输入对齐的关键是使用具有固定高度(以 em 为单位)的网格行或其他合理的响应式友好单位,并将输入和按钮设置为 100% 的高度。
我没有找到任何上述答案来产生像 CSS 网格选项那样简洁和可管理的代码,该选项适用于 Firefox、Chrome 和基于 Webkit 的浏览器。
请注意,如果使用 CSS 网格,那么您的表单中需要恰好零个
div
元素,但是,如果使用字段集,您可能需要像 CSS 网格一样将它们设置为display: content
不适用于fieldset
。In 2019 you want to be using CSS Grid for your forms even if they just have one input and one button.
This saves a lot of pain.
I have lots going on with my working example so I will not post a working example here. Instead, here are the things you need to know.
The outer container can be the
form
, CSS for this beingdisplay: grid; grid-template-rows: 2em; grid-template-columns: 1fr auto;
The input should have a label which I will assume is set to be hidden. So the CSS for the input has
grid-column: 1; height: 100%;
the button hasgrid-column: 2; height: 100%
You are going to do your own things with margins, backgrounds, paddings, borders, border radius and such like.
The crucial thing of getting the button to line up with the input is done with the grid row having a fixed height in ems or another sensible responsive friendly unit and the input and button being set to a height of 100%.
I did not find any of the above answers to result in as succinct and manageable code as the CSS Grid option which works for me on Firefox, Chrome and a Webkit based browser.
Note that if using CSS Grid then you need precisely zero
div
elements in your form, however, if using fieldsets you may need to set them todisplay: content
as CSS Grid does not work withfieldset
.我检查了很多WEB资源如何做到最好,跨浏览器,独立于主题。我希望我有解决方案,但它不是 100%(它需要通过垂直 CSS 填充进行修剪)。我的问题是让标签和文本输入具有相同的宽度和紧凑。
期望的结果:
我通过多次实验找到的解决方案在我的博客中进行了描述: http://vasvadev.blogspot.cz/2013/02/css-exact-same-height-and-alignment-of.html
解决方案
CSS:
HTML:
我的实验代码由 JSFIDDLE 共享: css-exact-same-height-and-alignment-of .html
I have checked many sources in WEB how to do it the best, cross-browser, theme-independent. I hope I have solution, bit it is not 100% (it needs to be trimmed by vertical CSS padding). My problem was to have label and text-input with the same width and compact.
Wished result:
The solution I found by several experiments is described by my blog: http://vasvadev.blogspot.cz/2013/02/css-exact-same-height-and-alignment-of.html
Solution
CSS:
HTML:
My experimental code is shared by JSFIDDLE: css-exact-same-height-and-alignment-of.html
我花了几天时间试图解决这个问题,但每个解决方案都不完整,并且无法在不同的浏览器中工作。
我发现这段代码可以完成工作:
我已经更新了 Fiddle - https://jsfiddle.net/p9maozk2/
I am trying to solve this for days and every solution is not whole, and not working in different browsers.
I find that this code do the work:
I have updated Fiddle - https://jsfiddle.net/p9maozk2/
您的另一个选择是使用Bootstrap。我通过分配类“input-group 解决了这个问题” 到包含输入和按钮的 div 。
Your other option is to use Bootstrap. I solve this issue by assigning class "input-group" to the div that wraps the input and the button/s.
当尝试以跨浏览器的方式实现像素完美尺寸时,您应该做的第一件事就是考虑实施 CSS 重置。我更喜欢 HTML5 Boilerplate,但是您可以通过谷歌搜索找到其他内容。
CSS 重置对于规范跨浏览器差异非常重要(尤其是在处理表单时)。
When trying to achieve pixel perfect dimensions in a cross-browser way, the very first thing you should do is consider implementing a CSS reset. I prefer HTML5 Boilerplate, but there are others you can find from googling.
The CSS reset is really fundamental in normalizing cross-browser differences (especially when dealing with forms).
对于 IE:您可以使用条件样式表来设置不同的样式:
因此您可以编写:
For IE: You can style things differently by using conditional stylesheets:
So you would write:
对于现在尝试这样做的人,我发现这个解决方案似乎有效。尝试将 : 添加
到两个元素上当前的
css
中,这仍然会使它们稍微未对齐,然后在未对齐的元素中使用顶部填充。我发现:对我有用。
Anyone who's trying to do it now I found that this solution seems to work. Try adding :
to your current
css
on both elements which will still leave them slightly misaligned, and then play around with the top padding in the misaligned element. I found that :worked for me.
这是垂直对齐的
input
+button
组合的示例,元素之间没有边距(感谢font-size:0px
),并且当选择输入字段时,其垂直高度不会增加;这里 font-size 控制垂直高度,而 line-height 会导致垂直错位。html
css
This is an example of a vertically aligned
input
+button
combo, with no margin between elements (thanks tofont-size:0px
), and where the input field does not increase in vertical height when selected; here font-size controls vertical height, whereas line-height would cause vertical misalignment.html
css
嗯,已经过去了大约十年的时间,我对已经发布的任何内容都不满意,所以我花了几天时间来找出解决这个问题的解决方案,该解决方案至少在以下内容中完美运行浏览器版本:
HTML
CSS
除上述之外所需的最低 CSS 规则,您可以更改大多数其他常用的样式规则,例如
color
、background-color
、box-shadow
、border-radius
等等,只要你喜欢。然而,有些事情可能会稍微限制您的样式选择。例如,您应该避免添加/调整padding
,尤其是顶部/底部,因为它可能会剪切文本(如果是,请使用height
/width
规则可能的)。Well, it's been a good decade or so, and I wasn't satisfied with anything already posted, so I spent a couple of days to figure out a solution for this problem that works perfectly in at least following browser versions:
HTML
CSS
Aside from above bare-minimum required CSS rules, you can change most other usual style-rules like
color
,background-color
,box-shadow
,border-radius
, etc. however you like. However, there are some things that could restrict your styling options a bit. For example, you should avoid adding/adjustingpadding
, especially top/bottom, as it might clip text (useheight
/width
rules instead if possible).使用带有图像的背景 div 作为输入。这样更容易保持一致。此外,绝对定位也适合这种情况。
编辑:我5年前写了这个答案,现在有更好的方法。
Use a background div with an image for the input. That is much easier to keep consistent. Also, absolute positioning is good for this type of situation.
edit: I wrote this answer 5 years ago, there are better ways now.