简体   繁体   English

CSS:代码内容后的浮动元素

[英]CSS: Floated element after the content in code

I need to have a floated element after the content/text that's supposed to flow around it in my code for SEO reasons. 由于SEO的原因,我需要在代码中围绕它的内容/文本后添加一个float元素。 Usually floats are done like so: 通常,浮动是这样完成的:

CSS: CSS:

#menu {
float: right;
width: 180px;
padding: 10px;
background: #fcc;
margin: 0 0 15px 15px;
}

HTML: HTML:

<div id="menu">This is a right float. The long text flows around it.</div>

<div id="content"><p>This is a long text. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Praesent nec risus.
Praesent adipiscing aliquet magna. Proin bibendum velit
vitae tortor. Vestibulum a dui quis urna feugiat viverra. 
Vestinbulum diam dui, ullamcorper in, rhoncus at, facilisis at, 
lorem. Phasellus turpis metus, sodales sit amet, laoreet nec, 
aliquet sit amet, tortor. Vivamus massa orci, gravida sit amet, 
dictum quis, euismod a, est. Aenean pretium facilisis nunc.</p>
<p>Nulla eros mauris, egestas eget, ullamcorper sed, aliquam ut, 
nulla. Phasellus facilisis eros vel quam. Etiam rutrum turpis 
a nibh. Integer ipsum. Vestibulum lacus diam, varius in, 
blandit non, viverra sit amet, sapien. Sed porta sollicitudin 
nibh. Nam eget metus nec arcu ultricies dapibus.</p></div>

But I need to have the HTML like this: 但是我需要这样的HTML:

<div id="content"><p>This is a long text. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Praesent nec risus.
Praesent adipiscing aliquet magna. Proin bibendum velit
vitae tortor. Vestibulum a dui quis urna feugiat viverra. 
Vestinbulum diam dui, ullamcorper in, rhoncus at, facilisis at, 
lorem. Phasellus turpis metus, sodales sit amet, laoreet nec, 
aliquet sit amet, tortor. Vivamus massa orci, gravida sit amet, 
dictum quis, euismod a, est. Aenean pretium facilisis nunc.</p>
<p>Nulla eros mauris, egestas eget, ullamcorper sed, aliquam ut, 
nulla. Phasellus facilisis eros vel quam. Etiam rutrum turpis 
a nibh. Integer ipsum. Vestibulum lacus diam, varius in, 
blandit non, viverra sit amet, sapien. Sed porta sollicitudin 
nibh. Nam eget metus nec arcu ultricies dapibus.</p></div>

<p id="menu">This is a right float. Because it's placed below the text in code,
it also appears that way.</p>

Basically, I need this HTML to look like the previous example (HTML and CSS). 基本上,我需要此HTML看起来像前面的示例(HTML和CSS)。 How can I do this? 我怎样才能做到这一点?

The width of the floated element is constant, but the height can change. 浮动元素的宽度是恒定的,但是高度可以改变。 The content has to flow around it. 内容必须围绕它流动。 The reason I need to have it this way is because the floated element is the menu, which doesn't contain any important text and is usually the same for many pages, so the content should be topmost in the code. 之所以需要这种方式,是因为float元素是菜单,它不包含任何重要的文本,并且通常对于许多页面都是相同的,因此内容应位于代码的最顶层。

Simple you have add the following css 很简单,您添加了以下CSS

#content {
    float: left;
    width: 300px; /* put here the width you want */
}

demo: http://jsfiddle.net/qTDLr/1/ 演示: http//jsfiddle.net/qTDLr/1/

Edit: make sure that the sum of #content and #menu width is less than the container width. 编辑:确保#content和#menu宽度的总和小于容器宽度。

This recent question may be the same 最近的问题可能是一样的

Wrap text around right floated column where left column appears first in html 将文字环绕在右浮动列上,其中左列首先出现在html中

the solution involves floating a empty "spacer" div right , this spacer is first in source, it should have the width and height of the content to be in the right side - in the link a solution including a bit of jQuery to get the height - the position the actual menu over the top of the floated spacer 该解决方案涉及向右浮动一个空的“ spacer” div ,此spacer首先出现在源代码中,其内容的宽度和高度应在右侧-链接中的解决方案包括一些jQuery以获取高度-实际菜单在浮动间隔条上方的位置

a JS fiddle example produced from that link : HERE 从该链接生成的JS小提琴示例: 此处

You could just use a table. 您可以只使用一张桌子。 This 'sidebar before content' problem of CSS has been a huge step backwards in terms of accessibility. CSS的“内容前侧边栏”问题在可访问性方面已经倒退了一大步。

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

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