Ask Your Question
0

How can a wider child element be centered horizontally in a parent container that is narrower?

asked 2023-05-07 05:11:31 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-07 05:13:01 +0000

pufferfish gravatar image

To center a wider child element in a parent container that is narrower, you can use the CSS property margin: 0 auto; on the child element. This will center the child element horizontally within the parent container.

For example, if your HTML looks like this:

<div class="parent">
  <div class="child">Wider content here</div>
</div>

You can use the following CSS for the child element:

.child {
  width: 80%; /* Or any other width */
  margin: 0 auto;
}

This will make the child element 80% of the width of the parent container and centered horizontally.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-07 05:11:31 +0000

Seen: 8 times

Last updated: May 07 '23