Mada za sehemu hiiBuilding the front-end using a frameworkMada 3
- Implementing components and layouts
- Performing customisation of styles
- Implementing user interactions
Customising Styles in React
Styling makes web applications visually appealing by controlling how elements appear on screen, including colors, fonts, spacing, and layout. In React, you can apply styles using external CSS files, inline styles, or CSS modules.
When you build a website or application, the content alone is not enough. Styling helps users navigate easily, creates a consistent look across pages, and makes the application professional and user-friendly. For example, a well-styled student registration form with clear labels and buttons is easier to use than one with plain, unformatted text.
React provides three main ways to apply styles to components. Each method has its own advantages and use cases.
1. External CSS
External CSS involves writing styles in a separate .css file and importing it into your component. This method keeps your code clean and organized because the styling code is separated from the JavaScript logic.
How to use external CSS:
- Create a CSS file in your project folder (for example,
Welcome.css) - Write your styles in that file
- Import the CSS file into your React component
Example:
/* Welcome.css */
h1 {
color: blue;
text-align: center;
}
// Welcome.js
import React from 'react';
import './Welcome.css';
function Welcome() {
return <h1>Welcome to React</h1>;
}
export default Welcome;
In this example, the Welcome.css file contains the styling rules, and the Welcome.js component imports and uses those styles automatically.
2. Inline Styles
Inline styles are written directly inside the component using the style attribute. The styles are written as JavaScript objects, where property names use camelCase instead of hyphenated names (for example, textAlign instead of text-align).
Example:
// Welcome.js using inline styles
import React from 'react';
function Welcome() {
return (
<h1 style={{ color: 'green', textAlign: 'center' }}>
Welcome to React
</h1>
);
}
export default Welcome;
In this example, the style is applied directly to the <h1> element using the style attribute. Note that the value is a JavaScript object with curly braces {{ }} — the outer braces inject JavaScript into JSX, and the inner braces create the object.
Key differences from external CSS:
- Property names must be in camelCase:
backgroundColor, notbackground-color - Values are strings:
'center', not'center' - Units are written as strings:
'20px', not20px
3. CSS Modules
CSS modules allow styles to be scoped to a specific component, which prevents style conflicts in large applications. This is useful when multiple components might use the same class names.
Example:
/* Welcome.module.css */
.title {
color: purple;
font-size: 24px;
}
// Welcome.js using CSS Modules
import React from 'react';
import styles from './Welcome.module.css';
function Welcome() {
return <h1 className={styles.title}>Welcome to React</h1>;
}
export default Welcome;
CSS modules are more advanced and are typically used in larger projects. For beginners, external CSS and inline styles are easier to start with.
Let us walk through creating a styled card component called MountainCard that displays information about Mount Kilimanjaro.
Step 1: Create the component file (MountainCard.js)
// MountainCard.js
import React from 'react';
import './Mountain.css';
function MountainCard() {
const name = "Mount Kilimanjaro";
const description = "The highest mountain in Africa";
const height = "5,895 metres";
return (
<div className="mountain">
<div className="mountain-info">
<h2>{name}</h2>
<p><strong>Description:</strong> {description}</p>
<p><strong>Height:</strong> {height}</p>
</div>
</div>
);
}
export default MountainCard;
Step 2: Create the CSS file (Mountain.css)
.mountain {
width: 300px;
margin: 20px auto;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
border-radius: 10px;
overflow: hidden;
font-family: 'Segoe UI', sans-serif;
background-color: #fff;
}
.mountain-info {
padding: 15px;
text-align: center;
}
.mountain-info h2 {
color: #003366;
margin-bottom: 10px;
}
.mountain-info p {
font-size: 16px;
margin: 5px 0;
color: #555;
}
Step 3: Use the component in App.js
import React from 'react';
import MountainCard from './MountainCard';
function App() {
return (
<div>
<MountainCard />
</div>
);
}
export default App;
When you run this application with npm start, you will see a clean, styled card displaying information about Mount Kilimanjaro with a shadow, rounded corners, and proper spacing.
Once you understand the basics, you can customise styles by:
- Changing colors: Modify properties like
color,backgroundColor, orborder - Adjusting spacing: Use
margin,padding, orgap - Changing fonts: Set
font-family,font-size, orfont-weight - Adding effects: Use
box-shadow,border-radius, oropacity
For example, to change the card background to a light blue, you would modify the .mountain class in the CSS file:
.mountain {
background-color: #e6f2ff; /* Light blue */
/* other styles remain the same */
}
| Method | How it works | Best for |
|---|---|---|
| External CSS | Import a separate .css file | Most projects, keeps code organized |
| Inline styles | Use style attribute with object | Quick testing, small adjustments |
| CSS modules | Import CSS as object with unique class names | Large projects, preventing conflicts |
In Tanzania, web developers use style customisation when building school management systems, e-commerce websites for local businesses, or student portals. For example, when developing an online result-checking system for a secondary school in Dar es Salaam, a developer would customise the colours and layout to match the school's branding, making the portal look professional and trustworthy for parents checking their children's exam results.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Customizestyles using CSS, inline styles, or framework-specific utilities.
- Apply consistent themes across components.
Tathmini ya matokeo
A webpage with customized styles, including modified colors, typography, and spacing, adhering to a theme or branding.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how to
- use of CSS frameworks (e.g., Tailwind, Bootstrap) for styling.
Principles: The student should explain the principles of:
- best practices for styling.
Theories: The student should explain:
- concepts of separation of concerns in design (content vs. presentation).
Circumstantial knowledge:
Detailed knowledge about:
- trade-offs between inline styles and external stylesheets
- handling conflicting styles.
Swali
In React, how do you apply styles using the external CSS method?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado