DEV Community

Je Phiri
Je Phiri

Posted on

Code for responsive apps options

Creating a responsive app requires the integration of techniques and frameworks that adapt to various screen sizes, ensuring an optimal user experience on devices like desktops, tablets, and smartphones. Here's an example structure for a responsive app built using React and CSS (tailored for front-end responsiveness).

Responsive App Example with React

  1. Setup Install dependencies:

bash

npx create-react-app responsive-app
cd responsive-app
npm install react-router-dom
Enter fullscreen mode Exit fullscreen mode
  1. App Structure Folder Structure:

plaintext

src/
├── components/
│   ├── Header.js
│   ├── Footer.js
│   └── MainContent.js
├── App.js
├── index.js
└── App.css
Enter fullscreen mode Exit fullscreen mode
  1. Code Implementation App.js:

jsx

import React from 'react';
import './App.css';
import Header from './components/Header';
import Footer from './components/Footer';
import MainContent from './components/MainContent';

function App() {
  return (
    <div className="App">
      <Header />
      <MainContent />
      <Footer />
    </div>
  );
}

export default App;
Header.js:

jsx
Copy code
import React from 'react';
import './Header.css';

const Header = () => {
  return (
    <header className="header">
      <h1>Responsive App</h1>
      <nav>
        <ul className="nav-links">
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav>
    </header>
  );
};

export default Header;
MainContent.js:

jsx

import React from 'react';
import './MainContent.css';

const MainContent = () => {
  return (
    <main className="main-content">
      <section className="intro">
        <h2>Welcome to the Responsive App</h2>
        <p>This layout adjusts to different screen sizes.</p>
      </section>
      <section className="gallery">
        <div className="gallery-item">Item 1</div>
        <div className="gallery-item">Item 2</div>
        <div className="gallery-item">Item 3</div>
      </section>
    </main>
  );
};

export default MainContent;
Footer.js:

jsx

import React from 'react';
import './Footer.css';

const Footer = () => {
  return (
    <footer className="footer">
      <p>© 2024 Responsive App. All rights reserved.</p>
    </footer>
  );
};

export default Footer;
Enter fullscreen mode Exit fullscreen mode
  1. Responsive CSS App.css:

css

body {
  margin: 0;
  font-family: Arial, sans-serif;
}

.App {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
Header.css:

css

.header {
  background-color: #282c34;
  color: white;
  padding: 1rem;
  text-align: center;
}

.nav-links {
  display: flex;
  justify-content: center;
  list-style: none;
  padding: 0;
}

.nav-links li {
  margin: 0 1rem;
}

.nav-links a {
  color: white;
  text-decoration: none;
}

/* Responsive Design */
@media (max-width: 768px) {
  .nav-links {
    flex-direction: column;
    align-items: center;
  }

  .nav-links li {
    margin: 0.5rem 0;
  }
}
MainContent.css:

css

.main-content {
  flex: 1;
  padding: 1rem;
  text-align: center;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.gallery-item {
  background-color: #ddd;
  padding: 2rem;
}

/* Responsive Design */
@media (max-width: 768px) {
  .gallery {
    grid-template-columns: 1fr;
  }
}
Footer.css:

css

.footer {
  background-color: #282c34;
  color: white;
  text-align: center;
  padding: 1rem 0;
}
Enter fullscreen mode Exit fullscreen mode
  1. Testing Responsiveness Use browser developer tools (F12) to check how the layout adjusts at different screen sizes (e.g., mobile, tablet, desktop). This structure sets the foundation for a fully responsive app. For complex projects, you can integrate libraries like Bootstrap, Material-UI, or frameworks like Next.js for additional features. This list of code consists of major used languages, other languages can be used too.

Top comments (0)