I need the image to appear on the far right and keep the text on the left. I also need to maintain the vertical alignment.
I have tried to float right on the ::after
pseudo element, but it ruins the vertical alignment. Any ideas?
JSFiddle
.holder{
width: 100%;
background: tomato;
height: 200px;
line-height: 200px;
}
.holder::after{
content: "";
border: 1px solid red;
background: transparent url('my-img.jpg') no-repeat;
width: 30px;
height: 30px;
display: inline-block;
}
<div class="holder">
Section 1
</div>
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".
CSS Demo: vertical-alignThe vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an image in a line of text. To vertically align the content of a cell in a table.
The vertical-align property in CSS controls how elements set next to each other on a line are lined up. In order for this to work, the elements need to be set along a baseline. As in, inline (e.g. <span> , <img> ) or inline-block (e.g. as set by the display property) elements.
.holder
element, set it to have position: relative
so that any children or pseudo elements can be positioned relative to itself.position: absolute
on the pseudo element. This will allow you to position it precisely within .holder
.right: 0
to the pseudo element so it shifts to the right edge of .holder
.top: 50%
. This moves it half way down .holder
. The problem at this point is its still not centred because the top edge of the pseudo element sits in the centre. So we need to move the pseudo element up by half of its own height. We can do this one of two ways: Because we know the height of the pseudo element (30px
) we can simply set a top margin of -15px
like this: margin-top: -15px;
.
Here's an example:
.holder{
width: 100%;
background: tomato;
height: 180px;
line-height: 180px;
position: relative;
}
.holder::after{
content: "";
background: blue;
width:30px;
height: 30px;
position: absolute;
right: 0;
top: 50%;
margin-top: -15px;
}
<div class="holder">
Section 1
</div>
If we don't know the height of the element we can use the translate value of the transform property to shift the pseudo element up by 50% of its height like this: transform: translateY(-50%);
(You may need to provide browser prefixes to get it working in all browsers).
Here's an example:
.holder{
width: 100%;
background: tomato;
height: 180px;
line-height: 180px;
position: relative;
}
.holder::after{
content: "";
background: blue;
width:30px;
height: 30px;
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
}
<div class="holder">
Section 1
</div>
Here is the fiddle: https://jsfiddle.net/3h71kzxe/5/
.holder{
width: 100%;
background: tomato;
height: 200px;
line-height: 200px;
position:relative; /* Added property */
}
.holder::after{
content: "";
border: 1px solid red;
background: transparent url('my-img.jpg') no-repeat;
width:30px;
height: 30px;
display: block;
position:absolute; /* Added property */
right:0; /* Added property */
top:0; /* Added property */
bottom:0; /* Added property */
margin: auto 0; /* Added property */
}
Explanation:
To shift :after element on the extreme right right:0 is added but before that we need to set it's positon as absolute and parent position needs to be relative to make absolute position work relative to its parents box.
top:0, bottom:0 and margin:0 auto;
are to make it center vertically, top:0 and bottom: 0 will stretch the :after element to make its height 100% but as we have given height 30px; and margin: 0 auto; it will bring the :after element in the vertical center.
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