Centering vertically a link inside an inline-block element

I am struggling to vertically align the text within an "a" tag in the same manner as the "button" tag. My goal is for my links and buttons to have a consistent look, but handling multiple lines of text poses a challenge when using line-height. I have tried using CSS properties like display: inline-block and vertical-align: middle, but haven't found a solution that works effortlessly across all browsers.

a,button {
  display: inline-block;
  -moz-box-sizing: border-box;
  width: 150px;
  height: 150px;
  vertical-align: middle;
  border: 1px solid #000;
  text-align: center;
}

To see my code in action, check out the jsfiddle link below:

http://jsfiddle.net/bZsaw/3/

While I have managed to achieve the desired alignment by nesting a span inside the "a" tag and setting specific display properties, this workaround does not function properly in IE7.

a,button {
    width: 150px;
    height: 150px;
    border: 1px solid #000;
    text-align: center;
}

a {
    display: table;
    -moz-box-sizing: border-box;
}

a span, button span {
    vertical-align: middle;
    text-align: center;
}

a span {
    display: table-cell; 
}

If possible, I am looking for a simple CSS-only solution that will effectively align the text within both "a" tags and "button" elements.

Answer №1

Not all answers are up to date; most of them are just workarounds. It's recommended to utilize the new CSS3 features, such as flexbox

a,button {
  -moz-box-sizing: border-box;
  width: 150px;
  height: 150px;
  display:flex;/*CSS3*/
  align-items:center;/*Vertical align*/
  justify-content:center;/*horizontal align*/
  border: 1px solid #000;
}
<a href="#"><span>Testing 1,2,3</span></a>
    <button><span>Testing 1,2,3</span></button>

This code snippet should resolve your issue. Keep in mind that align-items and justify-content behaviors will be different if you set flex-direction: vertical; by default, it is flex-direction: row.

You can freely use flexbox since all major browsers support it. Check compatibility at caniuse.com/#search=flex

For a great learning resource on flexbox, consider taking the free course at flexbox.io/. The instructor is highly recommended for his expertise.

Don't forget to explore css-grid, another exciting feature introduced in CSS3

Answer №2

To effectively align text vertically and enable text wrapping when it becomes too lengthy, I have discovered that utilizing a two-container method is the most dependable approach.

The outer container should possess a line height that is at least double the specified value for the inner container. In your situation, this translates to the following code:

a {
    width: 150px;
    height: 150px;
    border: 1px solid #000;
    text-align: center;
    line-height: 150px;
    display: block;
}

a span {
    display:inline;
    display:inline-table;
    display:inline-block;
    vertical-align:middle;
    line-height: 20px;
    *margin-top: expression(this.offsetHeight < this.parentNode.offsetHeight ? parseInt((this.parentNode.offsetHeight - this.offsetHeight) / 2) + "px" : "0");
}

If you desire all elements to be in line, include float left on the a tag. You can find an updated example with extended text within the A tag here.

You have the freedom to set the line height on the span as desired. If it is less than half of the parent's line height, the text will be centered and allow for wrapping if it exceeds the width of the parent container. This method is compatible with all modern browsers to the best of my knowledge.

Answer №3

To ensure that your text fits within the width of the box, consider adjusting the line-height to match the height of the box.

line-height:150px;

Answer №4

In my quest for the most efficient and organized method, I have come across this solution

display: grid;
align-items: center;

https://jsfiddle.net/wyqx4fn0/

Answer №5

Apply the CSS properties line-height:150px; and display-inline:block;

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

Delete an array with jQuery

I have been working with nested arrays and appending them to a div. When the button with id "name" is clicked, a movie title is stored in an array $titelBetyg, which is then added to another array called $films. Whenever a new $titelBetyg is created, I wan ...

Is there a way to seamlessly inject a stylesheet into a React application without causing any flickering or reloading on the website?

In my React application built with next.js, I am fetching a stylesheet via a GET request and appending it to the webpage. However, whenever this stylesheet is loaded in, the elements impacted by it re-render unnecessarily, even if there are no actual chang ...

Looking for guidance on how to use Python to populate an HTML document

I have a text file containing a conversation structure like this: name and date message name and date message and I already have an HTML/CSS template and a Python script that can read the text. My goal is to extract each name, date, and message from t ...

showcase 7 content sections in a two-column layout

I have a large container with 7 smaller containers inside <div id="big_div"> <div>a</div> <div>a</div> <div>a</div> <div>b</div> <div>b</div> <div>b</div><div>b</d ...

In React-bootstrap, is there a way for a custom class to take precedence over the btn and btn-danger classes without relying on IDs or !important, and only utilizing

I have been using React-bootstrap to style my buttons, however, I am facing difficulty in overriding the default btn and btn-danger classes provided by Bootstrap. Is there a way for my custom class slotTiming-box-button to take precedence over the btn and ...

Having trouble configuring the sticky-footer correctly

Currently enrolled in a web development course on Udemy, I am facing an issue with the footer on my webpage. Even after setting its CSS position to relative, the footer overlaps the content when more data is added. However, removing this positioning causes ...

Element escapes flex container boundaries

I'm facing an issue with styling a component that is taller than its flex container. My goal is to have a scroll appear for the component when it exceeds the height of the flex container, while keeping the footer at the bottom of the parent container. ...

Tips for implementing a dynamic value in the ng-style based on certain conditions

Is there a way to set a background-color conditionally using ng-style when the color value can be updated from $scope? These are the variables in my scope: $scope.someone = { id: '1', name: 'John', color: '#42a1cd' }; $scope ...

How can I align a button right next to the table row data in HTML using Bootstrap?

Is there a way to add a button next to the table data in each row without a border? I attempted to create a new column for the button, but the border interferes with the design. I am using Bootstrap 4 for this project. Here is the HTML code: <div cl ...

Utilize Boostrap to stylishly incorporate an image within a DIV using CSS

I have a project with numerous HTML pages all containing the same elements. To make updating easier, I decided to call all images from the CSS files so that if an image needs to be changed, I only need to update the CSS file rather than each individual HTM ...

Limiting the resizing boundaries in jquery ui: A step-by-step guide

Currently, I am using jquery ui to adjust the size of a div container. My goal is to restrict the resizing to a specific designated area. containment:"parent" After implementing the code, the drag operation adheres to the specified area boundaries. How ...

Following the recent update to IntelliJ IDEA 2022.1.3, the use of ::ng-deep has been marked

After updating the version of IntelliJ, I noticed that the ::ng-deep angular material selector is now marked as deprecated. Here's an example: <mat-form-field class="register-custom-select"> <mat-select formControlName="gende ...

Utilizing AngularJs and Materialize.css to create numerous dropdown menus

I am encountering a troublesome issue that I just can't seem to resolve. My current setup involves using AngularJs to showcase a set of cards, each equipped with its own dropdown menu. Here's the snippet of code in question: <div ng-repeat=&q ...

The malfunction of CSS3 effect

I designed a website and used DIV CSS to call an image. .header_bottom_area { background: url(../img/HomepagePanels.jpg)no-repeat scroll center center; max-width: 100%; width: auto; height: 911px; } I would like to have the same image as shown in ...

Is it possible to conceal glyphicons by employing the attribute hidden="hidden"?

Is it possible to conceal a glyphicon icon in this manner? <i class="glyphicon glyphicon-remove-circle" title="Please add a suitable value" id="author" hidden="hidden"></i> <p>Author</p> ...

CSS - Safari has trouble properly implementing clip-path

I'm currently working on a shape-based menu, but I'm encountering issues specifically in Safari. The layout gets distorted in Safari, and I'm struggling to understand the cause. Interestingly, when I use CSS clip-path without the SVG tag, it ...

Overflow of content from `div` on top of the window

I have a section with tabs, each tab displaying different content. One of those tabs contains a table taller than the screen size. When I open it, the section stretches and overflows from the top, extending beyond the window. Despite having overflow:auto s ...

Is it possible to make an asp:Panel extend beyond its boundaries to the right when dynamically incorporating controls into a webpage?

Currently, I am working on a solution to dynamically add controls to an asp:Panel on a website, ensuring that they all remain on the same line. Despite setting a specific width and adding overflow:auto, the controls still wrap to the next line when there i ...

Whenever I attempt to use window.print(), the styling gets completely messed up. I've experimented with both @media screen and @media print, but unfortunately, I

I am working on creating an invoice, the design looks perfect in the browser, but when I try to window.print() the style gets corrupted. I attempted to include @media screen and @media print, but I am still facing the same issue. The invoice form is incorp ...

Transform an iOS WebView into a user-friendly ebook reader

Typically, in a webview, you can scroll vertically like a browser if the page is too long. However, I am interested in transforming this experience to mimic an ebook reader. Rather than scrolling down, I would like users to be able to swipe to the next pag ...