Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS check for Chrome, IE, Firefox

Tags:

html

css

I've noticed a small alignment issue in the three major browsers when rendering my web site. As such how can I perform the following pseudo-code with pure CSS?

if Webkit (Safari/Chrome) {
    #myDIV {margin-top:-3px}
} else if (Firefox) {
    #myDIV {margin-top:0px}
} else { // IE and other browsers
    #myDIV {margin-top:1px}
}
like image 397
hankh Avatar asked May 17 '10 02:05

hankh


People also ask

How can I see the CSS code in Chrome?

Press Ctrl + Shift + i for Windows/Linux (or command + option + i for Mac). Right-click on an element on your website page and select Inspect. Now that you are familiar with accessing Google Chrome Developer Tools, you will be able to inspect CSS elements to modify them live.

How do I use CSS only for Internet Explorer?

#2 CSS Rules Specific to Explorer (IE CSS hacks) IE8 or below: to write CSS rules specificially to IE8 or below, add a backslash and 9 ( \9 ) at the end before the semicolon. IE7 or below: add an asterisk ( * ) before the CSS property. IE6: add an underscore ( _ ) before the property.


2 Answers

If its only one property, you don't need conditional comments for including other files. But if you want it that way, do it like SLaks already wrote. Another approach is just to add a property at the end of your specific class with a browser specific hack.

.foo {
  margin-top: 5px; // general property
  _margin-top: 2px; //IE 6 and below
}

Or you seperate your classes and add under your general class a browser specific class.

/* general */
foo {
   width : 20em;
}

/* IE 6 */
* html foo {
   width : 27em;
}

/* IE 7 */
* + html foo {
   width : 29em;
}
like image 149
ChrisBenyamin Avatar answered Sep 28 '22 01:09

ChrisBenyamin


You can use a CSS Hack. However, I wouldn't recommend it.

For IE, you can include a separate CSS file or <style> tag inside a conditional comment, like this:

<!--[if IE] <tag> <![endif]-->
like image 38
SLaks Avatar answered Sep 28 '22 01:09

SLaks