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.
How to fix this one?
Asked
Active
Viewed 515 times
-1

Sirawit 'Plum' P.
- 271
- 1
- 8
-
How is this a duplicate? I don't even know what is block. – Sirawit 'Plum' P. Apr 09 '18 at 19:13
5 Answers
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
-
-
margin: 0 auto gives you this result. adds equal margins on both side to make the element centered. this is what you wanted, isn't it? – Aryan Twanju Apr 09 '18 at 17:05
-
-
in that case you can do this body { text-align: center; } .mydiv { display: inline-block; width: 20%; } – Aryan Twanju Apr 09 '18 at 17:08
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.

Trinanjan Saha
- 17
- 1
- 7
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