Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position a pseudo :after element to the right of a list item

I am trying to position a pseudo :after element 30 pixels to the right of a menu list item.

No matter the length of the text of the item I want the element to be 30px to the right.

I have stripped the following code to the bare minimum that I think is required for this issue.

Note that this is a mobile menu. The menu and the a tag link extends the full width of the browser (phone).

#menu-main-menu-1 li {
  position: relative;
  list-style: none;
}
#menu-main-menu-1 li a {
  padding: 18px 0;
  display: block;
  text-align: center;
}
#menu-main-menu-1 a:after {
  content: "\25CF";
  position: absolute;
  right: 0;
  left: 150px;
  top: 20px;
}
<ul id="menu-main-menu-1">
  <li class="menu-item">
    <a href="#">Menu Item</a>
  </li>
  <li class="menu-item">
    <a href="#">Long Menu Item Text</a>
  </li>
</ul>

The above code produces the following result:

enter image description here

As you can see, I need to position it to the left 150px to get the element to go 30px to the right of the item. It works, but I can foresee an issue where if the menu item has a lot of text, it will surpass the 150px and the element will be in the text. For example:

enter image description here

I need the element to be 30px to the right of the text no matter the length. So it would look like:

enter image description here

Here is the JSFiddle link: JSFiddle

Please note that I have stripped many of the unnecessary styles that do not affect the functionality of this question (color, fonts, etc.)

Any help would be much appreciated!

Thanks

like image 885
cup_of Avatar asked Sep 14 '16 23:09

cup_of


People also ask

How do you position an element to the right in CSS?

If position: absolute; or position: fixed; - the right property sets the right edge of an element to a unit to the right of the right edge of its nearest positioned ancestor. If position: relative; - the right property sets the right edge of an element to a unit to the left/right of its normal position.

How do you align pseudo-elements?

You need to position the :before pseudo-element absolutely by using the well known centering technique (use the top, left, and transform properties). Here, -25px will offset the text above the circle. Note that for the <span> element, we set the position property to its "relative" value and the display to "block".

Is after a pseudo element?

In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

When would you use the :: before or :: after pseudo element in your CSS?

One simple example of using ::before pseudo-elements is to provide quotation marks. Here we use both ::before and ::after to insert quotation characters.


1 Answers

#menu-main-menu-1 li {
    list-style: none;
}
.menu-item {
    display: flex;             /* 1 */
    justify-content: center;   /* 2 */
}
.menu-item a {
    margin: 0 30px;            /* 3 */
    padding: 18px 0;
}
.menu-item::after {
    content: "\25CF";
    align-self: center;        /* 4 */
}
.menu-item::before {
    content: "\25CF";          /* 5 */
    visibility: hidden;
}
<ul id="menu-main-menu-1">
  <li class="menu-item">
    <a href="#">Menu Item</a>
  </li>
  <li class="menu-item">
    <a href="#">Long Menu Item Text</a>
  </li>
</ul>

Notes:

  1. Establish flex container (block-level; takes full width)
  2. Horizontally center child elements.
  3. Anchors/menu items have 30px horizontal margins
  4. Right-side pseudo-element is vertically-centered and always 30px to the right of menu item (regardless of text length).
  5. A second pseudo-element is added on the left for equal balance. This keeps the menu items centered in the container. It's concealed with visibility: hidden. (more info)
like image 111
Michael Benjamin Avatar answered Sep 28 '22 07:09

Michael Benjamin