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:
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:
I need the element to be 30px to the right of the text no matter the length. So it would look like:
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
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.
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".
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.
One simple example of using ::before pseudo-elements is to provide quotation marks. Here we use both ::before and ::after to insert quotation characters.
#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:
visibility: hidden
. (more info)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With