Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I alter only the top and bottom paddings with one property?

Tags:

css

I have a paragraph on a web page with 20 pixel margins on all 4 sides. I want to alter just the top and bottom paddings with a single property (so padding-top:0;padding-bottom:0; will not do).

What I have tried is demonstrated here.

http://jsfiddle.net/nFCru/1/

In this Fiddle, I tried to use padding: 30px inherit; to alter just the top and bottom paddings of a paragraph. However, this property-value pair sets the left and right paddings to 0 in addition to altering the top and bottom paddings.

p {
 border: 1px solid #000;
 padding: 20px;    
}

/* 
 * Here's my failed attempt at only altering the top 
 * and bottom padding values. The left and right padding
 * values are changing even if I use inherit.
 */

p {
 padding: 30px inherit;   
}​

Can I alter only the top and bottom paddings with one property?

like image 203
dangerChihuahua007 Avatar asked Mar 17 '12 21:03

dangerChihuahua007


People also ask

Do margin and padding specify the same property?

In CSS, a margin is the space around an element's border, while padding is the space between an element's border and the element's content. Put another way, the margin property controls the space outside an element, and the padding property controls the space inside an element.

What is the correct order of padding properties?

When three values are specified, the first padding applies to the top, the second to the right and left, the third to the bottom. When four values are specified, the paddings apply to the top, right, bottom, and left in that order (clockwise).

Can the padding property take a negative value?

Unlike margin properties, values for padding values cannot be negative. Like margin properties, percentage values for padding properties refer to the width of the generated box's containing block.


1 Answers

No, you can't. inherit means the element inherits the padding from its parent. That is, the body (or whatever element the p sits in), not the "original" p in the stylesheet. To leave the left and right padding intact, all you can do is use the two properties as you described.

like image 188
Mr Lister Avatar answered Nov 17 '22 03:11

Mr Lister