Struggling to properly center my logo vertically

At my website, the logo in the top left corner is not centered. Here is the code I am using:

.header_col1 {
    float: left;
    width: 20%;
    background: #009240;
    text-align: center;
    position: relative;
}
.header_col1 a {    /* my logo */
    display: inline-block;
    vertical-align: middle;
}

I have read articles suggesting that this technique should work, but for some reason it's not working in this case. I am avoiding absolute positioning because the green area surrounding the logo decreases in smaller screen resolutions, and the logo size must adjust accordingly.

Answer №1

Here is a technique to achieve the desired result.

To center an image within a specific block, apply the display: table-cell property to the wrapping a element and inherit the width and height from its parent .header-col1.

You can then further align the image using vertical-align: middle and text-align: center, adding padding for extra whitespace if necessary.

To ensure the image responds well on different screen sizes, set its width to 100% and use vertical-align: top to fix any white space issues below the image.

.header_col1, .header_col2 {
  height: 110px;
}
.header_col1 {
  float: left;
  width: 20%;
  background: #009240 none repeat scroll 0% 0%;
  text-align: center;
  position: relative;
}
.header_col1 a {
  vertical-align: middle;
  display: table-cell;
  text-align: center;
  width: inherit;
  height: inherit;
  padding: 0 10px; /* optional if you need left/right whitespace */
}
.header_col1 a img {
  width: 100%;
  vertical-align: top;
}
<div class="header_col1">
  <a id="logo" href="#">
    <img src="http://placehold.it/262x78">
  </a>
</div>

Answer №2

Perform the display table magic trick like a pro!

 .header_col1 {
   display:table;

}
.header_col1 a {    /* my logo */
    display: table-cell;
    vertical-align: middle;
}

Answer №3

To align it in the middle, I'd utilize transforms.

.header_col1 a {    /* my emblem */
    display: inline-block;
    vertical-align: middle;
    position: relative;
    padding: .5em;
    top: 50%;
    transform: translateY(-50%);
}

UPDATE: Included padding: .5em; to provide some space around the logo on smaller screens as the container size reduces.

Answer №4

The latest trend is as follows:

.header_col1 {
   display: -webkit-box;
   display: -moz-box;
   display: -ms-flexbox;
   display: -webkit-flex;
   display: flex;

}
.header_col1 a {    /* custom logo */
    margin: auto;
}

Keep in mind, over time, the use of vendor prefixes like -webkit- -moz- ms- will become obsolete.

Check out this example: https://jsfiddle.net/w4m50ybd/1/

On another note, resizing an image to fit the container requires different techniques, but a simple solution would be width: 100%; height: auto;.

Answer №5

Give this a try

.header_col1:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em;
}

.header_col1 a {
  display: inline-block;
  vertical-align: middle;
  max-width: 100%;
}

Check out the Codepen Demo here

Answer №6

To make this technique effective, ensure the line-height matches the height of .header_col1:

.header_col1 {
    float: left;
    width: 20%;
    background: #009240;
    text-align: center;
    position: relative;
    line-height: 110px;
}
.header_col1 a {    /* my logo */
    display: inline-block;
    vertical-align: middle;
}

Make sure the image is set as a block-element:

.header_col1 a img {
    display: block;
}

Edit: Include the following for flexible sizing:

.header_col1 a {
    width: 100%;
}
.header_col1 a img {
    max-width: 100%;
}

Answer №7

I included the line-height property within the anchor tag.

  header_col1 a {
  display: inline-block;
  vertical-align: middle;
  line-height: 10;
}

Answer №8

One effective method to align a tag both vertically and horizontally is by using the table display. This approach offers a simple way to position an element, as demonstrated on your website where it worked seamlessly.

.header_col1 {
    float: left;
    width: 20%;
    background: #009240;
    text-align: center;
    position: relative;
    display: table;
}
.header_col1 a {    /* my logo */
    display: table-cell;
    text-align: center
    vertical-align: middle;
}

Alternatively, you can also utilize padding to center your element:

.header_col1 a {
  padding: 16px 0px;
}

In order to prevent any complications when adjusting the navigation size for various devices and screen resolutions, I would suggest sticking with the first solution.

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

Adding a drop shadow effect to borders using CSS is a great way to

I created this unique button design using Figma and now I want to implement it in CSS. https://i.sstatic.net/b6hNx.png Upon closer inspection, you'll notice that I included a highlight with a white shadow on the border. However, when I try to repli ...

How can I incorporate margins or adjust scaling within dompdf?

After creating a PDF using dompdf with the paper size set to A6, I am experiencing issues when trying to print it on A4 and letter paper sizes. The left and top borders are being cut off. Is there a way to generate a PDF that is compatible with A4, A6, l ...

Ways to position Bootstrap 4 navbar elements at the bottom

Struggling to adjust the alignment of the text in the navbar items using Bootstrap 4? My initial thought was to align them at the bottom of the ul and then position the ul accordingly, but I'm facing challenges with both. The objective is to have the ...

Is it possible to create a transparent colored foreground in HTML?

Looking to create a translucent foreground color on a webpage? I'm aiming to give a hint of red to the overall look of the website. Edit: Just to clarify, I am not referring to changing font colors. I want a color that can overlay the entire page wit ...

Challenges with moving images in Unslider

There seems to be an issue with the unslider-arrows on the unslider banner. The images are not sliding properly when transitioning to the next image without user input. Instead, they resize from small to full size starting at the top of the .banner div. ...

Check to see if a div element with an id that contains a numerical value has been

My HTML code contains X elements, each with an ID in the format: viewer_mX In this case, X is a number ranging from 1 to m (where m varies). I am looking to utilize JavaScript to retrieve the respective element's number X when a user clicks on one ...

Occasional jquery issue causing font to render incorrectly within iframe

Currently facing a bug that needs resolution, the issue has been identified but unsure of the solution. An iFrame is loaded with content via jQuery like this: var content = {{ json_encode($template) }}; $('#preview-iframe').contents().find(&apo ...

On smaller screens, the top placement of right sidebar links is necessary

Our website layout is organized into 3 main sections: the top part contains the main menu, followed by the main content and a sidebar specific to each page. The main content and sidebar are structured using bootstrap col-* classes. On small screens, I en ...

How Bootstrap Navigation is negatively impacting the width of the website

I am experiencing a problem with site width changes in the navigation. I have checked margins and padding, but couldn't find a solution to fix the issue. It seems like the row element is causing the problem, but I'm not sure why. Please assist me ...

Comparing adjust-leading-to with leader in Compass - what sets them apart?

Currently exploring Compass and feeling a bit puzzled about the functionality of two mixins: adjust-leading-to and leader. Would appreciate it if someone could clarify the distinction between them. ...

Safari: Contenteditable div text without spaces fails to wrap around an image when focused

In a hypothetical dialog layout, there is a div on the left side and an image on the right side. Both the div and the image start at the same vertical point. However, the text in the div is longer than the image. The desired layout is for the text to be di ...

JQuery Parallax issue not resolved

While working on a web page, I decided to implement a parallax background effect. I have three divs with the same background, which you can see in this fiddle. Interestingly, when I use different background images for each div, the parallax effect works p ...

I'm currently working on implementing custom hover animations for the navigation items in my React application, using Bootstrap nav. However, I'm facing an issue where

I'm attempting to implement a custom hover effect on my navigation items using external CSS. However, when I import it into my Navbar, the Bootstrap styles are not being overridden and the effect is not visible. Additionally, when the Navbar collapses ...

Eliminate the margin on the subnavigation menu

Hey there, I'm looking to adjust the layout of my menu buttons by removing the left and right "margins" (if that's the correct term), so they all fit neatly inside the navigation bar. https://i.sstatic.net/udes8.png I want to ensure that all th ...

Implementing CSS classes onto HTML elements following their retrieval from a database

Is there a way to add CSS classes to HTML tags when retrieving them from a database for a blog? For instance, if the database contains the following in a column named body: <p> SO is great </p> Then when outputting it in the View using s ...

Is it possible to showcase dates within input text boxes prior to POSTing the form?

I am currently working on a form that posts to a PHP script by selecting a date range. For a better understanding, you can check out my visual representation here. Before the data is posted, I would like to display the selected dates in empty boxes or inpu ...

Preventing scrollbar-induced content shifting in Material-UI components: A guide

My browser app displays content dynamically, which can vary in length compared to the screen size. This causes a vertical scrollbar to appear and disappear on desktop browsers, leading to the content shifting left and right. To prevent this, I apply style ...

When the page is resized for mobile, the Bootstrap modal shifts downwards

I am encountering an issue with a modal that pops up on my webpage when clicked. It is perfectly centered in a browser view and smaller views. However, when I resize the page for phones, the modal shifts down the page and requires scrolling to see it. How ...

Set the CSS position to fixed without having to reset the scroll position

What is the best way to change an element's CSS position to fixed without losing the current scroll position? Here is a method using JavaScript: $('.bigwidth').click(function() { $(this).css('position','fixed'); ...

Guide to selecting a checkbox using its unique identifier with a specific key press

Does anyone know how to create a script that will automatically click a checkbox when the 'c' key is pressed? I would appreciate any guidance or assistance in creating this script. This is the desired functionality: $("#cHideChat").click(); ...