wupeng
wupeng

Reputation: 63

How to set child width equal to parent element width with css?

My html is:

.parent
  .child

and the .parent width is not determined value, .child width is 100%, both child and parent has own border, at last the child element width is less 2px then parent's width,

How can I set the child width equal to parent element width only with css?

Upvotes: 0

Views: 12630

Answers (2)

NendoTaka
NendoTaka

Reputation: 1224

Set the padding to 0 in the parent. So in the parent css class add padding: 0px; this should fix it. You may also need to set margin: 0px; in the child css class. Also try display: block;.

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

Make sure the child element is display: block; (many elements are by default) and don't set any width for it. Block elements take up all the horizontal space they can get by default.

Upvotes: 4

Related Questions