DIV next to centered div

后端 未结 6 632
轻奢々
轻奢々 2020-12-16 20:19

\"enter

body {
    background: rgba(0, 0, 0, 0.8);
    font-family: sans-s         


        
6条回答
  •  星月不相逢
    2020-12-16 20:50

    I imagine there are quite a few approaches you can take. Here is one.

    Using the same HTML structure as in your example, the goal can be achieved thus:

    • Make all elements except the main wrapper inline-block.
    • Center the "centered" element by giving text-align: center to the main wrapper.
    • Put the sidebar out of the document flow by giving it position: absolute. This requires that you give its container position: relative as well.
    • Give the sidebar's container zero width and height so that it doesn't affect the centering calculations. Give it vertical-align: top so that its top edge (which is also the sidebar's top edge) aligns with the top edge of the centered element.
    • Optionally specify text-align for the centered element and the sidebar if you don't want their contents to be centered within themselves.

    As a bonus, with this approach you can directly specify the widths for both the centered div and the sidebar in just one place.

    See it in action.

提交回复
热议问题