What is the best way to position a bigger character directly above a smaller container?

My goal is to center a single character, using CSS, in a span that is smaller than the character itself.

The setup I have is:

<span id="A">X</span><span id="B">Y</span><span id="C">Z</span>

All three spans are styled with:

display: inline-block;
width: 32px;
height: 32px;

The Y character in the middle span is larger than the span itself. My aim is to have the character centered over the span, overlapping both X and Z.

When Y is smaller than the span, vertical alignment and text centering rules work:

vertical-align: middle;
text-align: center;

However, when Y is wider than the containing box, these rules no longer apply and the Y character shifts to the right. How can I horizontally center Y when it exceeds the width of the container?

(I attempted to use display: table-cell, as suggested in other solutions, but this did not work because the width: 32px was then disregarded.)

Here is my attempted solution: https://jsfiddle.net/seehuhn/hec3xucu/

span {
  display: inline-block;
  width: 32px;
  height: 32px;
  font: bold 32px/32px courier;
  position: relative;
  vertical-align: middle;
  text-align: center;
}

#A {
  background: red;
}

#B {
  background: #8f8;
  font-size: 92px;
  color: #F99;
  z-index: 1;
}

#C {
  background: blue;
}
<p><span id="A">X</span><span id="B">Y</span><span id="C">Z</span>

Answer №1

Aligning any item with CSS flexbox is a straightforward process.

p {
  display: flex;               /* 1 */
}

span {
  width: 32px;
  height: 32px;
  text-align: center;
  font: bold 32px/32px courier;
}

#B {
  z-index: 1;
  font-size: 92px;
  color: #F99;
  background: #8f8;
  display: flex;               /* 2 */
  justify-content: center;     /* 3 */
}

#A {
  background: red;
}

#C {
  background: blue;
}
<p>
  <span id="A">X</span>
  <span id="B">Y</span>
  <span id="C">Z</span>
</p>

Insights:

  1. Establish the main container as a flex container. This allows you to utilize flex alignment properties on its child elements, while lining them up in a row by default.

  2. Transform the #B element into a nested flex container, enabling the application of flex properties to its content (which resides three levels down in the HTML structure).

  3. Utilize flex properties for horizontal alignment of the content. For vertical alignment needs, simply add align-items: center.

Answer №2

Utilize flex-box to achieve this

  span {
    display: flex;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    justify-content: center;
    -webkit-justify-content: center;
    align-items: center;
    -webkit-align-items: center;
    width: 32px;
    height: 32px;
    font: bold 32px/32px courier;
    position: relative;
    float: left;
  }

  #A {
    background: red;
  }

  #B {
    background: #8f8;
    font-size: 92px;
    color: #F99;
    z-index: 1;
  }

  #C {
    background: blue;
  }

To learn more about flex-box, check out this link.

Answer №3

You have the option to use absolute positioning for the Y element on top of the other two.

p {
  position: relative;
  display: inline-block;
  background: #8f8;
}

span:not(#B) {
  display: inline-block;
  width: 32px;
  height: 32px;
  font: bold 32px/32px courier;
  position: relative;
  vertical-align: middle;
  text-align: center;
}

#A {
  background: red;
  margin-right: 32px;
}

#B {
  font-size: 92px;
  color: #F99;
  z-index: 1;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#C {
  background: blue;
}
<p><span id="A">X</span><span id="B">Y</span><span id="C">Z</span>

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

Adjust the size of the <textarea> to match the height of the table cell

Below is the code I am using to generate a table containing an image along with a <textarea>: <table border="1" style="border-color: #a6a6a6" cellpadding="4" cellspacing="0" width="702">\ <col width="455"> <col width="230"> ...

What is the best method for deleting scripts to optimize for mobile responsiveness?

My current plugin.js file houses all my plugins for responsive design, but it is unnecessarily large and cumbersome for mobile devices. I am considering creating two separate plugin.js files to toggle between for mobile and desktop views. What are the r ...

How to Implement Autoplay Feature in YouTube Videos with React

I'm having trouble getting my video to autoplay using react. Adding autoplay=1 as a parameter isn't working. Any ideas? Below is the code I am using. <div className="video mt-5" style={{ position: "relative", paddingBot ...

tap the hyperlink to complete the form and mimic the action of a designated

I'm working on an HTML form that includes a table and two submit buttons. <input type="image" name="button1" value="First Button" src="some_image.png"> <input type="image" name="button2" value="Second Button" src="some_image.png"> One ch ...

Interactive SVG viewport dimension

Check out my "hamburger button" made in SVG, shown below. body { margin: 0; text-align: center; } svg#ham-btn { margin: 2rem; border: 1px solid black; fill: #383838; } <svg id="ham-btn" width="107.5px" height="80px" viewBox="0 0 80 80" pre ...

Errors are not being shown on mandatory fields in the form

After watching a tutorial video, I attempted to create a sign-up form. However, despite following all the steps, I encountered an issue where the Surname and Given name fields, which I set as required fields, were still processing blank when the form was s ...

Updating the image source URL dynamically inside a division within a script using JavaScript

I'm attempting to insert a dynamic URL within an img src tag. Below is my HTML and Script code. When a tab is clicked, a GET HTTP Method is called, which responds with an image URL. I want to use this image URL in the img src field. HTML Code ...

Is there a way to stop TinyMCE from adding CDATA to <script> elements and from commenting out <style> elements?

Setting aside the concerns surrounding allowing <script> content within a Web editor, I am fully aware of them. What I am interested in is permitting <style> and <script> elements within the text content. However, every time I attempt to ...

Guide to setting up npm pug-php-filter in conjunction with gulp

I'm having trouble setting up pug-php-filter (https://www.npmjs.com/package/pug-php-filter) with gulp in order to enable PHP usage in my Pug files. Any assistance would be greatly appreciated. ...

"Trouble with getting the Twitter Bootstrap dropdown menu to function properly

Currently, I am facing a challenge with my project as the dropdowns on the menu are not functioning properly. Despite having all the necessary files included, it seems like there might be an issue within my code. You can view the page here: (please try t ...

issues stemming from the css of leaflets

This is my first time using leaflet and I am encountering a CSS issue. I'm not sure if there is another CSS file for leaflet, but whenever I try to adjust its position, it changes unexpectedly. I have a floating nav bar that should appear when scroll ...

html Comparison of Documents

My goal is to compare HTML documents to see if they have the same tags in the same arrangement, regardless of different inner text and attribute values. I am only interested in comparing the general tag structure, such as: <html> <head> </h ...

Tips for horizontally aligning an item in a flexbox only when it wraps onto a new line

In my layout, I have a container that consists of text on the left and a button on the right. The challenge I'm facing is ensuring proper alignment when they are on the same line versus when the button wraps to a new line on smaller screens. Ideally, ...

Adding custom styles to an unidentified child element in React using Material-UI

When the function below is executed in a loop, I need to include styles for the icon which will be passed as an argument to the function. The icon element will be an unspecified React Material-UI Icon component. const renderStyledCard = (lightMode, headi ...

The alignment of elements on the right and left sides is not functioning as intended

I've been facing an issue with the alignment of my floats on my brand new website www.unicmedia.nl/case/oranje. Surprisingly, they seem to work fine in IE this time, but Firefox and Chrome are completely out of sync. Despite trying numerous solutions ...

"Arranging elements with identical class in a single column but on separate rows using CSS grid - a step-by-step guide

I'm facing an issue with organizing choices and matches in columns using grid CSS. Currently, the match column is overlapping with the choice column. Here is how it is coded at the moment: .grid{ display:grid; grid-template-columns: 1fr 1 ...

Here's a guide on how to display texts underneath icons in Buttons using Material UI

Currently, this is the Button I have displayed I am trying to figure out how to position the Dummy Button text beneath the icon. Can someone assist me with this? Below is my code snippet: <Button className={classes.dummyButton}> <Image src ...

Is it possible to manually trigger a version change transaction in IndexedDB?

I have been working on a Chrome extension that uses the IndexedDB to store data client-side in an IDBObjectStore within an IDBDatabase. The data structure requires users to be able to modify the object store freely, such as adding new objects or modifying ...

Utilizing Vue.js i18n for a multi-line text display

Currently, I am implementing i18n single file component in order to provide translation support for my application. In order to achieve this, I have been utilizing the i18n tag as shown below: <i18n> { "fr": { "text": "Lore ...

Having trouble with your website's container not wrapping properly?

I've run into an issue where my container is not wrapping a border around the other elements, only around the header. I checked with validators for both CSS and HTML but there are no errors being shown. Does anyone have an idea what might be causing t ...