Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:before, :after and padding

I have the following css-code:

.readMore:before {
    content: '';
    display: block;
    float: left;
    width: 10px;
    height: 27px;
    margin: 0;
    background: red url('images/button.png');
    background-position: 0 0;
}

.readMore {
    float: left;
    height: 24px;
    background: url('images/button.png');
    background-position: -10px 0;
    border: 0;
    margin: 0;
    padding: 4px 0 0 0;
    cursor: pointer: 
}

.readMore:after {
    content: '';
    display: block;
    float: right;
    width: 10px;
    height: 27px;
    margin: 0;
    top: -3px;
    background: red url('images/button.png');
    background-position: -411px 0;
}

Which styles a link that looks like this: enter image description here

But when trying to adjust the text in the .readMore in vertical the :before and :after images also get "jumps" down. Which is logical, but is there a solution so it will align better with the "total image"?

like image 236
JohnSmith Avatar asked Jan 07 '13 12:01

JohnSmith


People also ask

What is :: before and :: after?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.

What are before and after pseudo elements?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

What is the order for padding?

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).


1 Answers

I tend to use absolute positioning for :before and :after elements. Then you can do whatever you want to the parent without worrying about your pseudoelements going anywhere (unless, of course, you move the element itself).

View on JSFiddle

html

<div></div>

css

div {
  position: relative;
  background: #eee;
  width: 25px;
  height: 25px;
  margin: 30px 0 0 30px;
}
div:before {
  position: absolute;
  width: 10px;
  height: 25px;
  top: 0;
  left: -10px;
  content:"";
  background: #222;
}
div:after {
  position: absolute;
  width: 10px;
  height: 25px;
  top: 0;
  right: -10px;
  content:"";
  background: #222;
}

This shows how I would lay them out. You can then use any method you want to adjust the position of the text in the parent.

The key points of the above code are the following:

  1. The parent is relatively positioned. This allows us to use absolute positioning on its children, the pseudoelements, to place them in relation to their parent.
  2. The left and right position of the before and after elements, respectively, is equal to their width if you want the elements to be border-to-border.

If you want to center the text in the parent div vertically, and it's just a single line, you can set the line-height equal to the height of the container. View that here. This would be better than 'guessing' the padding to make it vertically centered, if that's what you're going for.

Of course, there are other ways to center the text vertically, too, and accordingly there are lots of SO questions on the subject. Here's just one.

like image 166
jamesplease Avatar answered Sep 18 '22 12:09

jamesplease