简体   繁体   English

使用CSS,当广告位于源代码中的最后位置时,您如何在语义结构化的HTML5页面上推送广告?

[英]Using CSS, how do you push ads up on a semantically structured HTML5 page when they are last in the source?

This is a hypothetical question, as I haven't really written any HTML code in years. 这是一个假想的问题,因为多年来我还没有真正编写任何HTML代码。 Provided you have an HTML page, and its content is semantically structured (so you have your main content in a <main> and <article> tag etc.), you would probably have your ads as one of the last elements in your source code, as they will semantically be least important. 假设您有一个HTML页面,并且其内容是语义结构化的(因此您的主要内容位于<main><article>标记等中),则您可能会将广告作为源代码中的最后一个元素,因为它们在语义上将是最不重要的。 Financially, however, they would be rather crucial, so you'd like to have them displayed at the top of your page, above the "fold" (maybe in a right-hand column or "sidebar"). 但是,从财务角度而言,它们非常重要,因此您希望将其显示在页面顶部的“折页”上方(可能显示在右侧列或“侧栏”中)。

Is there a way to "push"/move an aside or div container (holding your ads) up in a sidebar/column, just using CSS - without having to absolutely position them? 是否可以仅使用CSS在侧边栏/列中“推送” /移动asidediv容器(保留您的广告),而不必完全定位它们?

Would you position them relative to the containing element (that forms the column), then add margin-top to the element that would be next visually (ie first child of the column element - say your navigation)? 您是否将它们相对于包含元素(构成该列)定位,然后将margin-top添加到视觉上将要出现的下一个元素(即列元素的第一个子元素-说您的导航)?

Is there a way of doing this if you don't know the height of the "ad" containing element in advance (say because it's containing text, not an ad)? 如果您事先不知道“ ad”包含元素的高度(例如,因为它包含文本而不是广告),有没有办法做到这一点?

Is there a way of achieving this, nowadays, with the latest HTML and CSS features? 如今,是否可以通过最新的HTML和CSS功能来实现这一目标? Or is visual design still dictating markup structure after all? 还是视觉设计毕竟仍然决定着标记的结构? :) :)

It's possible in CSS 3 using display: flex-box and order: # to reorder how elements display, but CSS 3 only works in modern browsers. 在CSS 3中可以使用display: flex-boxorder: #重新排列元素的显示顺序,但是CSS 3仅在现代浏览器中有效。

It doesn't make sense to do this when the ad will always be in a specific place, though. 但是,当广告始终位于特定位置时,这样做是没有意义的。 Save yourself and others the confusion, and keep HTML in charge of layout. 避免让自己和他人感到困惑,并让HTML负责布局。

Is visual design still dictating markup structure after all? 视觉设计毕竟仍在决定标记结构吗?

This question sounds backwards. 这个问题听起来倒是。 " Does markup structure still dictate visual design? " 标记结构是否仍支配视觉设计?
No , that ended with the rise of CSS2 positioning. ,这随着CSS2定位的兴起而结束。

You can use JS to create a widget and position it wherever you want in the layout, or create the ads as separate templates and use JS to retrieve and position them. 您可以使用JS创建小部件并将其放置在布局中的任意位置,也可以将广告创建为单独的模板,然后使用JS检索并放置它们。 This will work in almost any browser. 这几乎可以在所有浏览器中使用。

For the most part if your requirements are simple then it will be easier to simply place the ad element where it is required in the markup. 在大多数情况下,如果您的要求很简单,则将广告元素简单地放置在标记中所需的位置会更容易。

Visual design does not directly dictate markup, but designing markup with layout in mind makes life easier. 视觉设计并不直接决定标记,但是设计标记时要考虑布局,使生活更轻松。

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

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