Building a Reddit Clone : HTML

I'm interested in creating a Reddit clone using HTML, CSS, and JavaScript. Could someone provide guidance on the necessary steps and code examples to develop key features such as user registration, post submissions, comment threads, and upvoting/downvoting? Additionally, I'd appreciate any insights on implementing a responsive design.

Asked by: Reon

Answers:

Structure and functionality of a Reddit-like website:

HTML code (index.html):

<!DOCTYPE html>
<html>
<head>
  <title>Reddit Clone</title>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
  <header>
    <h1>Reddit Clone</h1>
    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Popular</a></li>
        <li><a href="#">New</a></li>
        <li><a href="#">Submit Post</a></li>
      </ul>
    </nav>
  </header>
  
  <div id="content">
    <section id="posts">
      <article class="post">
        <h2 class="post-title">Sample Post Title</h2>
        <p class="post-author">Posted by <span class="author-name">JohnDoe</span></p>
        <p class="post-content">This is the content of the post.</p>
        <div class="post-actions">
          <button class="upvote-button">Upvote</button>
          <button class="downvote-button">Downvote</button>
          <span class="vote-count">0</span>
        </div>
        <div class="comments">
          <h3>Comments</h3>
          <ul>
            <li>Sample comment 1</li>
            <li>Sample comment 2</li>
            <li>Sample comment 3</li>
          </ul>
        </div>
        <div class="comment-form">
          <h3>Add a Comment</h3>
          <textarea></textarea>
          <button class="submit-comment-button">Submit Comment</button>
        </div>
      </article>
    </section>
    <aside id="sidebar">
      <h3>Popular Subreddits</h3>
      <ul>
        <li><a href="#">r/programming</a></li>
        <li><a href="#">r/science</a></li>
        <li><a href="#">r/movies</a></li>
        <li><a href="#">r/sports</a></li>
      </ul>
    </aside>
  </div>
  
  <footer>
    <p>&copy; TemplateXYZ | <a href="https://www.templatexyz.xyz/">https://www.templatexyz.xyz/</a></p>
  </footer>
  
  <script src="script.js"></script>
</body>
</html>

CSS code (styles.css):

/* Basic Styling */
body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

header {
  background-color: #333;
  color: #fff;
  padding: 10px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

nav ul li {
  display: inline;
  margin-right: 10px;
}

nav ul li a {
  color: #fff;
  text-decoration: none;
}

#content {
  display: flex;
}

#posts {
  flex: 2;
  padding: 20px;
}

.post {
  margin-bottom: 20px;
  border: 1px solid #ccc;
  padding: 10px;
}

.post-title {
  margin: 0;
}

.post-author {
  font-size: 14px;
  color: #777;
}

.post-content {
  margin-bottom: 10px;
}

.post-actions {
  margin-top: 10px;
}

.vote-count {
  margin-right: 10px;
}

.comments {
  margin-top: 20px;
}

.comments ul {
  list-style-type: none;
  padding: 0;
}

.comment-form textarea {
  width: 100%;
  margin-bottom: 10px;
}

/* Responsive Design */
@media screen and (max-width: 768px) {
  #content {
    flex-direction: column;
  }
  
  #sidebar {
    margin-top: 20px;
  }
}

/* Footer */
footer {
  background-color: #333;
  color: #fff;
  padding: 10px;
  text-align: center;
}

JavaScript code (script.js):

// Select all upvote buttons
var upvoteButtons = document.querySelectorAll('.upvote-button');

// Attach click event listener to each upvote button
upvoteButtons.forEach(function(button) {
  button.addEventListener('click', function() {
    var voteCountElement = button.parentNode.querySelector('.vote-count');
    var currentCount = parseInt(voteCountElement.textContent);
    voteCountElement.textContent = currentCount + 1;
  });
});

// Select all downvote buttons
var downvoteButtons = document.querySelectorAll('.downvote-button');

// Attach click event listener to each downvote button
downvoteButtons.forEach(function(button) {
  button.addEventListener('click', function() {
    var voteCountElement = button.parentNode.querySelector('.vote-count');
    var currentCount = parseInt(voteCountElement.textContent);
    voteCountElement.textContent = currentCount - 1;
  });
});

Save the HTML code in a file named index.html, the CSS code in a file named styles.css, and the JavaScript code in a file named script.js.

Answered by: Mr. Hyphen

Answer:

Related Pages:

  • Designing Architecture for Real-Time Ride-Sharing App with Android and Web Dashboard

    You are tasked with building a mobile app for a ride-sharing service similar to Uber or Lyft. The app should allow users to request rides, track their location in real-time, and display nearby drivers on a map. Additionally, the app should provide a web-based dashboard for drivers to manage ride requests, track their earnings, and update their availability status.

    How would you design and implement the architecture for this system, ensuring smooth communication between the Android app and the web-based dashboard? Discuss the key components, APIs, and protocols you would use to handle real-time location tracking, ride requests, and driver management, while maintaining security and data consistency across both platforms.

  • eBay Clone Website Template

    I'm seeking guidance on how to get started with creating an eBay clone website template. What technologies, frameworks, and languages should I consider? Are there any open-source projects or resources available that can help me with this task?

  • Centering Images with Flexbox

    Can you provide a detailed explanation of how to use Flexbox in CSS to center all images on a webpage effectively?

  • Building Spotify Clone with HTML, CSS, and JavaScript

    I'm looking to create a clone website of Spotify using HTML, CSS, and JavaScript. Can anyone guide me on the essential steps and provide some example code snippets? I want to ensure the website is SEO friendly and has an engaging user interface. Any suggestions on design patterns or best practices for this project?

  • Real-time text editor: Data consistency & sync.

    How can you implement a real-time collaborative text editor using HTML, JavaScript, PHP, and MySQL, while ensuring data consistency and synchronization among multiple users?