maybe this is a silly question, but I can’t solve on my own.
I have a container (a div) which contains some other elements (example paragraphs, images and so on).
I want to assign a value to the container’s width in order to center it in the page. (the default behaviour makes the container’s width 100%, so I can’t center it)
I have found a lot of posts talking about how to make the child element’s width fit the container’s one, but the only way I know to do the reverse is by using javascript (I want to avoid this).
Is there any way to do this by css?
Thanks in advance!
Correct me if I do not understand you correctly.
You have a div with child elements of unknown width and want the div to be centered.
As Billy Moat has stated, you can achieve this by adding a parent div with “text-align:center” as css property.
You do not need to add a fixed width to the div-to-be-centered; instead, add
to this div. The div will now be centered and take the width of the child elements.