简体   繁体   English

如何在标题(H1、H2 等)标签中定位元素?

[英]How do I Position an Element within Heading (H1, H2, etc) tags?

This seems like it should be something easy, yet it is driving me mad!!这看起来应该很简单,但它让我发疯!!

I need to be able to position a button, or other input element for that matter to the right of a header element, which also contains text.我需要能够将一个按钮或其他输入元素定位在标题元素的右侧,标题元素也包含文本。

The basic markup is:基本标记是:

<h3>Order Details  <input type="button" value="Refund" id="btnRefund"  /></h3>

The desired result is that the text "Order Details" is at the left of the H3 Element and the button is at the right.期望的结果是文本“Order Details”在 H3 元素的左侧,按钮在右侧。 The obvious solution is to add align:right to the button, however this causes the button to appear outside the H3 element or not inline with the text.显而易见的解决方案是向按钮添加 align:right ,但是这会导致按钮出现在 H3 元素之外或不与文本内联。

I've tried various combination of the position property on the H3 element and wrapping the text in div and span tags.我尝试了 H3 元素上 position 属性的各种组合,并将文本包装在 div 和 span 标签中。

I'm sure I kick myself when I get the solution to this.当我得到解决方案时,我敢肯定我会踢自己。

Edit/Update: I am sticking with nickf's answer (for now anyway) as I'm dealing with quite an old system with the H3 already styled in the external style sheet including background colour etc. There are also many instances of the H3 tag being used without any further elements nested in it and to me it does not make sense to have a div tag replicating the style of the H3 tag to accomodate this.编辑/更新:我坚持使用 nickf 的答案(无论如何现在),因为我正在处理一个相当旧的系统,其中 H3 已经在外部样式表中设置了样式,包括背景颜色等。还有许多 H3 标签的实例使用时没有嵌套任何其他元素,对我来说,使用 div 标签复制 H3 标签的样式来适应这一点是没有意义的。

If I were starting from scratch I might have considered Mark's answer.如果我从头开始,我可能会考虑马克的回答。

你必须把它放在第一位

<h3><input type="button" style="float: right">Order Details</h3>

That's not w3c compliant is it?这不符合 w3c 是吗?

Can't you wrap the whole thing in a div,你不能把整个事情都用一个 div 包装起来,

<div><h3>Order Details</h3><input type="button" value="Refund" id="btnRefund"  /></div>

And apply your styles to the div, rather than the h3 if you need some form of uniform look?如果您需要某种形式的统一外观,请将您的样式应用于 div,而不是 h3? Float the h3 left or use display:inline so they appear side-by-side.向左浮动 h3 或使用display:inline使它们并排显示。

Well here is what I've ended up with after being inspired by Mark and Nick F. It is a bit of a kludge but appeals to my sensiblities more.嗯,这就是我在受到 Mark 和 Nick F 的启发后得到的结果。这有点杂乱无章,但更能吸引我的感性。 The h3 is still the parent element, which to me makes more sense, at least semanticaly. h3 仍然是父元素,这对我来说更有意义,至少在语义上是这样。

<h3 style="position:relative;">
   <span style="position:absolute;">Order Details</span>
   <span style="text-align:right;
                width:100%;
                position:absolute">
      <input type="submit" name="btnRefund" value="Refund" id="btnRefund"
         class="TextFields" /></span></h3>

The styles should obviously end up in the style sheet.样式显然应该在样式表中结束。

The downside to this approach is more mark up, than Nicks answer but no more really than Mark's.这种方法的缺点是比尼克斯的回答更多,但并不比马克的回答更准确。 There is also the small kludge of putting a non breaking space as the final character in the h3.在 h3 中放置一个不间断空格作为最后一个字符也是一个小问题。

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

相关问题 html 标题标签的默认字体大小(以像素为单位)是多少(<h1> ,<h2> ,<h3> , 等等。)? - What are the default font sizes (in pixels) for the html heading tags (<h1>, <h2>, <h3>, etc.)? html 标题标签的默认边距是多少(<h1> ,</h1><h2> ,</h2><h3> , ETC。)?</h3> - What are the default margins for the html heading tags (<h1>, <h2>, <h3>, etc.)? 如何在javascript中获取所有h1,h2,h3等元素? - How do I get all h1,h2,h3 etc elements in javascript? HTML 标题是否有长度限制? (h1, h2 等) - Is there a length limit for a HTML heading? (h1, h2 etc) 使用TCPDF定位h1,h2,h3和其他标签 - Position h1,h2,h3 and other tags with TCPDF 使用php将字符串中的标题标签降级(将h1转换为h2,将h2递归转换为h3) - Demote heading tags (convert h1 to h2, h2 to h3 recursively)in string using php 标题标签的默认CSS样式是什么? (H1,h2,h3,h4,h5) - What are the default CSS styling of heading tags? (H1, h2, h3, h4, h5) 我如何定位<p>下一个元素<h1>元素? - How do I position a <p> element under an <h1> element? <small>标签可以在 HTML5 标题(即 h1、h2、h3 等)内吗?</small> - Can a <small> tag be inside an HTML5 heading (i.e. h1, h2, h3, etc…)? 我如何将此 H1 元素定位在右上角 - How Do i position this H1 element in the top right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM