Put text at the bottom of a div
In order to anchor text to the bottom of a div, apply position: relative
to the parent div. For the child text element, use position: absolute
with bottom: 0
.
The CSS rules will make sure the text is sent directly to bed (a.k.a. bottom) no matter how tall the parents (div) are.
Deploying Flexbox
Flexbox is an efficient tool for item alignment. To push your disobedient text down to the bottom, use display: flex
.
For bottom-right alignment, let's try a different form of discipline:
Remember to check browser compatibility while using the mighty Flexbox.
Crack the whip with Table-cell
Using display: table-cell
with vertical-align: bottom
is good old traditional method to put the text at the bottom. It works well when you're dealing with a stubborn single-line text in the fixed-height div!
Again, tweak the line-height to coax a single line of text. Bearing gifts helps:
Precise Control with Padding
Offer more freedom by adding padding to your parent div. It creates space like a playroom for the troublesome text!
This method is like a huge playroom (your div) for your kids to play in, but they're grounded for misbehaving!
Scalability in Multiple Containers
If you have a herd of divs to deal with, use a scalable solution that works across different screen sizes and container adaptability. This makes the text behave, no matter their number. For this, apply the "big sis" container class to each div:
Make it look neat by aligning everything at the right:
Practical Demonstration
A live demo on JSFiddle or CodePen would show your naughty little text finally obeying the rules!
Issues and Alternatives
Here are techniques and issues that could send your text to either the corner or the bottom right corner of your div:
CSS Grid Technique
Just like flexbox, CSS Grid can align items vertically:
Overflow Control
Overflow of content is an overflow of tantrums. If the text refuses to stay within its boundaries, use overflow: auto;
or overflow: hidden;
on your container to maintain peace.
Responsive Design
For a great responsive design, use relative units like em
, vh
, vw
or %
instead of fixed units like px
for padding, margins, and sizes to keep your design looking the same across devices.
Was this article helpful?