0

I'm trying to design a responsive website layout and I need to center a div exactly in the middle of the page (both vertically and horizontally). I've tried using margin and position, but it doesn't work as expected across all screen sizes.

2
  • Please provide enough code so others can better understand or reproduce the problem. Commented May 5 at 12:24
  • See stackoverflow.com/questions/19026884/… Commented May 5 at 12:25

1 Answer 1

0

You can use these for that;

HTML

<div class="parent">
    <div class="child"></div>
</div>

CSS

/*FLEX*/

.parent {
    display: flex;
    justify-content: center;
    align-items: center;
}

// OR

/*POSITION-RELATIVE-ABSOLUTE*/

.parent {
    position: relative;
}

.parent .child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.