html table width>page width right padding not working - html

I have a table that has a lot of columns and I need the columns to be the same width, so the table total width is bigger than page width, and that's fine because the browser shows the horizontal scrollbar. What's I can't get around is that, while left and top padding are ok, the right padding is not working, there's no space between table right end and the browser frame.
<table>
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
<tr><td>G</td><td>H</td><td>I</td></tr>
</table>
table {
width: 200%;
padding: 10px 0px 10px 0px;
background-color: #800000;
}
td {
background-color: #ffa500;
}
Here's a fiddle

As also mentioned in the answer by Umar A., You have used padding as padding: 10px 0px 10px 0px; That means, padding top and bottom is 10px and padding left and right is 0px; Use it as
padding:10px if you want it on all sides.
But, here is the example mentioned, it appears that you do not want padding. You actually require margin-right to be applied as whitespace on the right side of the table. Even if you put a value of margin-right, it will not have effect.
It happens so, because since the layout width is > 100%, element + margin-right extends beyond viewport now, and complete viewport width is consumed by table. Make view port less than 100% and margins are present. You gave your table a width of 200%. This is 200% of what? Entire page body. So, that means, you want table to be 200% of its parent container. Now, stuff like margin is applied to an element with comparison to its surrounding elements like parent container. But towards the right side, element size extends beyond container size. So, properties like margin do not have any visual effect. You can see it in snippet below. I have wrapped the table in a div to show application of margin-right.
div{
overflow:auto;
border:7px solid black;
}
table {
width: 80%;
margin-right:150px;
background-color: #800000;
display:inline-block;
}
td {
background-color: #ffa500;
}
<div>
<table>
<thead></thead>
<tbody>
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
<tr><td>G</td><td>H</td><td>I</td></tr>
</tbody>
</table>
</div>
But when you bring width of table to less than 100%, margin-right will start to apply itself in comparison to its surrounding elements. Because now, width of table + margin-right < width of document body, which is 100% of window viewport by default. So, the effect that you want to achieve can be created by wrapping table in a block level element like div, and allowing it to take expanse of div. Give it relative positioning and then define that it should be placed 5px from right side. If you specify left also, left will override right.
Another reference
div{
overflow:auto;
position:relative;
right:5px !important;
height:100%;
}
table {
width: 200%;
background-color: #800000;
}
td {
background-color: #ffa500;
}
<div>
<table>
<thead></thead>
<tbody>
<tr><td>A</td><td>B</td><td>C</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
<tr><td>G</td><td>H</td><td>I</td></tr>
</tbody>
</table>
</div>

As a general rule of thumb, think of padding as inward-facing spacing, and margin as outward-facing spacing.
That being said, the table element will not apply margin-right if the table's width is set to 100%, so I would recommend wrapping the table in a div with margin-right: 10px; for example.
Update:
Here's a working fiddle. If you're trying to simulate a wide table, remember a table will take all the available width, no need to set it to width: 200%; as that will give you unexpected results.

The css syntax for padding shorthand property is as follows:
padding: top right bottom left;
And you set it as follows:
padding: 10px 0px 10px 0px;
Which means:
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 0px;
It's normal, thus, that your table behaves like that.
If you check your fiddle with the inspector of your browser you have this result:
You can see that the body has 8px of margin.
And the table...
...has padding left and right set to 0px.
PS: Zoom to view the images well, or click on it.

Setting the width to 200% will not get clear results as per Johnny Kutnowski, which is correct.
Also if you rely on Browser's scroll, then even if you put the table inside a div, it will exceed and stick to the corners of your screen.
try this code if its acceptable to have a div which has a scroll if the table width exceeds.
Since you mentioned you have many columns:
<div class="container">
<table>
<tr><td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
</tr>
<tr><td>D</td><td>E</td><td>F</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
</tr>
<tr><td>G</td><td>H</td><td>I</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
<td>A</td><td>B</td><td>C</td><td>A</td><td>B</td><td>C</td>
</tr>
</table>
</div>
table {
width: 100%;
padding: 10px 0px 10px 0px;
background-color: #800000;
}
td {
background-color: #ffa500;
min-width: 50px !important;
}
.container {
padding-right: 100px !important;
overflow: auto;
}
Try this JsFiddle

You can not set padding to a table element with css. Only to td like
table td {padding: 10px;}

try using only padding-right:12px;
i think this will work

Related

clearing after a float left creates a void between two divs

I have the following part of my html
<div class="header">
<div class="header-bar">
<div class="pull-left">
<div class="title">Ci models database</div>
</div>
</div>
<div class="clear-both"></div>
<ol class=breadcrumb>
<li class="active">All models</li>
</ol>
</div>
the css(breadcrumb and active classes are bootstrap)
.header-bar {
border: None;
background-color: #66CCFF;
min-height:30px;
}
.title {
padding: 5px 5px 10px 5px;
color: white;
font-size: large;
}
.clear-both{
clear:both;
}
But between header-bar and breadcrumb html added a white space(see bootply). How can I remove this white space, since no padding and margin can be found between to divs.
The problem is that the calculated height of the internal .title div is greater than the calculated height of the container .header-bar. Properties like height, min-height, border, padding can directly effect heights, whereas properties like display, box-sizing and position can all indirectly effect height.
The result is the internal .title div pushes down the next div in the flow by 10px.
CSS has no rules that say a div must contain it's children in height and stop them from effecting other divs, even when height is directly defined. We need to tell it exactly how it should behave when things are rendered.
There are several ways to fix this:
http://www.bootply.com/Qa1ME2M2uk - use overflow: hidden; on the parent. Overflow is a css property which is used how to control what happens when child elements are larger than their parents. It's worth noting that depending on other properties overflow won't necessarily render itself in a way that disrupts layout.
http://www.bootply.com/ssq3EAzeyk - set explicit heights to take strict control over the dimensions of the elements. This might be the best option for a header bar.
http://www.bootply.com/yeodYRLLJk - set a greater min-height on the parent, one which will definitely contain the child. This is useful if your padding is for alignment purposes - setting min-height: 40px; in the example does this.
http://www.bootply.com/GznfJxUWUF - remove the padding that is making the element calculate as taller (as mentioned in another answer).
Apostolos, the white space is coming from the .titleclass.
The bottom padding of 10px.
Zero this and the white space will go.
.title {
padding: 5px 5px 0px 5px;
you will have to add a float: left to both parent containers (.header-bar and breadcrumb) otherwise the clear won't affect anything. furthermore you will have to give both containers width: 100%
.header-bar {
border: None;
background-color: #66CCFF;
min-height:30px;
width: 100%;
float: left;
}
.breadcrumb {
width: 100%;
float: left;
}
.title {
padding: 5px 5px 10px 5px;
color: white;
font-size: large;
}
.clear-both{
clear:both;
}

horizontal formatting for block-level element

In the "CSS The definitive Guide", the author said "The values of these seven properties must add up to the width of the element’s containing block, which is usually the value of width for a block element’s parent". But In the following, the child element is wider than the parent.
//html
<div class="wrapper">
<div class="content-main">
<div class="main">This is main</div>
</div>
</div>
// style
.wrapper {
width: 500px;
padding: 30px 0;
border: 1px solid #0066cc;
}
.content-main {
padding: 0 20px;
border: 2px solid #00CC33;
}
.main {
width: 500px;
border: 1px solid #f00;
}
So I have two quesions:
What does the author mean for the "seven properties must add up to the width of the element’s containing block".
Why in my example, the element will stick out the parent.
in the edit version, the seven properties add up to the width of the element' containing block seems work well. Why the equation not apply to my example?
EDIT VERSION
p.wide width is 438px, the author calculate as following
10px(left margin) + 1(left border) + 0 + 438px + 0 + 1(right border) – 50px(right margin) = 400px(parent width)
// HTML
<div>
<p class="wide">A paragraph</p>
<p>Another paragraph</p>
</div>
// CSS
div {width: 400px; border: 3px solid black;}
p.wide {
margin-left: 10px; width: auto; margin-right: -50px;
border: 1px solid #f00;
}
What does the author mean for the "seven properties must add up to the width of the element’s containing block".
He is teaching you CSS Box Model, here, you are using div elements which are block level in nature, block level means they take up entire horizontal space by default, unlike span or i or b tags, which are inline elements.
So when you use padding or border they are added outside of the element and not inside. So for example you have an element of say 100x100 in dimension, and you add a padding like
element {
width: 100px;
height: 100px;
padding: 10px;
}
So in the above case, your element will be 120x120 in total, because it will add up 10px of padding on all four size of your element.
Explaining padding syntax
You have two different padding syntax, which are as follows...
padding: 30px 0; in .wrapper and padding: 0 20px; in .content-main so these aren't the same.
Both the above syntax are nothing but short hand syntax of padding ... The complete version looks like...
padding: 5px 10px 15px 20px; /*Nothing to do with your code, this is just a demo */
So in the above example, you have to go clock wise, so 5px is nothing but padding-top: 5px;, then comes 10px which is right, next is bottom and the last 20px is padding-left.
So what when it's just two parameters defined, that means...
padding: 0 20px;
--^---^---
top bottom/left right
So, top and bottom are set to 0 here and right and left to 20px respectively...
Explaining the CSS
Note: None of the element has the height set by you, so the screens
you see ahead which I've attached are computed. So ignore height in them
completely.
.wrapper {
width: 500px;
padding: 30px 0;
border: 1px solid #0066cc;
}
Here, your element is 502px wide, so why? As I said that border will add on all four sides of the element, and hence it will add 1px on all four sides but your padding is applied to top and bottom only. It's better to use tools like Firebug which will show you graphical presentation of what's going on behind the scenes.
Coming to the second snippet which has the following syntax
.content-main {
padding: 0 20px;
border: 2px solid #00CC33;
}
Here, it is now adding 2px border to your element but, the padding is now applied to left and right and nothing for top and bottom so now the computation will be
Coming to the last snippet which is
.main {
width: 500px;
border: 1px solid #f00;
}
Here, just border is applied, but why it goes out? In technical words, why it overflows? Because you have width defined. So since you have padding set for the parent element, which is padding: 0 20px;, so it will nudge the child element by 20px from the left side. I'll attach a screen of Firebug to show you why it is nudged....
Why in my example, the element will stick out the parent.
Because you are defining width of 500px to your .main div
Demo (What happens when you take out the width)
The default box model is known as content-box
This can be altered by defining a new CSS3 introduced property called box-sizing set to border-box which will alter your box-model in such a way that it will count the padding and border inside the element instead of outside

Div is having different width than specified, also is having a margin that is not specified

Here is my code: http://jsfiddle.net/8z4aw/
I want to have a bunch of div's that are behaving like a table. As you can see, I have specified explicitly their widths, but the browser is completely ignoring it and doing what it wants. Also, it appears that those div's are having a right margin, that is not specified anywhere. So where does it come from? How to fix those two problems?
Ignore code below, pasting it only so that post will go through
.standardTable {
color: #fff;
}
You have a couple options:
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
My preferred is to set the font-size of the parent element to 0 and set the font-size on the children manually:
http://jsfiddle.net/8z4aw/9/
This is what I changed in the CSS:
.standardTable {
font-size: 0;
}
.standardTable .td,
.standardTable .th,
.standardTable .tr {
font-size: 18px;
}
Well the widths aren't behaving because you have two different sets of padding on them, due to the "th" and "td" classes.
So, anything with the classes "th integer" is going to have a width of 181px and a padding of 10px. This adds up to a total width of 201px
However, anything with the classes "td integer" has a width of 181px and a padding of 20px, which means they have a total width of 221px
So, due to your padding, those are always going to be 20px different from one another.
The reason you have a "right margin" is because the table with class "standardTable" is actually inheriting it's width from the js fiddle window, which means it is 100%. You need it to be set to a fixed with that adds up to all the cells inside.
You can see the problem here:
<div class="th integer">
Podkategorii
</div>
<div class="td integer">
0
</div>
.standardTable .th {
display: inline-block;
background-color: #2b5797;
padding: 10px;
}
.standardTable .td {
display: inline-block;
background-color: #2d89ef;
padding: 20px;
}
Here's a working fiddle:
http://jsfiddle.net/shayl/8z4aw/13/

Ignore parent padding

I'm trying to get my horizontal rule to ignore the parent padding.
Here's a simple example of what I have:
#parent {
padding:10px;
width:100px;
}
hr {
width:100px;
}
You will find that the horizontal rule extends out of the parent by 10px. I'm trying to get it to ignore the padding that everything else in the parent div needs.
I'm aware that I could make a separate div for everything else; this is not the solution I'm looking for.
Easy fix, just do
margin:-10px
on the hr.
For image purpose you can do something like this
img {
width: calc(100% + 20px); // twice the value of the parent's padding
margin-left: -10px; // -1 * parent's padding
}
In large this question has been answered but in small parts by everyone. I dealt with this just a minute ago.
I wanted to have a button tray at the bottom of a panel where the panel has 30px all around. The button tray had to be flush bottom and sides.
.panel
{
padding: 30px;
}
.panel > .actions
{
margin: -30px;
margin-top: 30px;
padding: 30px;
width: auto;
}
I did a demo here with more flesh to drive the idea. However the key elements above are offset any parent padding with matching negative margins on the child. Then most critical if you want to run the child full-width then set width to auto. (as mentioned in a comment above by schlingel).
Another solution:
position: absolute;
top: 0;
left: 0;
just change the top/right/bottom/left to your case.
Kinda late.But it just takes a bit of math.
.content {
margin-top: 50px;
background: #777;
padding: 30px;
padding-bottom: 0;
font-size: 11px;
border: 1px dotted #222;
}
.bottom-content {
background: #999;
width: 100%; /* you need this for it to work */
margin-left: -30px; /* will touch very left side */
padding-right: 60px; /* will touch very right side */
}
<div class='content'>
<p>A paragraph</p>
<p>Another paragraph.</p>
<p>No more content</p>
<div class='bottom-content'>
I want this div to ignore padding.
</div>
I don't have Windows so I didn't test this in IE.
fiddle:
fiddle example..
If you have a parent container with vertical padding and you want something (e.g. an image) inside that container to ignore its vertical padding you can set a negative, but equal, margin for both 'top' and 'bottom':
margin-top: -100px;
margin-bottom: -100px;
The actual value doesn't appear to matter much. Haven't tried this for horizontal paddings.
margin: 0 -10px;
is better than
margin: -10px;
The later sucks content vertically into it.
Here is another way to do it.
<style>
.padded-element{margin: 0px; padding: 10px;}
.padded-element img{margin-left: -10px; width: calc(100% + 10px + 10px);}
</style>
<p class="padded-element">
<img src="https://images.pexels.com/photos/3014019/pexels-photo-3014019.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940">
</p>
Here are some examples on repl.it: https://repl.it/#bryku/LightgrayBleakIntercept
Your parent is 120px wide - that is 100 width + 20 padding on each side so you need to make your line 120px wide. Here's the code. Next time note that padding adds up to element width.
#parent
{
width: 100px;
padding: 10px;
background-color: Red;
}
hr
{
width: 120px;
margin:0 -10px;
position:relative;
}
If your after a way for the hr to go straight from the left side of a screen to the right this is the code to use to ensure the view width isn't effected.
hr {
position: absolute;
left: 0;
right: 0;
}
The problem could come down to which box model you're using. Are you using IE?
When IE is in quirks mode, width is the outer width of your box, which means the padding will be inside. So the total area left inside the box is 100px - 2 * 10px = 80px in which case your 100px wide <hr> will not look right.
If you're in standards mode, width is the inner width of your box, and padding is added outside. So the total width of the box is 100px + 2 * 10px = 120px leaving exactly 100px inside the box for your <hr>.
To solve it, either adjust your CSS values for IE. (Check in Firefox to see if it looks okay there). Or even better, set a document type to kick the browser into strict mode - where also IE follows the standard box model.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
...
http://www.quirksmode.org/css/quirksmode.html
You just need to add negative margins to the child that match the padding of the parent. No need to set a width, change the box-sizing, or use absolute positioning.
#parent {
padding: 10px;
width: 100px;
}
hr {
margin-right: -10px;
margin-left: -10px;
// For modern browsers you can use margin-inline: -10px
}
The reason you don't need to set a width is because the hr element is a block element. It's width defaults to "auto", which means it will expand to fill it's parent (minus padding, margin, and border).
easy fix.. add to parent div:
box-sizing: border-box;

How can I force overflow: hidden to not use up my padding-right space

I have the following code:
<div style="width: 100px;
overflow: hidden;
border: 1px solid red;
background-color: #c0c0c0;
padding-right: 20px;
">
2222222222222222222222111111111111111111111111113333333333333333333</div>
(XHTML 1.0 transitional)
What happens is that the padding-right doesn't appear, it's occupied by the content, which means the overflow uses up the padding right space and only "cuts off" after the padding.
Is there any way to force the browser to overflow before the padding-right, which means my div will show with the padding right?
What I get is the first div in the following image, what i want is something like the 2nd div:
image
I have the same problem with the overflow:hidden; obeying all the padding rules, except for the right hand side. This solution works for browsers that support independent opacity.
I just changed my CSS from:
padding: 20px;
overflow: hidden;
to
padding: 20px 0 20px 20px;
border-right: solid 20px rgba(0, 0, 0, 0);
Having container divs works fine, but that effectively doubles the amount of divs on a page, which feels unnecessary.
Unfortunately, in your case this won't work so well, as you need a real border on the div.
Your best bet is to use a wrapping div and set the padding on that.
I had a similar problem that I solved by using clip instead of overflow. This allows you to specify the rectangular dimensions of the visible area of your div (W3C Recommendation). In this case, you should specify only the area within the padding to be visible.
This may not be a perfect solution for this exact case: as the div's border is outside the clipping area, that will become invisible too. I got around that by adding a wrapper div and setting the border on that, but since the inner div must be absolutely positioned for clip to apply, you would need to know and specify the height on the wrapper div.
<div style="border: 1px solid red;
height: 40px;">
<div style="position: absolute;
width: 100px;
background-color: #c0c0c0;
padding-right: 20px;
clip: rect(auto, 80px, auto, auto);">
2222222222222222222222111111111111111111111111113333333333333333333</div>
</div>
Wrap the div and apply padding to the parent
.c1 {
width: 200px;
border: 1px solid red;
background-color: #c0c0c0;
padding-right: 50px;
}
.c1 > .c1-inner {
overflow: hidden;
}
<div class="c1">
<div class="c1-inner">2222222222222222222222111111111111111111111111113333333333333333333
</div>
</div>
If you have a right-adjacent element to the one in question, put padding on its left. That way the content from the left element will flow up to but not past its margin, and the left padding on the right-adjacent element will create the desired separation. You can use this trick for a series of horizontal elements which may have content that needs to be cut off because it is too long.