Search code examples
phpcssalignment

Center an element with another element?


Say I have code like such:

<div id="thisone">Content</div>
<div id="thatone">More Content</div>

Say both elements have a background color, so you can see their size, then is there a way to align the element with an id of #thisone according to the element with an id of #thatone? thanks!

(Edit: One of the elements has position:fixed, while the other is position:static)


Solution

  • Can you maybe explain a little better what you are trying to achieve? As you have added the "center" tag, maybe you want something like this?

    <style>
      #thisone {
        background-color: red; width: 200px; height: 80px;
        margin-left: auto; margin-right: auto;
      }
      #thatone {
        background-color: blue; width: 200px; height: 80px;
        margin-left: auto; margin-right: auto;
      }
    </style>
    <div id="thisone">Content</div>
    <div id="thatone">More Content</div>
    

    (Which should center both divs in their parent)