Перейти к содержимому

Как расположить кнопку по центру css

  • автор:

How to center a button element using HTML and CSS

To center an HTML <button> element, you need to add certain CSS properties that will put the button at the center of your HTML web page.

Button center horizontally with text-align

The text-align property is used to specify the horizontal alignment of text in a block element.

One trick of this property is that it can also be used to set the alignment of children elements in a parent <div> tag.

For example, suppose you have a <div> tag that contains two buttons. You need to add text-align to the <div> tag as follows:

The output will be as shown below:

Button center horizontally using margin auto

Alternatively, you can also set the button in the middle of the <body> tag.

Set the display property to block and add margin: 0 auto to the <button> tag as shown below:

Here’s the result:

This is useful when your button has no parent element.

How to center button horizontally and vertically

To center a <button> horizontally and vertically, you can use a combination of display , justify-content , and align-items properties.

Suppose you have a <div> as the container of the <button> element.

  • Set the display property to flex or grid
  • Set the justify-content property as center

Use css class to make your code tidy as follows:

Next, create another CSS class rule with the align-items property and set it to center .

Also add the display property here so that you can center vertically without the center-h class.

To see the content centered vertically, let’s add one more class to set the <div> element height to 500 :

Finally, add all the classes assigned to the CSS rule above to the containing <div> tag as follows:

The output will be as shown below:

To center the button vertically but not horizontally, remove the center-h class:

Now you’ve learned how to center a <button>

And that’s how you can center an HTML button element using CSS properties!

I’ve also written more articles on CSS tricks that you may be interested in:

CSS is very useful for styling your web page elements, but it’s quite tricky to learn what rule to write and what property to use.

The guides I’ve written will help you to understand how to style HTML elements with CSS.

Thanks for reading! ��

Learn JavaScript for Beginners ��

Get the JS Basics Handbook, understand how JavaScript works and be a confident software developer.

A practical and fun way to learn JavaScript and build an application using Node.js.

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

How TO — Center a Button in DIV

Learn how to center a button element vertically and horizontally with CSS.

How To Center a Button Vertically

Example

<style>
.container <
height: 200px;
position: relative;
border: 3px solid green;
>

.vertical-center <
margin: 0;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
>
</style>

How To Center Vertically AND Horizontally

Example

<style>
.container <
height: 200px;
position: relative;
border: 3px solid green;
>

.center <
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
>
</style>

You can also use flexbox to center things:

Example

Tip: Go to our CSS Align Tutorial to learn more about aligning elements.

Tip: Go to our CSS Transform Tutorial to learn more about how to scale elements.

Tip: Go to our CSS Flexbox Tutorial to learn more flexbox.

Unlock Full Access

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

How To Center A Button In CSS

Buttons are a great way to format your CTAs or other links. Check out this easy guide on centering buttons in CSS.

Centering a button in CSS can sometimes feel like a puzzle. It’s a common task, yet it often leaves developers scratching their heads. This article will help you solve that puzzle, providing clear, easy-to-follow steps to ensure your buttons are perfectly centered every time.

Important disclosure: we’re proud affiliates of some tools mentioned in this guide. If you click an affiliate link and subsequently make a purchase, we will earn a small commission at no additional cost to you (you pay nothing extra). For more information, read our affiliate disclosure.

Understanding CSS Centering

CSS centering can be a bit of a puzzle. There are several ways to center elements, and the method you choose can depend on the context and specific requirements of your layout.

Block level elements can be centered using the margin property. By setting both the left and right margins to auto , and specifying a width, the element will center itself within its parent. Here’s an example:

For inline or inline-block elements, the text-align property can be used on the parent element to center them. This is a common method for centering text, but it works for any inline content. Here’s how you can do it:

Flexbox Centering

The Flexbox layout module provides a more efficient way to lay out, align and distribute space among items in a container. To center a single item, or a group of items in both the horizontal and vertical axis, you can use display: flex , justify-content: center , and align-items: center on the parent element.

Grid Centering

The CSS Grid layout is another powerful tool for centering. It works similarly to Flexbox but is better suited for two-dimensional layouts (rows and columns). To center items in a grid, you can use display: grid , justify-content: center , and align-items: center .

Center A Button Using Flexbox

The Flexbox layout module is a powerful tool in CSS that makes centering a breeze. It allows for easy alignment of items on both the horizontal and vertical axes.

To center a button using Flexbox, you first need to apply display: flex to the parent element. This turns the parent into a flex container and the child elements into flex items. Here’s an example:

For centering, use the justify-content property with the value center . This aligns the flex items along the horizontal line in the center of the container.

Center A Button Using Grid Layout

Using a grid allows more organization and structure in the layout. Here items can be placed in specific cells and span multiple columns or rows.

To center a button with the grid layout, you need to change the display property to Grid and set the place-items property to center center.

In this example, we’ve also set the style properties for the button.

Vertically and Horizontally Centering

This is another common method used for centering, especially when dealing with absolute positioning.

Here’s how it works:

  • position: absolute; takes the element out of the normal document flow, and positions it relative to the nearest positioned ancestor.
  • top: 50%; and left: 50%; positions the element’s top left corner at the center of the parent.
  • -ms-transform: translate(-50%, -50%); and transform: translate(-50%, -50%); then shift the element back by half of its own height and width, effectively centering the element within its parent.

This method is particularly useful when you don’t know the dimensions of the element you’re centering, or if those dimensions might change.

images

Nail the basics first, detail the details later.

Chris Anderson

Center A Button Using Text Alignment

The text-align property in CSS is a simple and effective way to center inline or inline-block elements, such as a button. This property works by aligning the text inside a block element.

To center a button using text alignment, you need to apply text-align: center to the parent element. This will center the text, and any inline or inline-block elements, within the parent. Here’s an example:

Inline and Inline-Block Elements

It’s important to note that the text-align property only works with inline or inline-block elements. If your button is a block-level element, you’ll need to change its display property to inline-block. Here’s how:

Centering Text Inside the Button

If you want to center the text inside the button as well, you can apply text-align: center directly to the button. This will center the text within the button.

MarketSplash Dasha Tkachenko

Using text alignment is a straightforward method for centering a button, especially when dealing with text or inline elements. It’s a handy tool to have in your CSS toolkit.

Troubleshooting Common Issues

While centering a button in CSS is generally straightforward, you might encounter some common issues. Here are a few troubleshooting tips to help you resolve them.

Button Not Centering

If your button is not centering as expected, check the display property of the parent element. For Flexbox and Grid layouts, the parent must have a display property of flex or grid respectively. If it’s set to block or inline-block , the centering properties won’t work. Here’s the correct setup:

Button Centering Horizontally But Not Vertically

If your button is centering horizontally but not vertically, it’s likely that the align-items property is missing or incorrect. Make sure it’s set to center :

Button Not Centering with Text-Align

If you’re using text-align: center and the button is not centering, check the display property of the button. The text-align property only works with inline or inline-block elements. If the button is a block-level element, you’ll need to change its display property:

Troubleshooting is a key part of any developer’s workflow. By understanding these common issues and their solutions, you’ll be well-equipped to center buttons in CSS.

Frequently Asked Questions

What is the difference between using text alignment and using Flexbox or Grid to center a button in CSS?

Flexbox and Grid can center a button horizontally and vertically, while text alignment can be used to center a button horizontally.

Why Isn’t My Button Centering?

If your button isn’t centering, it could be due to several reasons. The most common issue is not setting the correct display property on the parent element. For Flexbox and Grid layouts, the parent must have a display property of flex or grid respectively.

Can I Center a Button Vertically and Horizontally at the Same Time?

Yes, you can center a button both vertically and horizontally at the same time. This can be achieved using either the Flexbox or Grid layout. You need to use both justify-content: center and align-items: center on the parent element.

Can I Center Multiple Buttons at Once?

Yes, you can center multiple buttons at once. If the buttons are in the same parent element, applying the centering properties to the parent will center all the child elements, including all buttons.

If you need more CSS tips and guides? Check out the rest of our tutorials.

Which CSS property is used to center items horizontally in a flex container?

Как выровнять кнопку по центру css

Аватар пользователя Кирилл Маркеев

Для выравнивания кнопки по центру относительно родительского элемента мы можем указать следующие CSS свойства у родительского элемента:

Свойство justify-content отвечает в данном случае за выравнивание кнопки по горизонтали, а align-items — за выравнивание по вертикали.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *