-2

I am trying to put a div in the middle of another div, but always left aligns it. How I do it easily?

<div>
  <div>
    <p>This I want center</p>
  </div>
</div>
ruth
  • 1
  • 1

2 Answers2

0

The question is answered with the easiest google search, but the most obvious way is to give them CSS classes and then style them with CSS

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

And in CSS

  .parent{
      display: grid;
      place-items: center;
      background-color: red;
    }
    .child{
        background-color: blue;
    }
-1

If it's just text you can use:

text-align: center;

If you want to align the div, give to the div you want to center a margin:auto, but make sure to give it a width first, otherwise it will not work. For example:

width: 500px;
margin: auto;