简体   繁体   English

将页面内容推送到左浮动div以下

[英]Push page content below left-floated div

Some pages of a site I am working on use a script to automatically create a table of contents based on a page's headers. 我正在处理的网站的某些页面使用脚本根据页面的标题自动创建目录。 However, I'm having a bit of an issue getting the "toc" div to push the rest of the page's content below it. 但是,让“ toc” div将页面的其余内容推到它下面时,我遇到了一个问题。

The TOC div is within the innermost div on my page, "content". TOC div位于我页面上最里面的div中,即“内容”。 For this reason, I haven't been able to force my content div below the TOC with clear: both; 出于这个原因,我无法将我的内容div强制设置在TOC之下,并且要clear: both; as would be usual. 像往常一样。 I don't want to move it outside "content" because I want the TOC to be below the page's H1 header, and I want to avoid <div></div> spam as much as possible. 我不想将其移到“内容”之外,因为我希望目录位于页面的H1标头下方,并且我想尽可能避免<div></div>垃圾邮件。

Below is a jsfiddle with a simple layout showing what I mean. 下面是一个简单的jsfiddle,显示了我的意思。 Note that the TOC has a background color. 请注意,TOC具有背景色。 If I make its width 100%, it works, but the color stretches, which is not what I want. 如果我将其宽度设置为100%,则可以使用,但是颜色会拉伸,这不是我想要的。

http://jsfiddle.net/n3yKf/2/ http://jsfiddle.net/n3yKf/2/

I will probably be changing the script that generates the TOC as I'm not overly fond of the script using <a> titles. 我可能会更改生成TOC的脚本,因为我不太喜欢使用<a>标题的脚本。 However, my issue is with CSS so I don't think I'll get too much into that right now. 但是,我的问题与CSS有关,所以我认为现在我不会对此进行过多讨论。

Suggestions on good ways to create a better script are welcome (hopefully with source code; I hate black box solutions) all the same though. 虽然如此,但仍欢迎提出有关创建更好的脚本的好的方法的建议(希望有源代码;我讨厌黑盒解决方案)。

So why not just get rid of the float: left ? 那么,为什么不摆脱float: left呢? Why are you floating it if you want all the other content to display below? 如果要在下面显示所有其他内容,为什么要浮动它? Just get rid of float and throw a width on it. 只是摆脱浮标,并在其上扔一个宽度。

#toc {
    background: #999;
    border: 1px solid black;
    padding: 0 1em 1em;
    width: 45%;
}

there a re couple of things you can do 您可以做几件事

you can add the same class to each of your h2 tags and then apply the clear both to that class in the css; 您可以将相同的类添加到每个h2标签中,然后将清除都应用到CSS中的该类;

h2 {
    clear:both;
}

also you could wrap your content in a separate div and then apply the clear both to that div 您也可以将内容包装在单独的div中,然后将清除内容都应用于该div

also i modified your jsfiddle to iterate the jQuery each with a variable so you only have to append() one time by concatenating all the anchor tags. 我也修改了您的jsfiddle,使每个jQuery都使用一个变量进行迭代,因此您只需通过串联所有的anchor标签一次append()一次。

http://jsfiddle.net/n3yKf/5/ http://jsfiddle.net/n3yKf/5/

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

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