CSS Extensions – CSS Vendor Prefixes
CSS Vendor Prefixes
CSS vendor prefixes, sometimes called CSS browser prefixes, are used in CSS property names to implement experimental or pre-release CSS features that are not yet standardized or may have limited support in certain browsers. Vendor prefixes are used to identify specific browsers or browser versions that support these features.
Major browser vendors commonly use the following prefixes to implement nonstandard CSS features:
- -webkit- – Used for WebKit-based browsers, including Chrome, Safari, newer versions of Opera, and most iOS browsers (e.g., Firefox for iOS).
- -moz- – Used for Mozilla-based browsers, such as Firefox.
- -o- – Specific to older versions of Opera before it switched to WebKit.
- -ms- – Applied to Internet Explorer and early versions of Microsoft Edge.
According to the CSS specification, CSS vendor-specific extensions must start with a dash or underscore and have the following format:
['-' or '_'] + [vendor identifier] + ['-'] + [name]
For example, the -webkit-border-radius
is a vendor-prefixed property that allows you to round the corners of elements to create a visually appealing and modern design in WebKit-based browsers, such as Chrome.
Aspose CSS Extension – Java Example
The prefix that is used by Aspose.HTML library looks like -aspose- and gives you some experimental features. Following is a list of CSS functions that can be enabled by using -aspose- prefix:
Name | Description |
---|---|
currentPageNumber | This function returns the number of the current rendering page. |
totalPagesNumber | This function returns the number of the total pages in the document. |
The next code snippet demonstrates how to use CSS extensions to create custom marks on document margins:
- Initialize an instance of the Configuration class.
- Use the
getService()
method to fetch the User Agent service implementation from the configuration. - Use the
setUserStyleSheetU()
method to define CSS rules for page margins, content placement, and styling for page counters and titles.- Here is used the -aspose-content CSS property to define the content displayed in the bottom-right area. It includes the currentPageNumber() and the totalPagesNumber() functions, which are provided by the Aspose.HTML library to dynamically generate the current page number and a total number of pages.
- Also, the -aspose-content CSS extension defines the content displayed in the top-center area. It sets the content to the string “Hello, World Document Title!!!”.
- Initializes an HTML document using the HTMLDocument class.
- Create an XpsDevice instance and specify the output file path where the rendered document will be saved.
- Use the
renderTo(
device
) method to convert the HTML to XPS.
1// Initialize a configuration object
2Configuration configuration = new Configuration();
3
4// Get the User Agent Service
5IUserAgentService userAgent = configuration.getService(IUserAgentService.class);
6
7// Set a style of custom margins and create marks on it
8userAgent.setUserStyleSheet(
9 "@page {\n" +
10 " /* Page margins should be not empty in order to write content inside the margin-boxes */\n" +
11 " margin-top: 1cm;\n" +
12 " margin-left: 2cm;\n" +
13 " margin-right: 2cm;\n" +
14 " margin-bottom: 2cm;\n" +
15 " /* Page counter located at the bottom of the page */\n" +
16 " @bottom-right {\n" +
17 " -aspose-content: \"Page \" currentPageNumber() \" of \" totalPagesNumber();\n" +
18 " color: green;\n" +
19 " }\n" +
20 " /* Page title located at the top-center box */\n" +
21 " @top-center {\n" +
22 " -aspose-content: \"Hello, World Document Title!!!\";\n" +
23 " vertical-align: bottom;\n" +
24 " color: blue;\n" +
25 " }\n" +
26 "}");
27// Initialize an HTML document
28HTMLDocument document = new HTMLDocument("<div>Hello, World!!!</div>", ".", configuration);
29
30// Initialize an output device
31XpsDevice device = new XpsDevice("output.xps");
32
33// Send the document to the output device
34document.renderTo(device);
Recommendations
- Use vendor prefixes sparingly and only for required features, as modern browsers increasingly standardize CSS properties.
- Test your CSS across different browsers to ensure consistent behavior, especially when using prefixed properties.
You can download the complete examples and data files from GitHub.