On my webpage, I have three inline elements, and I want the second one to be centered, and the third to be aligned to the right.
<html>
<head>
<title>
Website
</title>
<style>
.inline1 {display: inline; padding-right: 10px;}
.inline2 {display: inline; text-align: center;}
.inline3 {display: inline; text-align: right; padding-left: 10px;}
</style>
</head>
<body>
<div>
<h4 class="inline1">
Inline 1
</h4>
<h4 class="inline2">
Inline 2
</h4>
<h4 class="inline3">
Inline 3
</h4>
None of these methods worked:
- using position and left in CSS;
- changing parent div display;
- changing text-align of parent div and adding margin between elements;
- changing inline elements to inline-block, flex, table, or tablecell display;
I want to keep the elements on the same line but aligned different horizontally.
4
Answers
You can do this with flex or inline-block. Check out the flex example below:
Use a flexbox with
justify-content: space-between
.Here is a complete guide to flexbox.
I’ve tried 4 methods by changing children’s width and position and making the parent display as flex and table as examples below. please have a look.
I prefer to use flexbox.
A 3 col grid – where the columns each have equal width – will allow you to place each of the elements as you wish, to left, to center, to right: