This is a great question about CSS architecture for migrating legacy applications. The user wants to create a scoped reset that:
- Works within a
.new-app container
- Resets inherited Bootstrap or Foundation styles
- Allows React components to have consistent styling
- Prevents CSS pollution from legacy code
This is a real-world problem many teams face when incrementally migrating from legacy systems to modern frameworks like React.
I'll create a comprehensive solution that includes:
- Reset CSS Architecture - A scoped reset that targets common Bootstrap/Foundation patterns
- CSS-in-JS and regular CSS examples
- Variables for consistency
- Protection from common issues
- Best practices and strategies
- Real examples with components
Let me design a complete solution:
CSS Architecture for Legacy Migration with Scoped Reset
I'll provide a comprehensive solution for isolating and resetting inherited styles from Bootstrap/Foundation.
1. Core Scoped Reset CSS
/* css/scoped-reset.css */
.new-app {
/* Re-establish default CSS variables and baseline */
--font-family-base: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
--font-size-base: 16px;
--line-height-base: 1.5;
--color-text: #212529;
--color-bg: #ffffff;
--color-border: #dee2e6;
--spacing-unit: 1rem;
/* Container reset */
all: revert;
box-sizing: border-box;
font-family: var(--font-family-base);
font-size: var(--font-size-base);
line-height: var(--line-height-base);
color: var(--color-text);
background-color: var(--color-bg);
text-align: left;
direction: ltr;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/* Scope all resets within .new-app */
.new-app * {
box-sizing: border-box;
}
.new-app *::before,
.new-app *::after {
box-sizing: border-box;
}
/* Reset all inherited properties */
.new-app *:not(svg):not(iframe) {
margin: 0;
padding: 0;
border: 0;
background-image: none;
background-color: transparent;
}
/* Typography reset */
.new-app p,
.new-app h1,
.new-app h2,
.new-app h3,
.new-app h4,
.new-app h5,
.new-app h6,
.new-app ul,
.new-app ol,
.new-app dl,
.new-app table,
.new-app blockquote,
.new-app pre,
.new-app code {
margin: 0;
padding: 0;
font-size: inherit;
font-weight: inherit;
line-height: inherit;
}
/* Headings */
.new-app h1,
.new-app h2,
.new-app h3,
.new-app h4,
.new-app h5,
.new-app h6 {
font-weight: 700;
margin: calc(var(--spacing-unit) * 0.5) 0;
line-height: 1.2;
}
.new-app h1 { font-size: 2rem; }
.new-app h2 { font-size: 1.75rem; }
.new-app h3 { font-size: 1.5rem; }
.new-app h4 { font-size: 1.25rem; }
.new-app h5 { font-size: 1.125rem; }
.new-app h6 { font-size: 1rem; }
/* List reset */
.new-app ul,
.new-app ol {
list-style: none;
}
.new-app li {
margin: 0;
padding: 0;
list-style: none;
}
/* Form elements reset */
.new-app button,
.new-app input,
.new-app select,
.new-app textarea,
.new-app optgroup,
.new-app option {
font-family: inherit;
font-size: inherit;
line-height: inherit;
color: inherit;
background-color: transparent;
border: 1px solid transparent;
margin: 0;
padding: 0;
}
.new-app button,
.new-app input[type="button"],
.new-app input[type="reset"],
.new-app input[type="submit"] {
cursor: pointer;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
.new-app input:focus,
.new-app textarea:focus,
.new-app select:focus {
outline: 2px solid var(--color-primary, #0066cc);
outline-offset: 2px;
}
/* Link reset */
.new-app a {
color: inherit;
text-decoration: none;
background-color: transparent;
cursor: pointer;
}
.new-app a:hover {
color: inherit;
text-decoration: inherit;
}
/* Image and media reset */
.new-app img,
.new-app svg,
.new-app video {
display: block;
max-width: 100%;
height: auto;
border: 0;
}
.new-app img {
vertical-align: middle;
}
/* Table reset */
.new-app table {
border-collapse: collapse;
border-spacing: 0;
}
.new-app th,
.new-app td {
padding: 0;
text-align: left;
vertical-align: top;
border-bottom: 1px solid var(--color-border);
}
/* Blockquote and code reset */
.new-app blockquote {
margin: var(--spacing-unit) 0;
padding: 0 var(--spacing-unit);
border-left: 4px solid var(--color-border);
}
.new-app code,
.new-app pre {
font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
font-size: 0.875em;
background-color: #f5f5f5;
border-radius: 3px;
}
.new-app code {
padding: 2px 6px;
}
.new-app pre {
padding: var(--spacing-unit);
overflow: auto;
}
.new-app pre code {
padding: 0;
background: transparent;
}
/* Utility resets for common Bootstrap/Foundation issues */
.new-app .container,
.new-app .row,
.new-app .col {
all: revert;
display: block;
width: 100%;
}
.new-app .clearfix::after {
display: none;
}
.new-app .visible,
.new-app .hidden {
visibility: visible;
display: auto;
height: auto;
width: auto;
overflow: visible;
}
/* Prevent Bootstrap/Foundation modals, alerts from showing */
.new-app .modal,
.new-app .alert-box {
display: none !important;
}
/* Screen reader text reset */
.new-app .sr-only,
.new-app .visuallyhidden {
position: static;
width: auto;
height: auto;
overflow: visible;
clip: auto;
}
2. CSS-in-JS Implementation (Styled-Components/Emotion)
// styles/scopedReset.js
import { createGlobalStyle } from 'styled-components';
const ScopedReset = createGlobalStyle`
.new-app {
/* CSS Variables */
--font-family-base: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto',
'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans',
'Helvetica Neue', sans-serif;
--font-size-base: 16px;
--line-height-base: 1.5;
--color-text: #212529;
--color-bg: #ffffff;
--color-border: #dee2e6;
--color-primary: #0066cc;
--spacing-unit: 1rem;
/* Base reset */
all: revert;
box-sizing: border-box;
font-family: var(--font-family-base);
font-size: var(--font-size-base);
line-height: var(--line-height-base);
color: var(--color-text);
background-color: var(--color-bg);
}
.new-app * {
box-sizing: border-box;
&::before,
&::after {
box-sizing: border-box;
}
}
/* Typography */
.new-app p, .new-app h1, .new-app h2, .new-app h3, .new-app h4, .new-app h5, .new-app h6 {
margin: 0;
padding: 0;
font-size: inherit;
line-height: inherit;
}
/* ... rest of reset styles ... */
`;
export default ScopedReset;
3. Advanced Reset with Theme Configuration
// utils/resetConfig.ts
export const RESET_CONFIG = {
// Bootstrap/Foundation specific classes to neutralize
neutralizeClasses: [
'container',
'container-fluid',
'row',
'col',
'col-*',
'btn',
'btn-*',
'form-control',
'form-group',
'alert',
'badge',
'card',
],
// Restore these to defaults
resetProperties: [
'margin',
'padding',
'border',
'background',
'color',
'font-size',
'font-weight',
'text-align',
'text-decoration',
'display',
'float',
'position',
'width',
'height',
],
// Variables to define
variables: {
'--font-family-base': '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
'--font-size-base': '16px',
'--line-height-base': '1.5',
'--color-text': '#212529',
'--color-bg': '#ffffff',
'--color-border': '#dee2e6',
'--spacing-unit': '1rem',
},
};
4. React Component with Consumer Styles
// App.tsx
import React from 'react';
import styled from 'styled-components';
import ScopedReset from './styles/scopedReset';
// Define component styles that respect the reset
const AppContainer = styled.div`
/* These styles won't be affected by legacy CSS */
font-family: var(--font-family-base);
color: var(--color-text);
background-color: var(--color-bg);
`;
const Button = styled.button`
/* Explicit styling - not relying on inheritance */
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
font-size: 1rem;
font-weight: 500;
color: white;
background-color: var(--color-primary, #0066cc);
border: 1px solid transparent;
border-radius: 4px;
cursor: pointer;
transition:
background-color 0.2s ease,
box-shadow 0.2s ease;
&:hover {
background-color: #0052a3;
}
&:focus-visible {
outline: 2px solid var(--color-primary, #0066cc);
outline-offset: 2px;
}
&:active {
box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.25);
}
`;
const Card = styled.div`
padding: 1.5rem;
border: 1px solid var(--color-border);
border-radius: 8px;
background-color: white;
`;
export default function App() {
return (
<>
<ScopedReset />
<div className="new-app">
<AppContainer>
<h1>My React App</h1>
<Card>
<h2>Welcome</h2>
<p>This content is isolated from legacy Bootstrap styles.</p>
<Button>Click Me</Button>
</Card>
</AppContainer>
</div>
</>
);
}
5. With PostCSS Plugin for Automation
// postcss.config.js
const postcss = require('postcss');
module.exports = {
plugins: [
require('postcss-nested'),
require('postcss-preset-env')({
stage: 3,
}),
require('./postcss-plugins/scoped-reset-plugin'),
],
};
// postcss-plugins/scoped-reset-plugin.js
module.exports = (opts = {}) => {
return {
postcssPlugin: 'postcss-scoped-reset',
Once(root) {
root.walkRules((rule) => {
// Add .new-app prefix to component styles
if (rule.selector && !rule.selector.includes('.new-app')) {
rule.selector = `.new-app ${rule.selector}`;
}
});
},
};
};
module.exports.postcss = true;
6. Complete Component Library
// components/Button.tsx
import React from 'react';
import styled, { css } from 'styled-components';
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'primary' | 'secondary' | 'danger';
size?: 'sm' | 'md' | 'lg';
}
const baseStyles = css`
display: inline-flex;
align-items: center;
gap: 0.5rem;
font-family: var(--font-family-base);
font-size: 1rem;
font-weight: 500;
border: 1px solid transparent;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
&:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`;
const variantStyles = {
primary: css`
color: white;
background-color: var(--color-primary, #0066cc);
&:hover:not(:disabled) {
background-color: #0052a3;
}
`,
secondary: css`
color: var(--color-text);
background-color: #f0f0f0;
border-color: var(--color-border);
&:hover:not(:disabled) {
background-color: #e8e8e8;
}
`,
danger: css`
color: white;
background-color: #dc3545;
&:hover:not(:disabled) {
background-color: #c82333;
}
`,
};
const sizeStyles = {
sm: css`
padding: 0.375rem 0.75rem;
font-size: 0.875rem;
`,
md: css`
padding: 0.5rem 1rem;
font-size: 1rem;
`,
lg: css`
padding: 0.75rem 1.5rem;
font-size: 1.125rem;
`,
};
const StyledButton = styled.button<ButtonProps>`
${baseStyles}
${(props) => variantStyles[props.variant || 'primary']}
${(props) => sizeStyles[props.size || 'md']}
`;
const Button: React.FC<ButtonProps> = ({
variant = 'primary',
size = 'md',
...props
}) => (
<StyledButton variant={variant} size={size} {...props} />
);
export default Button;
7. Migration Guide & Best Practices
# CSS Migration Guide
## Setup Instructions
1. **Install scoped reset in your main app wrapper:**
```html
<div class="new-app">
<YourReactApp />
</div>
- Import reset CSS before component styles:
import './styles/scoped-reset.css';
import ScopedReset from './styles/scopedReset';
Verification Checklist
Common Issues & Solutions
Issue: Descendant selectors still inherit styles
Solution:
.new-app .custom-component {
/* Explicitly reset or set all properties */
all: revert;
/* Then define your styles */
}
Issue: !important rules from legacy CSS
Solution:
// Use cascade specificity instead
.new-app .my-button {
background-color: blue; /* More specific than .btn */
}
Issue: SVG/Icon inheritance
Solution:
.new-app svg {
all: revert;
display: block;
fill: currentColor;
}
Performance Optimization
// Lazy load reset only when needed
const ScopedReset = lazy(() => import('./styles/scopedReset'));
export default function App() {
return (
<Suspense fallback={null}>
<ScopedReset />
<div className="new-app">
{/* Your app */}
</div>
</Suspense>
);
}
## 8. Testing Utilities
```javascript
// __tests__/scopedReset.test.ts
import { render } from '@testing-library/react';
import App from '../App';
describe('Scoped Reset', () => {
it('should not inherit Bootstrap button styles', () => {
const { container } = render(<App />);
const button = container.querySelector('.new-app button');
const styles = window.getComputedStyle(button);
expect(styles.background).not.toContain('rgb(0, 123, 255)'); // Bootstrap primary
});
it('should restore default typography', () => {
const { container } = render(<App />);
const heading = container.querySelector('.new-app h1');
const styles = window.getComputedStyle(heading);
expect(styles.fontSize).toBe('32px'); // 2rem at 16px base
expect(styles.fontWeight).toBe('700');
});
it('should reset form inputs', () => {
const { container } = render(<App />);
const input = container.querySelector('.new-app input');
const styles = window.getComputedStyle(input);
expect(styles.padding).toBe('0px');
expect(styles.margin).toBe('0px');
});
});
9. Theme Configuration Hook
// hooks/useResetTheme.ts
import { useEffect } from 'react';
interface ThemeConfig {
[key: string]: string;
}
export function useResetTheme(config: ThemeConfig) {
useEffect(() => {
const appElement = document.querySelector('.new-app');
if (!appElement) return;
Object.entries(config).forEach(([key, value]) => {
(appElement as HTMLElement).style.setProperty(key, value);
});
}, [config]);
}
// Usage:
export default function App() {
useResetTheme({
'--color-primary': '#1e40af',
'--font-family-base': 'Inter, sans-serif',
});
return (
<div className="new-app">
{/* Your app */}
</div>
);
}
This comprehensive architecture provides:
✅ Complete CSS reset scoped to .new-app
✅ Multiple implementation methods (CSS, styled-components, CSS-in-JS)
✅ Prevents Bootstrap/Foundation pollution
✅ Component-level styling control
✅ Theme customization support
✅ Testing strategies
✅ Performance optimization
✅ Migration guidelines
Perfect for gradual migration from legacy frameworks!