When there’s only one line of text, then I want the icon to be aligned in the centre, which is what I have achieved, however what I need to achieve now is when there’s more than one line (resizing the screen), the icon should be aligned at the top. Any ideas how I can achieve this without media queries, as the content inside the p tag will be different each time.
p {
margin: 0;
}
.rounded-container {
display: block;
position: relative;
padding: 14px;
border: 1px solid black;
border-radius: 14px;
padding-left: 60px;
background: yellow;
}
.rounded-container[data-icon-name="account"]::before {
position: absolute;
left: 14px;
top: 0;
content: "";
width: 40px;
height: 100%;
background-image: url('https://svgshare.com/i/qKT.svg');
background-repeat: no-repeat;
background-position: center;
}
<div class="rounded-container" data-icon-name="account">
<p>Please login into your account to see your paperless statements</p>
</div>
2
Answers
You have set the icon to have 100% of container height, meaning that it can never reach top when there are more lines (because you centered your icon with background tags), simply change its height to, for example,
height: 40px;
so you will have:Note that now it is at the very top, you might need to add so pixels to the
top
property to align it with the text.top: 10px
looks fine to me.You can simply add an event listener which listens for
window resize
and compare the height of each paragraph against a pre-defined constant value when theparagraph
text does not break into two lines.If you find such paragraph, you simply toggle a class for which you have pre-defined styles in your
CSS
, and walla, its done. Here is MY solution. This should work out of the box, the only thing you might need to change is thereference
, which may vary but that is solvable (you can see how the reference changes based on the window width).JavaScript:
CSS: