Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 1 year ago

article-preview-component-Javascript

lij110397•230
@lij110397
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?

1. How to add HTML elements and styles using javascript

  1. Using innerHTML in javascript
  • In this method, I structure the HTML in Javascript files and added it to the document using innerHTML this attribute.
  • Advantage: It can add HTML when needed.
  • Disadvantage: It makes the JS file too long.
const shareHTML = `
    
        SHARE
        
        
        
    
    
      
      
      This is a share button
      
    `;
const sharePanel = document.createElement("div");
sharePanel.classList.add("sharePanel");
sharePanel.innerHTML = shareHTML;
sharePanel.classList.add("shareStyle");
  1. Place the HTML in the HTML files, CSS in the CSS stylesheet. We can include the HTML and CSS in advance in HTML and CSS stylesheet. And using Javascript only controls the display attribute of the elements.
  • Advantage: This method can define the HTML and CSS of extra content in advance. The presentation and the control logic are separated, which makes the code neat.
socialPanel.style.display = "none";

2. cannot change its fill using javascript (if i have to change the svg fill using javascript) To change the fill of a svg image, the svg image has to be inline. If the svg is inside a label, its fill cannot be changed by setting style.fill. However, placing the style setting all in stylesheets may make this javascript file look better.


  Alternate Text
  

  const shareSVG = shareButton2.querySelector("#shareSVG");
  const pathElement = shareSVG.querySelector("path");
  pathElement.style.fill = "hsl(210, 46%, 95%)";
What challenges did you encounter, and how did you overcome them?

How To set the extra panel's position?

  • What I do: I use absolute position in this case. I use javascript to calculate the position of the button and get panel's position accordingly. Then I set the panel's position using "top" and "left" attributes.
  • What's the problem? This means I have to do this action according to the screen's size since I only do this in larger screen. This may cause different media queries from those in CSS stylesheet. In logic, it may be a better way to make the position relative, but how to do it?
  if (window.innerWidth > 910) {
    const buttonRect = shareButton.getBoundingClientRect();
    const buttonX = buttonRect.left;
    const buttonY = buttonRect.top;
    //use the button position to set panel position
    socialPanel.style.position = "absolute";
    socialPanel.style.top = `${buttonY - 100}px`;
    socialPanel.style.left = `${buttonX - 150}px`;
  }

How Javascript adjust to responsive design? How javascript may use media Queries? It is a good idea to use media queries in javascript too? What will be a safer way to use media queries in javascript? What are the common scenarios? If not using media queries, how to adjust js codes for different screen sizes just like in this case?

  • What I do? I put all CSS relative codes in CSS stylesheet including media queries. This means adjusting the styles of the extra panel in stylesheet only.
What specific areas of your project would you like help with?
  1. How To set the extra panel's position relatively?
  2. How Javascript adjust to responsive design? How javascript may use media Queries?
Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • delroscol98•410
    @delroscol98
    Posted about 1 year ago

    Hello there! Great job on the project. Just a couple of things to think about for your next challenge:

    1. If you haven't already, I'd recommend getting the PRO version of frontendmentor. The Figma files super increase the accuracy of the styling. Using viewport widths allow for better responsiveness.

    2. Always try to have a separation of concerns. HTML file controls the adding and removing elements. CSS file controls the styling. JS controls the interactivity. Otherwise (like you said) the JS file can handle too much and become too long. If unsure, it might be worthwhile researching into "Separation of Concerns"

    Hopefully this feedback was helpful. All the best in your next project!

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