Is there a way to use embedding style syntax for simplifying ( adding namespace to ) multiple class selectors in SCSS? - class

I have icon classes that contain the background-image and background-size properties. I want to protect the names of these classes with the prefix .icon. Then I can start writing out .icon.profile, .icon.search etc. selectors. If it were .icon .profile, .icon .search etc., then I could use embedding is SCSS to neatly protect namespace. However embedding won't work for multiple class selectors, because it selects child nodes. Once I use SCSS, it feels unintelligent to write out the prefix every time. To use embedding in DOM to support embedding in SCSS is an overkill and expensive ( adds unnecessary complexity of DOM elements ).
Is there a way to add "namespace" to classes that compile to multiple class selectors.
//////////////////////////////////////////////////////////////
// icons
.icon.search {
background-image: url(...);
background-size: cover;
}
.icon.profile {
background-image: url(...);
background-size: cover;
}
//////////////////////////////////////////////////////////////

If I understand your problem correctly, what you are looking for is the parent selector &:
.icon {
&.search {
background-image: url(...);
background-size: cover;
}
&.profile {
background-image: url(...);
background-size: cover;
}
}

Related

class overrule when two classes assigned to one div

I was creating a <div> tag in which I wanted to apply two classes for a <div> tag which would be a thumbnail gallery. One class for its position and the other class for its style. This way I could apply the style, I was having some strange results which brought me to a question.
Can two classes be assigned to a <div> tag? If so, which one overrules the other one or which one has priority?
Multiple classes can be assigned to a div. Just separate them in the class name with spaces like this:
<div class="rule1 rule2 rule3">Content</div>
This div will then match any style rules for three different class selectors: .rule1, .rule2 and .rule3.
CSS rules are applied to objects in the page that match their selectors in the order they are encountered in the style sheet and if there is a conflict between two rules (more than one rule trying to set the same attribute), then CSS specificity determines which rule takes precedence.
If the CSS specificity is the same for the conflicting rules, then the later one (the one defined later in the stylesheet or in the later stylesheet) takes precedence. The order of the class names on the object itself does not matter. It is the order of the style rules in the style sheet that matters if the CSS specificity is the same.
So, if you had styles like this:
.rule1 {
background-color: green;
}
.rule2 {
background-color: red;
}
Then, since both rules match the div and have exactly the same CSS specificity, then the second rule comes later so it would have precedence and the background would be red.
If one rule had a higher CSS specificity (div.rule1 scores higher than .rule2):
div.rule1 {
background-color: green;
}
.rule2 {
background-color: red;
}
Then, it would take precedence and the background color here would be green.
If the two rules don't conflict:
.rule1 {
background-color: green;
}
.rule2 {
margin-top: 50px;
}
Then, both rules will be applied.
Actually, the class that defined last in the css - is applied on your div.
check it out:
red last in css
.blue{ color: blue; }
.red { color: red; }
<div class="blue red">blue red</div>
<div class="red blue">red blue</div>
vs
blue last in css
.red { color: red; }
.blue{ color: blue; }
<div class="blue red">blue red</div>
<div class="red blue">red blue</div>
If you asking about they have same property then as per the CSS rule it's take the last statement.
<div class="red green"></div>
CSS
.red{
color:red;
}
.green{
color:green;
}
As per the above example it's take the last statement as per css tree which is .green.
The class that is defined last in the CSS have priority, if nothing else applies.
Read up on CSS priority to see how it works.
Many classes can be assigned to an element, you just separate them with a space
<div class="myClass aSecondClass keepOnClassing stayClassySanDiego"></div>
Because of the cascade in CSS, the overwriting rules closest the to bottom of the document will be applied to the element.
So if you have
.myClass
{
background: white;
color: blue;
}
.keepOnClassing
{
color: red;
}
The red color will be used, but not the background color as it was not overwritten.
You must also take into account CSS specificity, if you have a more specific selector, this one will be used:
.myClass
{
background: white;
color: blue;
}
div.myClass.keepOnClassing
{
background: purple;
color: red;
}
.stayClassySanDiego
{
background: black;
}
The second selector here will be used as it is more specific.
You can take a look at it all here.

Override GWT theme body margin css attribute?

I'm using GWT. I see that the gwt "clean" theme (the default one?) makes our body element have a 10px margin:
body {
color: black;
margin: 10px; <------
border: 0px;
padding: 0px;
background: #fff;
direction: ltr;
}
In my own css file, I set the margin to 0px, but it seems that GWT's keeps winning (maybe because it gets loaded last?).
What's the right way to override their setting?
Thanks
There are several possibilities:
You can use margin: 0px !important (this is the "brute-force" approach).
Or you can give your body a class like <body class="myApp">...</body>, and then in your CSS, use body.myApp { ... }. This will take precedence, because body.myApp is a more specific selector than body.
Or you can simply not use any theme at all (which is often a good idea if you want to create a fresh layout without worrying which attributes you'll have to override)
Another option is to load your css file by using clientbundle. (assume that playground.css is your css file)
public interface Resources extends ClientBundle {
public static Resources INSTANCE = GWT.create(Resources.class);
#Source("playground.css")
CssResource getPlaygroundCSS();
}
Note: playground.css is located in the same package as the Resources interface.
in the onmoduleload:
public class Playground implements EntryPoint {
#Override
public void onModuleLoad() {
Resources.INSTANCE.getPlaygroundCSS().ensureInjected();
Label lblHelloWorld = new Label("Hello World");
RootPanel.get().add(lblHelloWorld);
}
}
In the CSS:
body {
background-color: #FFFFD2 !important; }
works fine to change the background color.

GWT - Datagrid Selection Color

is there a way to change global the selection color of gwt datagrid?
I added following css-format in the main-app-css file:
.dataGridSelectedRow {
background: #1EDA17;
color: white;
height: auto;
overflow: auto;
}
I have seen also following link:
http://code.google.com/intl/de-DE/webtoolkit/doc/latest/DevGuideUiCss.html
Sadly my change had no effect.
Do I miss any setStyleName() call?
There is also another way of setting custom css formatting for selected row in DataGrid. You need to create your custom interface that extends DataGrid.Resources. In this interface you should ovveride method dataGridStyle() and in #Source annotaion put path to your custom css file.
For example:
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.DataGrid.Resources;
public interface CustomDataGridResources extends Resources {
public interface CustomDataGridResources extends Resources {
#Source({DataGrid.Style.DEFAULT_CSS, "resources/CustomDataGridStyles.css"})
CustomStyle dataGridStyle();
interface CustomStyle extends DataGrid.Style {
}
}
If you want just to change style for selected row then your css file will contain only:
.dataGridSelectedRow {
background: #1EDA17;
color: white;
height: auto;
overflow: auto;
}
But I also prefer to change cursor for howered row:
.dataGridHoveredRow {
cursor: pointer;
cursor: hand;
}
Look also at similar discussion.
For applying custom style to your DataGrid you can use grid's constructor
public DataGrid(int pageSize, Resources resources, ProvidesKey<T> keyProvider)
where Resource is an instance that implements your custom interface (in my case CustomDataGridResources).
DataGrid.Resources customDataGridResources = GWT.create(CustomDataGridResources.class)
The custom css will not work since GWT overrides it with clean.css. If you use FIREBUG or any other tool you might recognize it. The solution is simple. Add !important to each line which has not affected with your custom css
.dataGridSelectedRow {
background: #1EDA17 !important;
color: white !important;
height: auto !important;
overflow: auto !important;
}

Look up GWT CellTable header style/s?

How can TH style name/s of a GWT CellTable's heading be looked up programatically?
I have looked at the Client Bundle documentation but it isn't immediately obvious to me how it all fits together. Thanks.
Not sure exactly what you want to do when accessing the TH style names.
If you want to override the standard css style of a celltable header, here are some of the css styles you can override to change the Look and Feel of the component.
.cellTableFirstColumnHeader {}
.cellTableLastColumnHeader {}
.cellTableHeader {
border-bottom: 2px solid #6f7277;
padding: 3px 15px;
text-align: left;
color: #4b4a4a;
text-shadow: #ddf 1px 1px 0;
overflow: hidden;
}
.cellTableSortableHeader {
cursor: pointer;
cursor: hand;
}
.cellTableSortableHeader:hover {
color: #6c6b6b;
}
.cellTableSortedHeaderAscending {
}
.cellTableSortedHeaderDescending {
}
Here is the complete list of styles for cellTables CellTable.css
Now if you want to access you header programmatically, you can use this solution to get the TableSectionElement corresponding the the Header of your table. Then you can access the row, then the cells, and lookup for their styles I guess.
Last thing if you want to override the header style, maybe you can use the following method when adding your column to your table
public void addColumn(Column<T, ?> col, Header<?> header)
Then create your Header or use a TextHeader for example then set your style on it before adding it to the table using
public void setHeaderStyleNames(String styleNames)
Example
TextHeader textHeader = new TextHeader("headerTitle");
textHeader.setHeaderStyleNames("my-style");
myTable.addColumn(myColumn, textHeader);
Easy solution:
import com.google.gwt.user.cellview.client.CellTable.Resources;
private String getCellTableHeaderStyle() {
Resources res = GWT.create(Resources.class);
return res.cellTableStyle().cellTableHeader();
}

GWT Vertical Tabs like iGoogle

I am using GWT and would like to develop a vertical tab panel like the one in iGoogle.
How can the same be achieved ?
I had the same problem and decided not to use third party library just for one small widget. Here is a lightweight solution I ended up using - it is based on tweaking styles.
verticaltabpanel.css:
#external gwt-TabLayoutPanel;
.gwt-TabLayoutPanel>div {
position: static !important;
}
.gwt-TabLayoutPanel {
margin-left: 30px;
}
#external gwt-TabLayoutPanelTabs;
.gwt-TabLayoutPanelTabs {
top: 0 !important;
width: 140px !important;
}
#external gwt-TabLayoutPanelTab;
.gwt-TabLayoutPanelTab {
display: block !important;
margin-top: 2px;
padding: 8px 6px !important;
}
#external gwt-TabLayoutPanelContentContainer;
.gwt-TabLayoutPanelContentContainer {
left: 150px !important;
top: 0 !important;
}
Add it to resources as usually:
public interface YouAppResources extends ClientBundle {
#Source("verticaltabpanel.css")
CssResource verticalTabPanelStyles();
}
Then inject it when your app starts:
resources.verticalTabPanelStyles().ensureInjected();
Define the tab panel in your templates like this:
<ui:style>
.tabPanel {
height: 400px;
width: 800px;
}
</ui:style>
<g:TabLayoutPanel addStyleNames="{style.tabPanel}" barUnit='PX' barHeight='0'>
</g:TabLayoutPanel>
Note that you have to set height and width and the style should be added not set.
The drawback of this approach is that all the tab panels in your application will be now vertical. If you need to have a horizontal one, you can use old TabPanel (note that it is deprecated). It is fine for my case as I have a number of vertical tab panels in my application and only one horizontal.
you can use ext-js's vertical tabs - see this demo http://iamtotti.com/playground/js/ext-3.1.1/examples/tabs/tabs.html
there is a gwt port of ext-js which you can use : http://code.google.com/p/gwt-ext/
Smart gwt also has a vertical tab implementation (its different to gwt-ext's) - http://www.smartclient.com/smartgwt/showcase and search for orientation on the left menu.
thx for your answer, megas.
One extension to make it possible to use some TabLayoutPanels with horizontal (original) and some with vertical (new) layout:
one could add ids (i.e. #myVertTab) to the css selectors.
I think what you're looking for is the TabLayoutPanel (scroll down a bit). It works great and it's a vanilla GWT widget, no third party libraries required.