Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 5 months ago

Respponsive Article preview component using html css and js

Ohazulike Stanley•220
@Gentlestan
A solution to the Article preview component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

What went well? This was my very first JavaScript project on Frontend Mentor, and overall, I’m really happy with how it turned out. The initial steps, such as adding the share icon and making it functional to toggle the visibility of the profile and social media icons, felt straightforward. Once I figured out the basics of how the toggle functionality worked, it became easier to implement the core functionality of the project. I also learned how to manipulate the DOM and handle events more effectively, which was great for my understanding of JavaScript.

What challenges did you encounter, and how did you overcome them?

At first, I thought the task would be simple, but I quickly realized the real challenge was managing the layout and functionality across different screen sizes. The main issue was how to ensure that the profile and social media icons would toggle correctly on smaller screens and not break the layout. I struggled with the positioning of the elements, particularly in ensuring everything was responsive. Getting the JavaScript to work consistently across different screen sizes (mobile vs desktop) was tricky, and I had to troubleshoot quite a bit before I could get it working as expected.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • M. Abubakr•250
    @m-abubakr1
    Posted 5 months ago

    Here’s some feedback and guidance to help improve your code clearly and straightforwardly:

    1. Layout and Centering Using CSS Grid

    Current Approach: You’re using Flexbox for centering and stacking your elements, which works well. However, CSS Grid can give you even more control over the layout. For example, you can center the entire card on the page with minimal code:

    body {
      display: grid;
      place-items: center; /* Centers content both horizontally and vertically */
      min-height: 100vh;
      background: #ecf2f8;
      font-family: "Manrope", sans-serif;
    }
    

    This method is very concise and powerful for centering content.

    2. Handling Responsiveness with Clear Breakpoints

    Tip: Instead of duplicating large blocks of styles for different screen sizes, try to group common rules and then override only what’s necessary within media queries. For example:

    .container {
      max-width: 327px;
      width: 100%;
      /* Common styles here */
    }
    
    @media (min-width: 769px) {
      .container {
        max-width: 730px;
        flex-direction: row;
      }
    }
    

    This makes your CSS easier to maintain and reduces repetition.

    3. Improving the JavaScript Toggle Function

    Current Approach: Your current JavaScript uses inline style changes to toggle the visibility of the social media icons. This works, but you can improve the separation of concerns by toggling a CSS class instead. This keeps your style logic in CSS and your behavior in JS.

    New Approach:

    Define a CSS class for the visible state:

    .hidden {
      display: none;
    }
    

    You might already have a default hidden state for the social icons:

    .social__media-icons {
      display: none;
    }
    .social__media-icons.active {
      display: flex;
    }
    

    Toggle the class in JavaScript:

    const shareElements = document.querySelectorAll(".shared");
    const socialIcons = document.getElementById("social__media-icons");
    
    shareElements.forEach(el => {
      el.addEventListener("click", function () {
        // Simply toggle the "active" class
        socialIcons.classList.toggle("active");
      });
    });
    

    This approach simplifies your code and makes it easier to adjust the visual presentation just by modifying your CSS.

    4. Accessibility and Semantic Improvements

    Interactive Elements: For elements that act as buttons (like your share icon), use a <button> element instead of an image. This automatically makes them keyboard-accessible. For example:

    <button class="share-button" aria-label="Share Article">
      <img src="images/icon-share.svg" alt="Share Icon" />
    </button>
    

    Heading Hierarchy: You are using a single headline (<h1>) which is good for the main title. Just ensure that if you add more headings later, they follow a logical order (e.g., <h2>, <h3>, etc.) to help screen readers and SEO.

    Alt Text Descriptions: Your alt texts are descriptive, which is excellent. Always make sure the alt text provides meaningful context.

    5. General Code Organization

    DRY (Don’t Repeat Yourself): Avoid duplicating CSS rules. Consolidate similar styles so that you have one source of truth for common elements. For example, if multiple containers share the same maximum width or padding, define a common class or variable.

    Consistent Naming: Your naming conventions (like hero__img, profile__container) are clear and consistent. Keep this up—it makes the code much easier to read and maintain.

    In Summary

    • Use CSS Grid for centering and overall layout to simplify your centering logic.
    • Group common styles and override with media queries for cleaner responsiveness.
    • Toggle classes in JavaScript to manage visibility, keeping behavior and styling separate.
    • Ensure accessibility by using semantic elements (like <button>) for interactive features and maintaining a logical heading structure.
    • Keep your code DRY and well-organized to ease future maintenance.

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub