与 一起使用的 HTML 和 CSS 是否良好?
在 HTML 中使用 的最佳方式是什么?
我正在寻找语义良好的 HTML,其格式可通过 CSS 配置。
我希望能够对其进行样式/渲染,使其看起来像这样:
Car: (o) Yes
(X) No
(o) Maybe
Train: (o) Yes
(o) No
(X) Maybe
Address: [An input text box ]
考虑到 CSS,我认为我希望左侧的标签(例如“汽车”和“巴士”)采用某种 text-align: right
块?
我不知道右侧的单选按钮:也许是某种 ,带有
"display: inline-block"
?或者“空白:pre”
?
什么样的块级标签(例如
或
> 或
) 您会推荐吗?编辑:
以下怎么样。
HTML 使用 ,就像 HTML 应该使用的那样,并且按照 中的建议alistapart 文章:
<fieldset>
<legend>Car</legend>
<label><input type="radio" name="car" value="yes"/> Yes</label>
<label><input type="radio" name="car" value="no"/> No</label>
<label><input type="radio" name="car" value="maybe"/> Maybe</label>
</fieldset>
为了让 Firefox 更轻松地访问/定位 的内容,请将其放在
中:
<fieldset>
<legend><span>Car</span></legend>
<label><input type="radio" name="car" value="yes"/> Yes</label>
<label><input type="radio" name="car" value="no"/> No</label>
<label><input type="radio" name="car" value="maybe"/> Maybe</label>
</fieldset>
然后,使用 Legends of Style Revished 将范围的内容定位到字段集的左侧。
CSS 真的必须如此复杂并且特定于浏览器吗?理论上应该可以工作的最简单的 CSS 是什么,而不是实际与那些不完美的浏览器一起工作所需的更复杂的 CSS?如果 很难定位,那么什么是好的(语义)替代方案?
What's the best way to use <input type="radio">
in HTML?
I'm looking for HTML that's semantically good, whose formatting is configurable via CSS.
I want to be able to style/render it to look something like:
Car: (o) Yes
(X) No
(o) Maybe
Train: (o) Yes
(o) No
(X) Maybe
Address: [An input text box ]
Thinking of the CSS, I think that I'd like the labels on the left (e.g. "Car" and "Bus") to be in some kind of text-align: right
block?
I don't know about the radio buttons on the right: in some kind of <span>
perhaps, with "display: inline-block"
? Or "white-space: pre"
?
What kind of block-level tags (e.g. <p>
or <div>
) and/or other tags (e.g. <span>
or <br/>
) would you recommend?
Edit:
How about the following.
HTML uses <legend>
, like HTML is supposed to and as recommended in the alistapart article:
<fieldset>
<legend>Car</legend>
<label><input type="radio" name="car" value="yes"/> Yes</label>
<label><input type="radio" name="car" value="no"/> No</label>
<label><input type="radio" name="car" value="maybe"/> Maybe</label>
</fieldset>
To make it easer for Firefox to access/position the contents of the <legend>
, place it within a <span>
:
<fieldset>
<legend><span>Car</span></legend>
<label><input type="radio" name="car" value="yes"/> Yes</label>
<label><input type="radio" name="car" value="no"/> No</label>
<label><input type="radio" name="car" value="maybe"/> Maybe</label>
</fieldset>
Then, use the browser-specific CSS described in Legends of Style Revised to position the contents of the span to left of the fieldset.
Does the CSS really have to be so complicated and browser-specific? What's the simplest CSS which ought theoretically to work, instead of the more-complicated CSS required to actually work with those imperfect browsers? If <legend>
is hard to position then what's a good (semantic) alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是我通常使用单选按钮和复选框所做的事情。它允许在大多数浏览器中单击关联的文本,而无需执行任何操作,这使得表单更易于使用。 CSS
cursor
更改有助于提醒用户此功能。CSS
HTML
或者,对汽车使用
fieldset
图例,对单选按钮列表使用ul
:CSS
Stylizing a
fieldset
/legend
跨浏览器保持一致并不是太困难;但是,如果您想要图例周围有边框,则需要一个 IE 条件。唯一需要的额外 HTML 是legend
中的包装器span
。CSS
HTML
这就是我所能理解的最简单的了。 实例
This is what I usually do with my radio buttons and checkboxes. It allows the associated text to be clickable in most browsers without having to do any work, which makes the form a little easier to use. The CSS
cursor
change helps to alert the user to this feature.CSS
HTML
Alternatively, use a
fieldset
legend for cars and aul
for the list of radio buttons:CSS
Stylizing a
fieldset
/legend
to be consistent across browsers isn't too difficult; however, it does require one IE conditional if you want a border around thelegend
. The only extra HTML that is necessary is a wrapperspan
within thelegend
.CSS
HTML
That's about as simple as I can get it. Live example
嗯......
Car
和Train
绝对应该是。查看这篇经典的 A List Apart 文章,了解一个非常好的示例: Prettier Accessible Forms
单选按钮标签:好问题!我会再次说
,但
也可以。
Mmmm....
Car
andTrain
should definitely be<label>
s. Check out this classic A List Apart article for a really nice example: Prettier Accessible FormsAs for the radio button labels: Good question! I'd say
<label>
s again, but a<span>
would do as well.css:(需要更改丑陋的类名)
html:
编辑:没有看到其他答案。在我看来,使用字段集而不是“顶级”标签的段落和图例似乎是一个好主意。
EDIT2:根据评论,我同意,在这里使用列表会更干净。新版本将是:
css:
html:
这会更加优雅。但即使使用
display:block
firefox 似乎也不想设置legend
元素的宽度。奇怪的错误。css: (ugly class names need to be changed)
html:
EDIT: didn't see the other answer. Using a fieldset instead of a paragraph and legend for the "top-level" label seems to be a good idea IMO.
EDIT2: according to comments, and I agree, using a list would be cleaner here. The new version would be :
css:
html:
That would be much more elegant. But even with
display:block
firefox doesn't seem to want to set the width of alegend
element. Strange bug.我将使用
在不同浏览器中定位
的困难在“风格传奇修订版" 文章;因此,我可能不会使用
I'll use
<fieldset>
.The difficulty with positioning a
<label>
in different browsers is described in the "Legends of Style Revised" article; so instead of using a<label>
and trying to position it, I might use a<span class="label">
outside the<fieldset>
.