Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set only vertical padding?

Tags:

html

css

padding

Sometimes I would like to set top and bottom padding explicitly on an element but leave left and right padding to the unchanged browser default. I know I can write

.myElement { padding-top: 20px; }
.myElement { padding-bottom: 20px; }

but this way I need to repeat both the selector .myElement and the length value twice - or rather copy and paste the whole line and switch left with right. I was hoping to find something less redundant, so I tried to use padding with two values and replace the second length with inherit. That's not good CSS, I know, it was an attempt, but it doesn't work either (sets horizontal padding to 0):

.myElement { padding: 20px inherit; }

See here: http://jsfiddle.net/185ty3yp/

Any advice how to do it better?

like image 445
GOTO 0 Avatar asked Sep 16 '14 07:09

GOTO 0


1 Answers

If I understand what you want : you're looking for a way to only assign vertical padding with padding property (and keep original horizontal padding if it's set).

So you already have the answer :

.myElement { padding-top: 20px; padding-bottom: 20px;  }

You can't do that only with padding property, or you'll need to set horizontal value.

Or, you can consider using CSS preprocessors (such as Sass or Less), it will surely helps you to achieve this.

like image 88
enguerranws Avatar answered Sep 28 '22 04:09

enguerranws