简体   繁体   English

替代h1元素?

[英]alternative to h1 element?

I like the h1 element because it specifies the contents are header style contents, but you're not supposed to put things like images or divs inside an h1, so is there an alternative to an h1 that I can put other markup in? 我喜欢h1元素,因为它指定内容是标题样式内容,但你不应该把图像或div之类的东西放在h1中,那么是否可以替换h1我可以放入其他标记?

My current html looks like this: 我当前的HTML看起来像这样:

<div class="section">
    <h1>
        <div style="float:left">header text</div>
        <div style="float:right">text</div>
        <div style="clear:both;float:none;"></div>
    </h1>
    <div>body contents</div>
</div>

I like the h1 because I can add a css style to any h1 with a div.section class, but I'm not suppoed to put divs in it... 我喜欢h1,因为我可以使用div.section类为任何h1添加css样式,但是我不支持将div放入其中...

You could always do 你可以随时做

<h1>header text <span>text</span></h1>

Then you handle the css for clearing the floats and floating the second text in the css file. 然后你处理css来清除浮点数并将第二个文本浮动到css文件中。

Is there a reason you don't specify just: 有没有理由你没有指定:

<div style="float:right">text</div>
<h1>header text</h1>
<!-- <div style="clear:both"></div>  only if really necessary -->

This will keep your markup semantic, still float text to the right and keep it out of the h1 tag which it is semantically not part of. 这将保持您的标记语义,仍然将text浮动到右侧, 使其远离h1标记,它在语义上不属于它。

The method i personally prefer is to keep the <h1> tags intact and use <span> tags instead of divs inside them. 我个人更喜欢的方法是保持<h1>标签不变并使用<span>标签而不是其中的div。 You can style the spans to be display:block and then treat them like divs if need be. 您可以设置要display:block的范围的样式display:block ,然后在需要时将它们视为div。 This way, the semantic meaning of your <h1> tags is kept, and needless <divs> are omitted. 这样,保留了<h1>标签的语义含义,省略了不必要的<divs> Read up on divitis . 阅读divitis

This won't solve your problem if you need to include images inside your <h1> tags. 如果您需要在<h1>标记内包含图像,这将无法解决您的问题。 You probably shouldn't be adding graphical styling with img tags anyways, but rather applying the image as a background to the the <h1> element, moving style-related graphics out of your markup into your CSS files. 您可能不应该使用img标签添加图形样式,而是将图像作为背景应用于<h1>元素,将标记中的样式相关图形移动到CSS文件中。

You should use a semantic image replacement method : Which makes for the most elaborate design (images, colors ect.. the graphic is your oyster) as well as being completely semantic and accessible. 您应该使用语义图像替换方法 :这使得最精细的设计(图像,颜色等,图形是您的牡蛎)以及完全语义和可访问。

Otherwise as mentioned above, you can add any element that is an inline element: A, SPAN, ect... inside of your H1... but I would shy away from this if you are interested in semantics and being SEO friendly.: 否则如上所述,你可以添加任何内联元素的元素:A,SPAN,等等......在你的H1里面...但如果你对语义和SEO友好感兴趣,我会回避这个:

<style>
  h1{
    background:    url('../path/to/image/image_to_replace_header.jpg') no-repeat top left;  // Sets the BG that will replace your header
    text-indent:   -9999px;  //  Moves the test off screen
    overflow:      hidden;   //  Hides the staggered text for good
    width:         300px;    //  Sets width of block(header)
    height:        100px;    //  Sets height of block(header)
  }  
</style>

<h1>My Awesome Site</h1>

Now your text is still technically there, but you have a pretty photo in its place. 现在你的文字在技术上仍然存在,但你有一张漂亮的照片。 Sighted, non sighted, and robot friendly. 视力不佳,视力不佳,机器人友好。

You can use html5 structural elements : 您可以使用html5结构元素:

<section>
    <header>
        <div>header text</div>
        <div>text</div>
    </header>
    <article>body contents</article>
</section>

Just reverse the nesting order of some of your code: 只需反转某些代码的嵌套顺序:

<div class="section">
   <div style="float:left"><h1>header text</h1></div>
   <div style="float:right"><h1>text</h1></div>
   <div style="clear:both;float:none;">body contents</div>
</div>

I'm not sure that the right-floated text was supposed to be h1, but you get the idea. 我不确定正确浮动的文本应该是h1,但是你明白了。 Often these things are best solved by keeping block-elements on the outside and nesting the line-elements within them. 通常,通过将块元素保持在外部并将线元素嵌套在其中来最好地解决这些问题。

Headers have semantic meaning. 标题具有语义含义。 Think of a magazine and why they use headers. 想想杂志以及为什么他们使用标题。 If you want to place an image in a header for decoration purposes, use a background-image. 如果要将图像放在标题中以进行装饰,请使用背景图像。 I cannot think of a reason why you would need to put an image into a H1 for contextual purposes. 我想不出为什么你需要将图像放入H1以用于上下文目的。

To answer your question directly: yes you can use another method. 直接回答您的问题:是的,您可以使用其他方法。 It keeps your CSS editing ability, as well as having a proper H1 element: 它保持您的CSS编辑能力,以及具有正确的H1元素:

<div class="section">

<div id="Header">
<h1 style="float:left">header text<h1>
<div style="float:right">text</div>
</div>

</h1>
<div>body contents</div>
</div>

All the important text is in the H1 and you can still style it as you like. 所有重要文本都在H1中,您仍然可以根据需要设置样式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM