Help:CSS
- Help:CSS
Introduction to CSS in MediaWiki
Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML. In the context of MediaWiki, CSS allows you to customize the look and feel of your wiki—from the colors and fonts to the layout and positioning of elements. While MediaWiki provides a default appearance, CSS empowers you to tailor the wiki's visual presentation to match your specific needs and preferences, creating a unique and branded experience. This guide will provide a comprehensive introduction to CSS within the MediaWiki environment, geared towards beginners.
Why Use CSS in MediaWiki?
There are several compelling reasons to learn and utilize CSS in your MediaWiki installation:
- **Customization:** CSS allows for extensive customization beyond the available wiki settings. You can change almost any visual aspect of the wiki.
- **Consistency:** By defining styles in CSS, you ensure a consistent look and feel across all pages, improving user experience.
- **Branding:** CSS enables you to incorporate your organization's branding elements, such as logos, colors, and fonts.
- **Accessibility:** Well-written CSS can improve the accessibility of your wiki for users with disabilities. Consider using semantic HTML and appropriate color contrast ratios.
- **Maintainability:** Centralized style definitions in CSS make it easier to maintain and update the wiki's appearance. Changes to the CSS cascade throughout the entire wiki, rather than needing to be applied to each page individually.
- **Responsiveness:** CSS can be used to create responsive designs that adapt to different screen sizes and devices. This is crucial for modern web browsing habits. (See Help:Mobile access for more context).
Where to Add CSS in MediaWiki
MediaWiki provides several locations where you can add CSS rules. The most common and recommended methods are:
- **`MediaWiki:Common.css`:** This is the *global* stylesheet. Changes made here affect *all* users and all skins on the wiki. This is generally the best place for core customizations that should apply to everyone. Requires administrator privileges.
- **`MediaWiki:SkinName.css`:** This stylesheet applies only to a specific skin (e.g., `MediaWiki:Vector.css` for the Vector skin). This allows you to customize the appearance of individual skins without affecting others. Requires administrator privileges. Using skin-specific CSS is useful for tailoring the look and feel for different user preferences.
- **`User:YourUsername/vector.css` (or equivalent for other skins):** This is a *user-specific* stylesheet. Changes made here affect only *your* view of the wiki. This is ideal for personal preferences or customizations that don't need to be shared with others. You can create this page yourself without administrator privileges. The filename will change depending on the skin you are using; for example, `User:YourUsername/monobook.css` for the Monobook skin.
- **Page-Specific CSS (using `<style>` tag):** You can embed CSS directly within a wiki page using the `<style>` tag. This is generally discouraged for global styles, as it can make maintenance difficult. However, it can be useful for applying styles to specific content on a single page. Be mindful of the performance implications of inline styles.
Basic CSS Syntax
CSS rules are composed of two main parts: a *selector* and a *declaration block*.
```css selector {
property: value; property: value;
} ```
- **Selector:** The selector identifies the HTML element(s) to which the style will be applied. Selectors can be based on element type (e.g., `p` for paragraphs), class name (e.g., `.my-class`), ID (e.g., `#my-id`), or a combination of these.
- **Declaration Block:** The declaration block contains one or more declarations, separated by semicolons. Each declaration consists of a *property* and a *value*. The property specifies the aspect of the element to be styled (e.g., `color`, `font-size`, `background-color`), and the value specifies the desired setting for that property (e.g., `red`, `16px`, `#ffffff`).
Common CSS Selectors
Understanding CSS selectors is crucial for targeting the elements you want to style. Here are some common selectors:
- **Element Selector:** Selects all elements of a specific type.
* Example: `p { color: blue; }` (Selects all paragraphs and sets their color to blue.)
- **Class Selector:** Selects all elements with a specific class attribute. Class names are prefixed with a dot (`.`).
* Example: `.important { font-weight: bold; }` (Selects all elements with the class "important" and sets their font weight to bold.)
- **ID Selector:** Selects a single element with a specific ID attribute. ID names are prefixed with a hash (`#`). IDs should be unique within a page.
* Example: `#header { background-color: #f0f0f0; }` (Selects the element with the ID "header" and sets its background color.)
- **Attribute Selector:** Selects elements based on their attributes.
* Example: `a[href^="https://"] { color: green; }` (Selects all `<a>` elements with an `href` attribute that starts with "https://" and sets their color to green.)
- **Descendant Selector:** Selects elements that are descendants of another element.
* Example: `div p { color: red; }` (Selects all paragraphs that are inside a `
- **Child Selector:** Selects elements that are direct children of another element.
- ` element and sets their list style type to square.)
- **Adjacent Sibling Selector:** Selects an element that is immediately preceded by another element.
- **General Sibling Selector:** Selects all sibling elements that follow another element.
- **`color`:** Sets the text color. (e.g., `color: red;`)
- **`background-color`:** Sets the background color. (e.g., `background-color: #f0f0f0;`)
- **`font-size`:** Sets the font size. (e.g., `font-size: 16px;`)
- **`font-family`:** Sets the font family. (e.g., `font-family: Arial, sans-serif;`)
- **`font-weight`:** Sets the font weight (e.g., `bold`, `normal`). (e.g., `font-weight: bold;`)
- **`text-align`:** Sets the text alignment (e.g., `left`, `right`, `center`, `justify`). (e.g., `text-align: center;`)
- **`margin`:** Sets the margin around an element. (e.g., `margin: 10px;`)
- **`padding`:** Sets the padding inside an element. (e.g., `padding: 5px;`)
- **`border`:** Sets the border around an element. (e.g., `border: 1px solid black;`)
- **`width`:** Sets the width of an element. (e.g., `width: 200px;`)
- **`height`:** Sets the height of an element. (e.g., `height: 100px;`)
- **`display`:** Controls how an element is displayed (e.g., `block`, `inline`, `none`). (e.g., `display: none;`)
- **`float`:** Positions an element to the left or right. (e.g., `float: left;`)
- **`position`:** Specifies the positioning method (e.g., `static`, `relative`, `absolute`, `fixed`). (e.g., `position: absolute;`)
- **`visibility`:** Controls the visibility of an element (e.g., `visible`, `hidden`). (e.g., `visibility: hidden;`)
- **Specificity:** Determines which CSS rule takes precedence when multiple rules conflict. More specific selectors have higher priority. The order of specificity (from highest to lowest) is:
- **Cascade:** Refers to the order in which CSS rules are applied. Rules defined later in the stylesheet have higher priority than rules defined earlier, *as long as they have the same specificity*. User-specific styles override skin-specific styles, which override global styles.
- **Browser Developer Tools:** Use your browser's developer tools (usually accessed by pressing F12) to inspect elements, view applied CSS rules, and modify styles in real-time. This is the most effective debugging method.
- **Cache Clearing:** Browser caching can sometimes prevent you from seeing the latest CSS changes. Clear your browser's cache and cookies. Also, clear the MediaWiki server cache (if you have access).
- **Syntax Errors:** Check for syntax errors in your CSS code. Even a small error can prevent the entire stylesheet from loading. Use a CSS validator to identify errors.
- **Specificity Issues:** If a style isn't being applied as expected, check the specificity of the conflicting rules. Make your selector more specific if necessary.
- **Commenting:** Comment out sections of your CSS code to isolate the problem area.
- **Inspect Element:** Right-click on the element you are trying to style and select "Inspect" (or similar option) in your browser to see which CSS rules are being applied to it.
- **Media Queries:** Allows you to apply different styles based on the characteristics of the device, such as screen size or orientation. (See Help:Mobile access for information on responsive design).
- **CSS Variables (Custom Properties):** Allows you to define reusable values in CSS.
- **CSS Preprocessors (Sass, Less):** Extend CSS with features like variables, nesting, and mixins. These are typically used in larger projects.
- **Flexbox and Grid Layout:** Powerful layout modules for creating complex and responsive designs.
- **Animations and Transitions:** Add visual effects to your wiki using CSS animations and transitions.
- **W3Schools CSS Tutorial:** [1](https://www.w3schools.com/css/default.asp)
- **MDN Web Docs CSS:** [2](https://developer.mozilla.org/en-US/docs/Web/CSS)
- **CSS Tricks:** [3](https://css-tricks.com/)
- **Can I use...:** [4](https://caniuse.com/) (Check browser compatibility for CSS features)
- **CSS Validator:** [5](https://jigsaw.w3.org/css-validator/)
- Help:Formatting
- Help:Skins
- Help:Extension:Scribunto (For more advanced styling possibilities)
- **Technical Analysis:** [6](https://www.investopedia.com/terms/t/technicalanalysis.asp)
- **Moving Averages:** [7](https://www.investopedia.com/terms/m/movingaverage.asp)
- **Relative Strength Index (RSI):** [8](https://www.investopedia.com/terms/r/rsi.asp)
- **MACD:** [9](https://www.investopedia.com/terms/m/macd.asp)
- **Bollinger Bands:** [10](https://www.investopedia.com/terms/b/bollingerbands.asp)
- **Fibonacci Retracement:** [11](https://www.investopedia.com/terms/f/fibonacciretracement.asp)
- **Candlestick Patterns:** [12](https://www.investopedia.com/terms/c/candlestick.asp)
- **Support and Resistance Levels:** [13](https://www.investopedia.com/terms/s/supportandresistance.asp)
- **Trend Lines:** [14](https://www.investopedia.com/terms/t/trendline.asp)
- **Chart Patterns:** [15](https://www.investopedia.com/terms/c/chartpattern.asp)
- **Elliott Wave Theory:** [16](https://www.investopedia.com/terms/e/elliottwavetheory.asp)
- **Ichimoku Cloud:** [17](https://www.investopedia.com/terms/i/ichimoku-cloud.asp)
- **Parabolic SAR:** [18](https://www.investopedia.com/terms/p/parabolicsar.asp)
- **Stochastic Oscillator:** [19](https://www.investopedia.com/terms/s/stochasticoscillator.asp)
- **Volume Weighted Average Price (VWAP):** [20](https://www.investopedia.com/terms/v/vwap.asp)
- **Average True Range (ATR):** [21](https://www.investopedia.com/terms/a/atr.asp)
- **Donchian Channels:** [22](https://www.investopedia.com/terms/d/donchianchannel.asp)
- **Heikin Ashi:** [23](https://www.investopedia.com/terms/h/heikinashi.asp)
- **Market Sentiment:** [24](https://www.investopedia.com/terms/m/marketsentiment.asp)
- **Trend Following:** [25](https://www.investopedia.com/terms/t/trendfollowing.asp)
- **Contrarian Investing:** [26](https://www.investopedia.com/terms/c/contrarianinvesting.asp)
- **Swing Trading:** [27](https://www.investopedia.com/terms/s/swingtrade.asp)
- **Day Trading:** [28](https://www.investopedia.com/terms/d/daytrade.asp)
- **Position Trading:** [29](https://www.investopedia.com/terms/p/positiontrading.asp)