Tips for aligning content vertically within a header:

I'm currently working on creating a responsive header with two columns. My goal is to have the button in the right column vertically centered.

I am aware of the method for centering items using 'top: 50%; margin-top: -xy px', but my button has a fixed height of 40px.

To implement this method, I tried wrapping the button in a 'div {position: relative}'. However, this did not work because the div does not stretch its own height.

Is there a CSS solution to achieve vertical centering? Initially, I considered Flexbox as an option, but it lacks browser compatibility.

Check out the JSFIDDLE DEMO

Answer №1

One way to streamline your code is by eliminating floats and using display: inline-block instead, as well as getting rid of the unnecessary .relative div.

Check out this Working Fiddle for reference

html, body {
  margin: 0;
  padding: 0;
  font-size: 16px;
}
header {
  background: red;
  color: #fff;
  padding: 0.5em;
}
header h1 {
  font-size: 2em;
  margin: 0 0 0.2em;
}
header p {
  margin: 0;
}
header button {
  height: 40px;
  width: 70px;
}
.column-left {
  display:inline-block;
  width: 70%;
  vertical-align: middle;
}
.column-right {
  width: 29%;
  text-align: right;
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
/* responsive */
@media (min-width: 200px) {
  header {
    padding: 1em;
  }
}
@media (min-width: 300px) {
  header {
    padding: 1.5em;
  }
}
@media (min-width: 400px) {
  header {
    padding: 2em;
  }
}
@media (min-width: 500px) {
  header {
    padding: 2.5em;
  }
}
@media (min-width: 600px) {
  header {
    padding: 3em;
  }
}
/* helpers */
.clearfix:after {
    content: ".";
    clear: both;
    display: block;
    visibility: hidden;
    height: 0px;
}

Answer №2

Get rid of the position: relative in this div and instead apply it to the header tag. You can also consider removing the column-right div.

Alternatively, you could try:

header button {
  top: 50%;
  transform: translateY(-50%); // This is a better alternative to using negative margin-top (especially helpful when dealing with buttons of varying heights, compatible with IE9+ using -ms- prefix)
}

JSFIDDLE Link

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

javascript add items to a container

I created a looping animation that features dynamic words, but I'm struggling to append all the spans into the <div id"wordcloud"></div>. Any assistance with this would be greatly appreciated. var interval = 100; var width = 800; var he ...

Displaying recorded information from PHP/MySQL dropdown menu

My never-ending problem requires a simple solution. Despite searching online and in books, I can't seem to crack it. My goal is to display results from a drop-down menu that is currently connected to a database/table and showing the l_state as require ...

Is the canvas refusing to disappear?

My HTML5 Canvas element is not disappearing as expected. I wrote a timer function to hide the element when the timer hits 0. I tested this using an alert flag and it worked perfectly. Problem: if (TotalSeconds <= 0) { ctx.clearRect(0, 0, canvas.w ...

Creating a horizontal alignment for social media icons

Need assistance with aligning my social icons horizontally. I initially tried floating them to the left or using inline-block, but it's not working as expected. Any help would be greatly appreciated. Thanks! <!-- Customizing Social Network Icons ...

How to Restrict the Number of Rows Displayed in an Angular 4 Table

Currently, I am faced with a situation where I have a lengthy list of entries that I need to loop through and add a row to a table for each entry. With about 2000 entries, the rendering process is slowing down considerably. Is there a way to limit the disp ...

I'm having trouble pinpointing the origin of the gap at the top of my website, even after thoroughly reviewing all my tags

After setting margins and padding of 0 for both html and body, all my top-level elements inherited the same styling. I'm hoping to avoid using a hacky solution like adjusting the positioning. Any tips on how to achieve this without resorting to hacks ...

CSS that relies on the presence of a specific class

I'm a CSS novice and I have encountered a scenario where I need to apply a CSS class only when another class is not present. <div class="border-solid p-2 bg-white mt-1 h-fit" [ngClass]="SOME_CONDITION ? 'tile-con ...

Creating a Dynamic Clear Button for a Text Area in Angular

Working on my Angular application, I have implemented a form with a textarea element. My goal is to incorporate a clear button inside the textarea element that should: Appear only when the textarea is focused Disappear when the textarea is out of focus ( ...

there is no minimum height specified for the table

Hey there! I'm working on a dynamic table that I populate using data from my API. However, I'm facing an issue where I want the table to maintain a minimum height when there are only a few results, but I can't seem to make it work. I attemp ...

The response from my ajax call is accurate, but it does not seem to be triggering any action

My current project involves creating a like/dislike button using ajax. The data is sent to a separate file where it gets saved in a database. Upon successful completion, the response I receive back is: {"status":"success","message":"Like has been saved."," ...

Choosing each item from a lineup, one by one

Currently, I am in the process of developing a small website with multiple pages. The main requirement for this project is to have a vertical menu on the home page. Implementing the menu was relatively easy. I successfully added a hover effect to the vert ...

Resizing the background while scrolling on a mobile web browser

My website background looks great on desktop, but on mobile (using Chrome) the background starts to resize when I scroll, mainly due to the browser search bar hiding and reappearing upon scroll. Is there a way to prevent my background from resizing? Check ...

Directing a webpage to a particular moment within that page

Is it possible to automatically redirect users to a new page at a specific time? I would like visitors to be redirected to a new site starting from 12:00AM on December 31st, without allowing them to access the current site after that time. Is there a way ...

Using flexbox to adjust the height of a block in the layout

Is there a way to modify the height of .half-containers1 and .half-containers2? Additionally, how can I create the triangle shape that is shown in the image? Is it achievable with CSS alone or do I need to use an image? Check out my fiddle for reference ...

Grammarly is seamlessly inserting padding into my body element

After setting up my website on GitHub, I ran into an issue where the Grammarly Chrome extension was automatically adding a padding-top attribute to the body tag. <body data-new-gr-c-s-check-loaded="14.1028.0" data-gr-ext-installed="" ...

Command line functionality to eliminate comments in HTML minifier

I am interested in utilizing html-minifier to compress my html documents. After executing the command npm install -g html-minifier, I have successfully installed it. However, the command sudo html-minifier rb.html --removeComments did not eliminate comme ...

In PHP, the use of ul, li, class, and

I have searched high and low, but unfortunately the answers provided by others did not alleviate my problem. Instead, they caused even more confusion. I am currently working on a small project for my website and attempting to convert it to PHP in order to ...

Updating rows within a table dynamically using AJAX

I currently have a table with an empty body: <table id="tablem" border="1" width="100"> <thead> <tr> <th style="width: 10%">PageName</th> <th style="width: 5%">Lang</th> ...

What could be causing the show button to change its style every time I click it?

I'm a beginner in the world of JavaScript. I have been working on customizing the "show more" button and encountered an issue. I managed to add text and a down arrow below the button, but when I click it, another button labeled "I don't know how ...

CSS: Revert INPUT Element Back to Default Width

Although it may seem impossible, I am still going to ask the question. Suppose I have the following HTML: <div style="width: 500px;"> <input class="full" /> </div> And the corresponding CSS: input { width: 100%; } .full { ...