Centering an image (svg) vertically inside a link tag

I've been struggling to vertically center the config button (#config) and none of the options I've tried worked the way I wanted. Here is a link to my CodePen:

http://codepen.io/fenwil/pen/ONRXwz

Here is the crucial part of the code that needs modification:

HTML

<header id="cabecera">
    <img id="logo" alt="Mayor Igual a Siete" src="http://imgh.us/logo_207.svg">
    <h3 id="user">Username</h3>
    <a href="perfil.html"><img alt="Configuracion" src="http://imgh.us/config.svg" id="config"></a>
</header>

CSS

#config {
    transition: all 0.5s ease;
    height: 20px;
    width: 20px;
}

Appreciate any help in advance! And if there are other issues you spot, please let me know!

Answer №1

What happens when you introduce a new line like this?

#customize{
  padding-top: 20px;
}

Answer №2

To achieve the desired layout, it is recommended to remove floats and instead use display:inline-block along with vertical-align:middle.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body,
html {
  font-family: 'Roboto', sans-serif;
  height: 100%;
  overflow-y: hidden;
}
a {
  text-decoration: none;
}
#cabecera {
  text-align: right;
  margin-top:50px; /* for demo only */
}
#cabecera,
a {
  color: #FFFFFF;
}
#cabecera {
  background-color: #393E46;
  line-height: 50px;
  height: 50px;
}
#cabecera * {
  margin: 0px 4px 0px 4px;
}
#user,
#config {
  display: inline-block;
  vertical-align: middle;
}
#logo {
  height: 50px;
  width: 55px;
  float: left;
}
#config {
  transition: all 0.5s ease;
  height: 20px;
  width: 20px;
}
#config:hover {
  transform: rotate(90deg);
}
#user {
  font-weight: 100;
  font-size: 18px;
}
<header id="cabecera">
  <img id="logo" alt="Mayor Igual a Siete" src="http://imgh.us/logo_207.svg">
  <h3 id="user">Username</h3>
  <a href="perfil.html">
    <img alt="Configuracion" src="http://imgh.us/config.svg" id="config">
  </a>
</header>

Answer №3

For easy centering of any element:

.container
{
    position: relative;
}

.element
{
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Remove unnecessary directions


Explanation: Using top/left percentage positioning adjusts based on the size of the parent container, shifting the upper left corner of the inner element to the center of the parent. To perfectly center the element, utilize transform: translate with -50% to adjust based on the element's own size resulting in a precise center alignment.

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

Incorporating multidimensional arrays into the `<input type=checkbox>` element

Hey there, I've set up my form in HTML format like this: <input type="checkbox" name="opt[]" value="<?php echo $option['optionname']?>"> Currently, I'm processing the above option[] as an array with just the 'optionn ...

output a string from the write_html function in plotly

I am currently attempting to utilize plotly express to generate a string representation of a div object in HTML for integration with other packages. Although I have been following the documentation (Plotly v5.17.0), it seems that my attempts have not been ...

Create two floating divs that adjust their height dynamically to be equal

Situation: I have a container with two direct children, known as "left" and "right". The height of the "left" child should never exceed the height of the "right" child. However, the "right" child can extend beyond the height of the "left" child. I am stru ...

Creating SVG Symbol Sprites with linearGradient

Having an SVG file in my possession <svg preserveAspectRatio="xMidYMid meet" focusable="false" width="0" height="0" visibility="hidden" style=" visibility: hidden; ...

Discover the nodes with the highest connections in a D3 Force Graph

As I explore the functionalities of a D3 Force Directed Graph with zoom and pan features, I encounter an issue due to my limited knowledge of d3.js. Is there a way to estimate the number of links for each node in this scenario? I am currently at a loss on ...

Passing the value in a td element to a JavaScript function using Thymeleaf onClick

Trying to utilize "Thymeleaf" for the first time, I am attempting to pass a value to JavaScript with the following code: onclick="getPropId('${properties.id}')" The corresponding function is as follows: getPropId(inputID){alert(inputId);} Unf ...

What is the best practice for incorporating CSS and JavaScript files into a PHP template?

I've created a basic template for my PHP website, but I'm struggling to find the best way to include my CSS and JavaScript files. Take a look at my index.php file below: <?php include'core/template.php'; $temp=new Template(); $sett ...

The stacking order of React components

I am having an issue with the Z-index in my component-based React app. I currently have a list component that contains items, and I want to adjust the CSS scale and z-index to display one item in front of the others. The problem arises when the hovered it ...

What is the best way to incorporate text below grid columns in html?

Can you show me how to insert text below a column using the grid system? Here is what I am trying to accomplish Here is the code I have created: .grid{ display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-auto-rows: 240px 240px; row-g ...

What is the best way to ensure that the child flex box matches the height of the parent flex box by 100%?

As a beginner, I'm venturing into creating an experimental site for myself. However, I am facing an issue where the .menu-container does not seem to stretch 100% the height of the .container. You can view the code on jsfiddle: https://jsfiddle.net/y1 ...

Why is inner HTML returning input/textbox instead of the value?

I need help extracting the value from an input/textbox within a table cell. Although the rest of my code is functioning correctly, I'm struggling to retrieve the value from this particular input element. I've attempted to access the value using ...

Steps for creating a tileView from scratch

Have you noticed websites using a tile view layout lately? It seems like a popular trend. The concept involves having multiple elements on an HTML page with various widths and heights. These websites often use JavaScript to randomly arrange each element n ...

Display a fresh <p> tag when the condition in the if-statement is met

If you are looking for a questionnaire, check out this one. Now, I am interested in creating if-statements using HTML/CSS/jQuery to achieve the following scenario: Initially, only Question 1 will be visible. When the input matches a certain value like X, ...

Display a series of numbers in a stylish Bootstrap button with uniform dimensions

I am trying to find a way to show the number of days in a specific month within a bootstrap button. However, the code I have been using does not evenly distribute the buttons in terms of height and width. I would like the display to look similar to the sc ...

How can I prevent dataTable resizing?

Is there a solution to prevent datatable resizing? For instance, when the default number of rows per page in the datatable is set to 10 rows. If you input 13 rows into the datatable, it will be divided into 2 pages - with 10 rows on the first page and 3 ro ...

VueJS can manipulate an inline template by dynamically changing its content and reinitializing

this particular query shares similarities with the discussions on VueJS re-compiling HTML in an inline-template component as well as How to implement Vue js directive in an appended html element Regrettably, the approach suggested in those threads is no l ...

What is the best way to ensure that two contact fields are capable of adapting

Looking for a way to center two fields ("Call Us" and "Our Email") while also ensuring they are responsive on different screen sizes? The goal is to have them stack one on top of the other when viewed on a small screen. Even though inline CSS has been used ...

Is there a way for me to manually designate certain domains with the rel="follow" attribute while assigning all other external links with the rel="nofollow" attribute?

I'm working on developing a community platform similar to a social network. I've implemented a code that automatically makes all external links nofollow. However, I would like to create a feature that allows me to remove the nofollow attribute f ...

How can one locate a particular element/style/class in the dev tools DOM while debugging in MUI v5?

Exploring the DOM and pinpointing the specific component file and style block in MUI v4 is a straightforward process: Locating a particular element/style class in MUI v4 However, in MUI v5, this functionality is no longer available, making it challenging ...

PHP code isn't showing any posts

Context: I have implemented a PHP loop to generate a dynamic feed of my portfolio posts on my WordPress website. Issue: The first five posts are being displayed correctly, but the subsequent posts are not. I'm uncertain about the underlying cause and ...