English 中文(简体)
Use any CSS compiler (Sass, Less) to generate the selector [closed]
原标题:
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 7 years ago.

So I ve recently been playing around with CSS compilers, but I have no idea how (or if it s possible) to dynamically generate pieces of a selector.

For instance, let s say I wanted to make mixins to get

display: inline-block; 

to work cross browser. I would have to do the styles, yeah, but I would have to do the IE6/7 selector hacks to get them to work in those browsers too. Ideally I m looking for a one off thing to add to an element and have the ability for that to work. Some kind person recently gave me this solution: CSS compilers and converting IE hacks to conditional css

and it would be nice to implement that in a minimal way that would allow me to specify it for a given element and be on my way (for instance in Less, you can create a class with styles, pass that class to another element, and that element will inherit all of those styles. It would be nice to pass an element .inline-block; and it create the styles needed to support IE6/7 without having to resort to stuff like

_color: pink; 

Any ideas?

EDIT: for instance as well, how could I do something like clearfix for LESS? (lesscss.org)? If Sass can only do it then that will work too.

最佳回答

So I did some digging in the documentation, and some experimentation, and Sass (to my relief) supports this, though I don t think LESS does (which is why it s getting dropped in favor of Sass).

You can use the ampersand as a pointer to the selector you used in the definition, and the position of that ampersand in a nested declaration doesn t have to be at the front, you can put selectors before it. For instance, you can use conditional IE tags on to target IE in the same stylesheet in a way that doesn t use hacks. The following uses the SCSS notation (but should work fine with the indentation notation):

@mixin inline-block {

// Cross browser implementation of the inline-block attribute

  display: inline-block;
  body.ie6 & { display: inline; }
  body.ie7 & { display: inline; }

}

And all you have to do is drop this on anything you want:

#foo { @include inline-block; }

and it will generate/manage all the your inline-block stuff cross browser:

#foo { display: inline-block; }
body.ie6 #foo { display: inline; }
body.ie7 #foo { display: inline; }

Semantic way of handling cross-browser issues, no more having to deal with the cumbersome method of managing 2 different files for styles (which is asinine to me), no more code block eyesores.

Edit: Of course that functionality was stated in the old documentation, not in the new, which is why I couldn t find it.

问题回答

A cross-browser implementation of inline-block is provided by compass, a framework built on top of sass:

http://compass-style.org/docs/reference/compass/css3/inline_block/#mixin-inline-block-scss

http://jsfiddle.net/zsitro/G74pT/

Here you van see the crossbrowser solution for inline blocks. simple and short.

Above example will not give a layout to the element in IE. You need zoom for example.





相关问题
Image rendered with wrong colors in IE8

I have a gradient image as a PNG (no transparency) in a web-page. By taking a screenshot of Chrome showing the page and cropping the image from the page, I see the exact same colors are drawn as the ...

Determine Mobile Internet Explorer version

I need to determine the version of Mobile Internet Explorer on a Windows Mobile 6.1 device. So that I can report the same user-agent string as is being used by Mobile Internet Explorer. The user-...

CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

Internet Explorer only part loading JavaScript/CSS

I m having trouble with my local development environment where IE (6 through to 8) is only part loading JavaScript/CSS files. It throws random errors at random places in jquery.min.js every time I ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签