gpt4 book ai didi

html - 表单元素和链接具有相同高度的最小 CSS

转载 作者:太空狗 更新时间:2023-10-29 16:09:49 26 4
gpt4 key购买 nike

在我的元素中似乎反复出现的一个问题是将表单元素和链接设置为具有相同的高度。

这是一个简单的例子(fiddle):

HTML:

<select><option>one</option></select>
<input type="text">
<button>foo</button>
<a href="foo">test</a>

CSS:

select,
input,
button,
a {
padding: 0.5rem;
margin: 0.25rem;
border: 1px solid red;
}

所有元素都具有完全相同的样式,包括内边距、边距和边框。但是它们的高度都略有不同,我真的不明白为什么。

谁能

  1. 解释差异的来源? Chrome 检查器告诉我每个元素的实际内部元素大小不同 - 不应该相同吗?
  2. 请告诉我我需要对 CSS 做哪些最小的更改才能实现我想要的效果,同时又不会让每个元素的样式略有不同?我的目标是自由选择内边距、边距和边框大小(使用变量)并且仍然保持一致的高度。

Updated fiddle with solution

最佳答案

最小版本:

您需要添加如下附加规则:

select,
input,
button,
a {
padding: 0.5rem;
margin: 0.25rem;
border: 1px solid red;
display: inline-block; /*new*/
font: inherit; /*new*/
}

但这仍然不能保证它们在某些浏览器中对某些输入类型接收相同的高度。您也可以重置外观,但我不建议在全局范围内这样做,除非设计需要。

-webkit-appearance: none;
appearance: none;

非最小版本:

*,
*:before,
*:after {
box-sizing: border-box;
}

::-moz-focus-inner {
border-style: none;
padding: 0;
}

::-webkit-file-upload-button {
font: inherit;
-webkit-appearance: button;
}

::-webkit-inner-spin-button,
::-webkit-outer-spin-button {
height: auto;
}

::-webkit-search-cancel-button,
::-webkit-search-decoration {
-webkit-appearance: none;
}

button,
input,
optgroup,
select,
textarea {
font-family: inherit;
font-size: 1rem;
line-height: 1.15;
margin: 0;
}

button,
input {
overflow: visible;
}

button,
select {
text-transform: none;
}

[type="checkbox"],
[type="radio"] {
padding: 0;
}

[type="search"] {
outline-offset: -2px;
-webkit-appearance: textfield;
}

[type="color"],
[type="date"],
[type="datetime"],
[type="datetime-local"],
[type="email"],
[type="month"],
[type="number"],
[type="password"],
[type="search"],
[type="tel"],
[type="text"],
[type="time"],
[type="url"],
[type="week"],
select,
textarea,
button,
[type="button"],
[type="reset"],
[type="submit"] {
display: inline-block;
vertical-align: middle;
height: calc(2.25rem + 2px);
color: #333;
border: 1px solid #ccc;
border-radius: 3px;
}

[type="color"],
[type="date"],
[type="datetime"],
[type="datetime-local"],
[type="email"],
[type="month"],
[type="number"],
[type="password"],
[type="search"],
[type="tel"],
[type="text"],
[type="time"],
[type="url"],
[type="week"],
select,
textarea {
max-width: 100%;
padding: 0.5rem;
background-clip: padding-box;
background-color: #fff;
box-shadow: inset 1px 1px 2px rgba(0, 0, 0, 0.1);
}

[type="color"]:focus,
[type="date"]:focus,
[type="datetime"]:focus,
[type="datetime-local"]:focus,
[type="email"]:focus,
[type="month"]:focus,
[type="number"]:focus,
[type="password"]:focus,
[type="search"]:focus,
[type="tel"]:focus,
[type="text"]:focus,
[type="time"]:focus,
[type="url"]:focus,
[type="week"]:focus,
select:focus,
textarea:focus {
border-color: rgb(30, 144, 255);
box-shadow: 0 0 2px rgba(30, 144, 255, 0.8);
outline: 0;
}

button,
[type="button"],
[type="reset"],
[type="submit"] {
padding: 0.5rem 0.75rem;
background-color: #f7f7f7;
box-shadow: 0 1px 0 #ccc;
cursor: pointer;
-webkit-appearance: button;
}

button:hover,
[type="button"]:hover,
[type="reset"]:hover,
[type="submit"]:hover {
background-color: #fafafa;
border-color: #999;
}

button:focus,
[type="button"]:focus,
[type="reset"]:focus,
[type="submit"]:focus {
border-color: rgb(30, 144, 255);
box-shadow: 0 0 2px rgba(30, 144, 255, 0.8);
outline: 0;
}

button:active,
[type="button"]:active,
[type="reset"]:active,
[type="submit"]:active {
background-color: #eee;
border-color: #999;
box-shadow: inset 0 2px 5px -3px rgba(0, 0, 0, 0.5);
}

button:disabled,
[type="button"]:disabled,
[type="reset"]:disabled,
[type="submit"]:disabled {
background-color: #f7f7f7;
color: #a0a5aa;
border-color: #ddd;
box-shadow: none;
text-shadow: 0 1px 0 #fff;
cursor: default;
}

select {
-moz-appearance: textfield;
-webkit-appearance: textfield;
}

select::-ms-expand {
display: none;
}

select[multiple],
select[size]:not([size="1"]) {
height: auto;
padding: 0;
}

select[multiple] option,
select[size]:not([size="1"]) option {
padding: 0.5rem;
}

select:not([multiple]):not([size]),
select:not([multiple])[size="1"] {
padding-right: 2rem;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z'/%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3C/svg%3E") right 0.25rem center no-repeat;
}

textarea {
height: auto;
overflow: auto;
}
<select>
<option>one</option>
</select>
<input type="text" placeholder="text">
<button>foo</button>

上面的大部分代码都没有直接回答问题甚至不相关,也不包括<a>。标签。但在真实的 Web 应用程序中,它可能最终具有或多或少相同数量的 CSS。

关于html - 表单元素和链接具有相同高度的最小 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com