简体   繁体   English

如何使用Twitter中的Bootstrap样式表和Rails中的现有样式表而不会搞乱一切?

[英]How do I use the Bootstrap stylesheet from Twitter with my existing stylesheet in Rails without messing up everything?

So I included the URL to the stylesheet in my Rails app, but it throws everything into haywire. 所以我在我的Rails应用程序中包含了样式表的URL,但它将所有内容都抛入了haywire。

I included it like this: 我把它包括在内:

<%= stylesheet_link_tag('http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css', 'style', 'css3buttons', 'jquery.lightbox-0.5', :media => 'all') %>

What I want to do, is to just have the Twitter styles applied when I apply the class to the html tag. 我想要做的是,当我将类应用于html标签时,应用Twitter样式。 Ideally, what I would like to do is have it even override the styles in my 'style' sheet - which is the stylesheet for my entire app. 理想情况下,我想要做的是让它甚至覆盖我的“样式”表中的样式 - 这是我整个应用程序的样式表。

Is it possible to do this in an easy way, without having to modify the style for every single element I want to work with? 是否有可能以简单的方式完成此操作,而无需为我想要使用的每个元素修改样式?

Look at http://twitter.github.com/bootstrap/1.4.0/bootstrap.css , you'll notice it defines resets and styles on root html elements. 看看http://twitter.github.com/bootstrap/1.4.0/bootstrap.css ,你会发现它定义了root html元素的重置和样式。

For example: 例如:

h1 {
  margin-bottom: 18px;
  font-size: 30px;
  line-height: 36px;
}

So, if your style.css already defines a style for h1 or if your layout expects the h1 to have a different margin, your layout is not going to look as expected. 因此,如果您的style.css已经为h1定义了样式,或者如果您的布局希望h1具有不同的边距,那么您的布局将不会像预期的那样。

Twitter bootstrap is generally for when you start a project and not something you can completely plug into an existing project's stylesheets without any problems. Twitter引导程序通常用于启动项目时,而不是可以完全插入现有项目的样式表而没有任何问题。

An alternative is to use their LESS stylesheets if you want to include, for example, just the forms and table styles. 另一种方法是使用它们的LESS样式表,如果你想要包括,例如,只包括表单和表格样式。 ( using Bootstrap with Less ). 使用Bootstrap with Less )。

<link rel="stylesheet/less" type="text/css" href="lib/tables.less">
<link rel="stylesheet/less" type="text/css" href="lib/forms.less">
<script src="less.js" type="text/javascript"></script>

You're not going to want to use lib/boostrap.less since includes everything, but you can include the other LESS stylesheets that work for you. 你不会想要使用lib/boostrap.less因为包含了所有内容,但你可以包含其他适合你的LESS样式表。 Be warned, just including a subset of the twitter boostrap, may not work as expected, especially if you don't have css resets defined. 请注意,仅包含twitter boostrap的子集,可能无法按预期工作,特别是如果您没有定义css重置。 If that doesn't work for you, look into using Preboot.less instead. 如果这对您不起作用,请考虑使用Preboot.less Note: Rails 3.1 supports LESS compilation 注意: Rails 3.1支持LESS编译

If you still want to use the complete twitter bootstrap css in your application, you can create a new rails layout template that uses the twitter bootstrap. 如果您仍想在应用程序中使用完整的twitter bootstrap css,则可以创建一个使用twitter引导程序的新的rails布局模板。 Then, build your new pages with the twitter boostrap, specify render :layout => "boostrap" or something similar to have your new pages use the twitter bootstrap layout. 然后,使用twitter boostrap构建新页面,指定render :layout => "boostrap"或类似的东西,让你的新页面使用twitter bootstrap布局。 Then, when you're ready, you can migrate your old pages to the new layout template. 然后,当您准备好后,您可以将旧页面迁移到新的布局模板。

I think you need to change order of css loading.. 我认为你需要改变css加载的顺序..

in your case to override your app stylesheet with bootstrap 在您的情况下,使用bootstrap覆盖您的应用程序样式表

first load your app stylesheet and then bootstrap.css 首先加载你的应用程序样式表然后再加载bootstrap.css

 <%= stylesheet_link_tag :default %>

and then 接着

 <%= stylesheet_link_tag  ('http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css', :media => 'all') %> 

You can simply customize the bootstrap from twitter github customization tool Customize Bootstrap 您可以从twitter github自定义工具Customize Bootstrap中定义引导程序

Select only items of your choice and download customized version. 仅选择您选择的项目并下载自定义版本。

I was also in search of something like this since long , Now I did and helped me alot. 很长一段时间我也在寻找这样的东西,现在我做了并帮助了我很多。

You can use less or sass mixins for this purpose, boostrap provides some nice ones. 你可以使用less或sass mixins来达到这个目的,boostrap提供了一些不错的选择。

For instance : 例如 :

<!- our new, semanticized HTML -->
<article>
  <section class="main">...</section>
  <aside></aside>
</article>

<!-- its accompanying Less stylesheet -->
article {
  .makeRow();

  section.main {
    .makeColumn(10);
  }

  aside {
    .makeColumn(2);
  }
}

See http://ruby.bvision.com/blog/please-stop-embedding-bootstrap-classes-in-your-html for some more details, and http://www.sitepoint.com/5-useful-sass-mixins-bootstrap/ and also https://github.com/twbs/bootstrap-sass/tree/master/assets/stylesheets/bootstrap/mixins 有关更多详细信息,请参阅http://ruby.bvision.com/blog/please-stop-embedding-bootstrap-classes-in-your-html ,以及http://www.sitepoint.com/5-useful-sass- mixins-bootstrap /还有https://github.com/twbs/bootstrap-sass/tree/master/assets/stylesheets/bootstrap/mixins

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

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