Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 2 years ago

HTML CSS

Olebogeng Sebogodi•80
@Olebxgeng
A solution to the Product preview card component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


I struggled to space the image & details 50/50 on a bigger screen. Struggled with centering my main on the page but figured it out (Would really like to know a better way to do it). How to center my main on the mobile device to get rid of the horizontal scrolling. Any advice on how to better my code, thanks.

Appreciate the feedback & help :).

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Apah•280
    @apah-dev
    Posted about 2 years ago

    I've struggled with centering items myself for a while but now i know many options you could use. I'll share them here. You could try them all out on a test project and then apply them on real projects when you understand them.

    container {
    margin-left: auto; 
    margin-right: auto; 
    width: 400px;
    }
    

    If you want to use margin and make it work you have to specify width if not it doesn’t work *There’s no way to vertically align it to the centre. That’s the disadvantage of this option *

    container {
    position: absolute; 
    top: 50%; left: 50%; 
    transform: translate(-50%, -50%);
    }
    
    
    • the -50%, -50% represent x and y axis *

    Using Flexbox method:

    main { 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    height: 100vh; }
    

    Using the grid method:

    main { 
    display: grid; 
    align-items: center; 
    height: 100vh; 
    justify-content: center; 
    }
    

    this is far from exhaustive as the methods you can use are many. I do advice you try them out and figure out the one that's best for the particular project you're working on. also consider how it would work for the responsiveness of your page. Hope this helps

    Marked as helpful
  • orphandeity•530
    @orphandeity
    Posted about 2 years ago

    you can center your content by giving the containing element (the body in your case) a height and some flex-box properties like so...

    body {
        min-height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    

    you could use grid instead like this...

    body {
        min-height: 100vh;
        display: grid;
        place-content: center;
    }
    

    to space the content within main evenly you can also take advantage of flex-box...

    main: {
        display: flex;
    }
    
    .img, .details {
        flex: 1;
    }
    
    Marked as helpful
  • wheels63•50
    @wheels63
    Posted about 2 years ago

    to center the main container horizontally and vertically smack in the middle:

    main {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    

    some kind stranger told me about this. this will position the main container smack in the middle of the viewport.

    Marked as helpful

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

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