Skip to content

Commit d1a5d6c

Browse files
authored
Basic Styling (ChartsCSS#47)
* Basic Styling: reverse sections * Basic Styling: Style with CSS variables
1 parent 8a027c8 commit d1a5d6c

File tree

1 file changed

+89
-49
lines changed

1 file changed

+89
-49
lines changed

src/customization/basic-styling.md

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,6 @@ prev: ../
77

88
Charts are styled using regular CSS rules. You can apply custom styles on any HTML element or [chart layer](../docs/anatomy/) inside the `<table>` element.
99

10-
## Style Specific Elements
11-
12-
To style specific elements, use any method supported by CSS.
13-
14-
### CSS Selectors
15-
16-
Use CSS `:nth-of-type()` pseudo-class to match the nth data CSS based on the position among siblings.
17-
18-
```html
19-
<tbody>
20-
<tr>
21-
<td> 20 </td>
22-
</tr>
23-
<tr>
24-
<td> 40 </td>
25-
</tr>
26-
<tr>
27-
<td> 60 </td>
28-
</tr>
29-
<tbody>
30-
```
31-
32-
```css
33-
#my-chart tbody tr:nth-of-type(2) td {
34-
font-weight: bold;
35-
}
36-
```
37-
38-
### CSS Classes
39-
40-
Use custom classes on the element you want to style, and apply custom styles only on that CSS class.
41-
42-
```html
43-
<tr>
44-
<td> 20 </td>
45-
<td> 40 </td>
46-
<td> 60 </td>
47-
<td> 80 </td>
48-
<td class="highlight"> 100 </td>
49-
</tr>
50-
```
51-
52-
```css
53-
.highlight {
54-
font-weight: bold;
55-
}
56-
```
57-
5810
## Style Chart Elements
5911

6012
To style specific chart elements or layers, use one of the HTML tags - `<caption>`, `<tbody>`, `<tr>`, `<th>` or `<td>`.
@@ -121,7 +73,7 @@ To control the chart width/height ratio, use one of the following selectors.
12173

12274
```css
12375
#my-chart .bar tbody {
124-
aspect-ratio: none;
76+
aspect-ratio: auto;
12577
}
12678

12779
#my-chart .column tbody {
@@ -140,3 +92,91 @@ To control the chart width/height ratio, use one of the following selectors.
14092
aspect-ratio: 1 / 1;
14193
}
14294
```
95+
96+
## Style Specific Elements
97+
98+
To style specific elements, use any method supported by CSS.
99+
100+
### CSS Selectors
101+
102+
Use CSS `:nth-of-type()` pseudo-class to match the nth data CSS based on the position among siblings.
103+
104+
```html
105+
<tbody>
106+
<tr>
107+
<td> 20 </td>
108+
</tr>
109+
<tr>
110+
<td> 40 </td>
111+
</tr>
112+
<tr>
113+
<td> 60 </td>
114+
</tr>
115+
<tbody>
116+
```
117+
118+
```css
119+
#my-chart tbody tr:nth-of-type(2) td {
120+
font-weight: bold;
121+
}
122+
```
123+
124+
### CSS Classes
125+
126+
Use custom classes on the element you want to style, and apply custom styles only on that CSS class.
127+
128+
```html
129+
<tr>
130+
<td> 20 </td>
131+
<td> 40 </td>
132+
<td> 60 </td>
133+
<td> 80 </td>
134+
<td class="highlight"> 100 </td>
135+
</tr>
136+
```
137+
138+
```css
139+
.highlight {
140+
font-weight: bold;
141+
}
142+
```
143+
144+
## Style with CSS Variables
145+
146+
**Charts.css** offers several [custom CSS variables](../development/supported-features/#variables) to style different chart elements.
147+
148+
Use CSS variables on different chart types.
149+
150+
```css
151+
#my-chart .bar {
152+
--color: red;
153+
--labels-size: 150px;
154+
--labels-align-inline: start;
155+
--labels-align-block: center;
156+
}
157+
158+
#my-chart .column {
159+
--primary-axis-color: blue;
160+
--primary-axis-width: 3px;
161+
}
162+
163+
#my-chart .area {
164+
--data-axes-color: grey;
165+
}
166+
167+
#my-chart .line {
168+
--line-size: 3px;
169+
--color: black;
170+
--labels-size: 3rem;
171+
}
172+
173+
#my-chart .pie {
174+
--primary-axis-color: blue;
175+
}
176+
177+
#my-chart .legend {
178+
--legend-border-width: 2px;
179+
--legend-border-style: solid;
180+
--legend-border-color: brown;
181+
}
182+
```

0 commit comments

Comments
 (0)