Jump to content

HTML/CSS - How can I align something to the centre with things either side?

pythonmegapixel
Go to solution Solved by Jobgh,

Set these rules to the parent:

display: grid;
grid-template-columns: 1fr 1fr 1fr;

Then, group your elements into three divs. Buttons on first, text on second, buttons on last.

Finally, set:

text-align: center;


On the center div, and set:

text-align: right;

on the last div.

Hi,

 

I have these buttons, which are all within a div:

image.thumb.png.12cd9d7f179aa79831d4cc64ad3705f3.png

 

 

What CSS rules do I need to style them so that:

  • The "previous", "help" and "print" buttons are aligned to the left of the div
  • The text showing the page number is centred
  • The "Save" and "Next" buttons are aligned to the right of the div?

Here is the markup for that div if it helps. (Note - the <i> tags are for the icons)

<div>
<button class="buttonPrev buttonPurple">
	<i class="bi-caret-left-fill"></i> Previous
	</button>

<button class="buttonOrange">
	<i class="bi-patch-question-fill"></i> Help
	</button>

<button class="buttonOrange">
	<i class="bi-printer-fill"></i> Print
	</button>
	
	<span class="pageNum">Page 7 of 23</span>
	
<button class="buttonGreen">
	<i class="bi-check2"></i> Save
	</button>
	
	
<button class="buttonNext buttonPurple">
	Next	<i class="bi-caret-right-fill"></i>
	</button>
</div>

 

____________________________________________________________________________________________________________________________________

 

 

____________________________________________________________________________________________________________________________________

pythonmegapixel

into tech, public transport and architecture // amateur programmer // youtuber // beginner photographer

Thanks for reading all this by the way!

By the way, my desktop is a docked laptop. Get over it, No seriously, I have an exterrnal monitor, keyboard, mouse, headset, ethernet and cooling fans all connected. Using it feels no different to a desktop, it works for several hours if the power goes out, and disconnecting just a few cables gives me something I can take on the go. There's enough power for all games I play and it even copes with basic (and some not-so-basic) video editing. Give it a go - you might just love it.

Link to comment
Share on other sites

Link to post
Share on other sites

Set these rules to the parent:

display: grid;
grid-template-columns: 1fr 1fr 1fr;

Then, group your elements into three divs. Buttons on first, text on second, buttons on last.

Finally, set:

text-align: center;


On the center div, and set:

text-align: right;

on the last div.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Jobgh said:

Set these rules to the parent:


display: grid;
grid-template-columns: 1fr 1fr 1fr;

Then, group your elements into three divs. Buttons on first, text on second, buttons on last.

Finally, set:


text-align: center;


On the center div, and set:


text-align: right;

on the last div.

Pretty good solution, but for smaller screens the grid could be improved to 1fr auto 1fr so the middle div isn't always as large as the outer 2 divs

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Jobgh said:

Set these rules to the parent:


display: grid;
grid-template-columns: 1fr 1fr 1fr;

Then, group your elements into three divs. Buttons on first, text on second, buttons on last.

Finally, set:


text-align: center;


On the center div, and set:


text-align: right;

on the last div.

That worked excellently, thanks. However, in case anyone comes across this and wants to do the same - it's necessary to specify "margin: auto;" on the left, middle and right divs; before I did that they were ever so slightly misaligned vertically, probably because of different padding amounts.

 

 

1 minute ago, mikat said:

Pretty good solution, but for smaller screens the grid could be improved to 1fr auto 1fr so the middle div isn't always as large as the outer 2 divs

I found that "auto 1fr auto" worked better - this is because "1fr auto 1fr" had the irritating side-effect of not expanding the middle div enough, causing a large gap between each edge and the first button, which "auto 1fr auto" didn't.

____________________________________________________________________________________________________________________________________

 

 

____________________________________________________________________________________________________________________________________

pythonmegapixel

into tech, public transport and architecture // amateur programmer // youtuber // beginner photographer

Thanks for reading all this by the way!

By the way, my desktop is a docked laptop. Get over it, No seriously, I have an exterrnal monitor, keyboard, mouse, headset, ethernet and cooling fans all connected. Using it feels no different to a desktop, it works for several hours if the power goes out, and disconnecting just a few cables gives me something I can take on the go. There's enough power for all games I play and it even copes with basic (and some not-so-basic) video editing. Give it a go - you might just love it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×