Centering content vertically in CSS

I am facing an issue with aligning buttons vertically inside a div. The number of buttons may vary, but I want them to be aligned vertically in a single column. Using "vertical-align: middle" does not seem to work. How can I achieve this?

The height of the div is fixed. You can view an example at http://jsfiddle.net/WmD3L/. Any suggestions on how to align these buttons vertically would be greatly appreciated.

Answer №1

To vertically center text within a button with a fixed height, you can utilize the line-height property.

For example:

#button {
   height: 50px;
   line-height: 50px; /* Must match the height for vertical alignment */
}

Another approach is to use:

#button {
    display: table-cell;
    vertical-align: middle;
}

Unfortunately, this method may not work consistently across all browsers.

Answer №2

I believe this method is the most effective (Compatible with IE8 and above):

HTML:

<div class="block">
  <div class="centered">
    <div><input type="submit" value="ABC"/></div>
    <div><input type="submit" value="ABC"/></div>
    <div><input type="submit" value="ABC"/></div>
  </div>
</div>

CSS:

/* Can have any width and height */
.block {
  height:500px;
  text-align: center;
}

/* The invisible element, slightly moved to ensure perfect centering */
.block:before {
  content:'';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}

/* The item being centered, can have any width or height */
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
}

Demo: http://jsfiddle.net/WmD3L/13/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to create a translucent glass floating box over HTML elements, similar to the frosted glass effect seen in iOS7?

Creating an overlapping div with a frosted glass effect typically requires using an image background (like ). I am interested in having a floating div (position:fixed) that can apply a frosted glass effect over any content below it, whether it be an image ...

Need to delete the product page link on WooCommerce?

I am currently working on fixing the one-page checkout process and I need to remove a single product link from a product picture. The goal is to have only the checkout link displayed on this page, specifically within a quickview view. After trying out var ...

Obtain all text within a <div> element by utilizing the agility html package

When attempting to retrieve all <p> tags from within a <div> using the Agility HTML package, I encountered an issue where only the first <p> tag was obtained. <div id='bodayDiv'> <p> hi </p> <p> what is ...

Toggling checkboxes based on user input

My dynamic table is filled with checkboxes that can be checked or unchecked. I have created a jquery script that should change the background color of the table cell whenever a checkbox is modified. However, the script seems to have some bugs and doesn&apo ...

Read a local file using the HTML5 FileReader

I am currently working on developing an offline application that can read text from a locally stored text file. I have been researching and found that using html5 and FileReader can make this possible. My goal is to set a hard-coded relative path for the ...

Deselect the tick marks on the dropdown box for material selection

I need help implementing a function for a click event that will unselect all items. <mat-autocomplete [panelWidth]='290' panelClass="myPanelClass"> <mat-option *ngFor="let item of items" [value]="item.name&qu ...

The Angular factory function was unable to be initialized

I recently started learning how to integrate Angularjs with ROR using this helpful tutorial While working on adding a new function to retrieve all posts from the database, I encountered a problem. The page no longer loads when I run the code and the HTML ...

Command field in Javascript

I've crafted an exquisite search box for my website, but I'm struggling to make it functional and display search results. Below are the Html and CSS files pertaining to this section of my site: .searchbox{ /*setting width of the form eleme ...

Is there a rationale behind using intricate CSS classes such as page-left-column-container-box-element-header for technical purposes?

What is the reasoning behind using this specific CSS and HTML structure for styling web pages? <div class="page"> <div class="page-left-column"> <div class="page-left-column-container-box"> <div class="page-left-column-con ...

Arrange two absolute divs side by side without overlapping each other

I have a specific layout configuration: <div id="container"> <div class="left"> 1 </div> <div class="left"> 1 </div> <div class="right"> 2 </div> </div> My goal is to stack the left divs on top ...

Tips for overriding ng-disable in AngularJSUnleashing the

This is a comment box using AngularJS: <tr> <td colspan="5"><br/>Comments* <div><textarea class="form-control" rows="3" cols="60" ng-model="final_data.drc_scope" placeholder="Add comments here" ng-disabled="is_team==0 || isDis ...

Error: Code cannot be executed because the variable "sel" has not been defined in the HTML element

Every time I try to click on the div, I encounter an error message stating 'Uncaught ReferenceError: sel is not defined at HTMLDivElement.onclick' I am currently developing with Angular 8 and this error keeps popping up. I have read through simil ...

What is the best way to place two radio buttons side by side in a row?

How can I place two radio buttons in a single row with their labels, like the example shown in the picture? ...

Is there a way to keep my fixed footer container from moving while zooming in and out on a webpage

Is there a way to prevent the bottom text from shifting when zoomed in or out on the page? The rest of the content remains stable, but due to using position:absolute for this section to stay at the bottom of another div (content), it causes movement. #con ...

Having trouble viewing the initial value in an AngularJS2 inputText field

I'm having trouble displaying the initial value in inputText. I'm unsure of what mistake I'm making, but the value should be showing up as expected. Kind regards, Alper <input type="text" value="1" [(ngModel)]="Input.VSAT_input1" name= ...

Is it possible to incorporate a <div> element within a PHP code?

After successfully writing some PHP/SQL code, I encountered an issue when trying to create a new div called "content" along with applying a CSS class. The recommended approach was suggested as follows: $mydiv = '<div name="content">'; $myn ...

Send a parameter to be used as a link attribute in a pop-up box

I am seeking a way to pass a URL for my modal that relies on extracting the adder variable from the main HTML document. While I understand how to pass variables as text (using spans) to a modal, I am unsure how to incorporate a variable in an anchor <a ...

Experienced an unexpected setback with the absence of the right-click capability on a Javascript-powered hyperlink, specialized for

I am facing an issue with a hyperlink on my website. This particular hyperlink submits a hidden form using the POST method to redirect users to another site. However, when someone right-clicks on this hyperlink and tries to open it in a new tab, they are o ...

What methods can a web server or website use to trigger the re-caching of its own content?

We have recently launched a simple HTML and JS website that requires frequent updates to be reflected to users quickly. However, we are facing challenges with users having to clear their caches. We are exploring options within the HTML file or the Apache w ...

Ensure that the class is consistently assigned with identical col-md-* and col-lg-* properties across all instances

Bootstrap 5.2 is my framework of choice for targeting github pages, and here is a snippet of my code: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" ...