简体   繁体   English

如何在GWT / Bootstrap中覆盖CSS样式?

[英]How can I override CSS styles in GWT/Bootstrap?

I just started to port twitter's bootstrap to GWT (see the github project here and a very ugly demo here ), but, I was having a log of issues with bootstrap styles vs Gwt styles. 我刚开始端口Twitter的引导GWT (见GitHub的项目在这里和一个非常难看的演示在这里 ),但是,我有一个日志的自举风格VS GWT风格的问题。

Bootstrap put a border-top in tr/td elements, and GWT components basically use tables everywhere. Bootstrap在tr / td元素中放置了一个边界,而GWT组件基本上在任何地方都使用表。 In the demo you can see that bug in the left VerticalPanel. 演示中,您可以在左侧VerticalPanel中看到该错误。

So, I was looking for a way to make GWT components ignore bootstrap styles, and I have no idea how to do this. 因此,我一直在寻找一种使GWT组件忽略引导程序样式的方法,而且我不知道该怎么做。

Is there a simple way to make it work right? 有没有简单的方法可以使其正常工作?

Thanks in advance. 提前致谢。

You can simply add a style to one of your root GWT objects and then simply override the bootstrap styles to remove those messy borders: 您可以简单地将样式添加到您的根GWT对象之一,然后简单地覆盖引导程序样式以消除这些混乱的边界:

<div class="gwt">
   ... some other GWT-content
</div>

and in your CSS: 并在您的CSS中:

.gwt tr, .gwt td {
    border-top: 0px;
}

Of course if you need to embed some bootstrap elements in your GWT elements then you will have to hack around and do: 当然,如果您需要在GWT元素中嵌入一些引导元素,那么您将不得不四处寻找并执行以下操作:

<div class="gwt">
   ... some other GWT-content
   <div class="bootstrap">...
        ... Bootstrap elements
   </div>
</div>

and in your CSS: 并在您的CSS中:

.bootstrap tr, .bootstrap td {
    border-top: 1px; // Whatever bootstrap style puts
}

It's possible, but somewhat complex to do something with a Linker in GWT. 在GWT中使用链接器可以执行某些操作,但有些复杂。 The high-level idea would be: 高层次的想法是:

  1. Put all your GWT components in a <div id="gwt">...</div> 将所有GWT组件放入<div id =“ gwt”> ... </ div>
  2. Add a linker to the GWT Module file that will process CSS files. 将链接器添加到将处理CSS文件的GWT模块文件。
  3. In the linker, transform the GWT CSS (eg, standard.css) to insert a #gwt before each selector rule. 在链接器中,转换GWT CSS(例如standard.css)以在每个选择器规则之前插入#gwt。

The first part is easy, just add an id to your root element. 第一部分很简单,只需向根元素添加一个id。

The second part is also easy, simply add code that looks like this to your Module.gwt.xml file: 第二部分也很简单,只需向Module.gwt.xml文件中添加如下所示的代码:

<define-linker name="cssLinker" class="com.you.bootstrap.linker.CssRenamingLinker" />
<add-linker name="cssLinker"/>

The hard part is implementing the Linker. 困难的部分是实现链接器。 It's possible to do parse it by hand, but you might find it easier to use something like SAC . 可以手动解析它,但是您可能会发现使用SAC之类的东西更容易。

Using the Linker, you can transform your CSS by inserting a #gwt before each selector. 使用链接器,可以通过在每个选择器之前插入#gwt来转换CSS。 Using SAC, you might do that by overriding all the DocumentHandler methods to simply emit each of their arguments to an OutputStream. 使用SAC,您可以通过重写所有DocumentHandler方法以将其每个参数简单地发送到OutputStream来实现。 In DocumentHandler.startSelector() you would first emit "#gwt " before each selector. 在DocumentHandler.startSelector()中,您将首先在每个选择器之前发出“ #gwt”。

[Edit] This assumes that GWT's standard.css defines styles that override the bootstrap styles. [编辑]假设GWT的standard.css定义了覆盖引导样式的样式。 If not, you might have to 'enhance' the GWT CSS with defaults. 如果没有,您可能必须使用默认设置来“增强” GWT CSS。 There's a list of W3C recommended defaults here . 有W3C推荐的缺省值的列表在这里

The benefit is that this is future-resistant - if GWT styles change or if bootstrap styles change, this should be robust. 这样做的好处是可以防止将来发生变化-如果GWT样式更改,或者引导样式更改,则该标记应很健壮。

Hope that helps, 希望能有所帮助,

Adam 亚当

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

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