Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Padding on Display table overflow its container

Example: http://www.homeroe.com/homeroe.com/newHome/pulpaForum/test.php

Why is that the table div is going out from its container whenever I add padding?

Is there any work around for this problem?

<style>
.foroContainer {
    width: 700px;
    margin-left: auto; 
    margin-right: auto; 
    background: yellow;
}

.foroContainer .table{
    display: table;
    width: 100%;
    padding: 8px;
    border: 1px solid #a9a9a9;
    margin-bottom: 1em;
}

.foroContainer .row{
    display: table-row;
}

.foroContainer .cell{
    display: table-cell;
}

#right.cell{
    text-align: right;
    border-top: 1px solid #a9a9a9;
}
}
</style>

<div class="foroContainer">
<div class="table">
    <div class="row">
        <div class="cell">asdasdasdas</div>
    </div>
    <div class="row">
        <div id="right" class="cell">asdasdas | asdasdsad | asdasdasdas</div>
    </div>
</div>
like image 806
Homeroe Avatar asked Jun 08 '12 09:06

Homeroe


2 Answers

Alternatively, you could try altering the box-sizing property, either for every element on the page, or just that one container. E.g. for each element on the page, you would write:

* { box-sizing: border-box; }

This will alter the default box model that the browser uses so that width of the element is not changed, padding or not.

like image 162
pkjp Avatar answered Sep 30 '22 14:09

pkjp


The hierarchy of encapsulation in CSS is: margin - border - padding

When you are adding padding to an object you practically alter it's width. If something is 100px in width and you add padding:10px it's width will become 120px (100 + 10 padding-left + 10 padding right)

This is the reason that your container is pushed over (it's width:100%) a good way would be another container internal to your table with width:100% but the table without width.

like image 30
Dimitris Romeo Havlidis Avatar answered Sep 30 '22 13:09

Dimitris Romeo Havlidis