-1

I'm working on HTML.
I tried to center div in parent.
But there's always right margin extended out of the div.
So, now I changed to just a plain text, but it doesn't solve the problem. Right Margin shown in image How to fix this one?

5 Answers5

0

Instead of margin: 0px; Try margin:0 auto;

0

Try this one for your code.

<body>
<p class="mydiv">ok</p>
</body>

.mydiv {
width: 20%;
margin: 0 auto;
}
Aryan Twanju
  • 2,464
  • 1
  • 9
  • 12
0

Paragraph is a block level element which takes the entire width by default. If you give width 20% remaining space will be filled with margin. If you don't want the remaining margin use inline block element. Check out w3scholl website for block and inline block elements.

0

That margin is because p is a block element.

Add following CSS.

  p.myDiv {
    display: inline-block;
  }
Harish Kommuri
  • 2,825
  • 1
  • 22
  • 28
0

Centering in CSS: A Complete Guide canu you read CSS TRICKS

Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn't that it's difficult to do, but in that there so many different ways of doing it, depending on the situation, it's hard to know which to reach for.

Kondal
  • 2,870
  • 5
  • 26
  • 40