PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] How to vertically center text when using the :before modifier in CSS?

 November 16, 2022     css, html, vertical-alignment, vertical-text     No comments   

Issue

This is another "how do I vertically center" question in HTML. This one is when using the :before modifier.

HTML

<div style="border: 1px solid blue">
  <div class="status-yellow">vertically center me</div>
</div>

CSS

.status-yellow {
    display: inline-block;
    padding: .35em .65em;
    font-size: .75em;
    font-weight: 400;
    line-height: 1;
    color: #000;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    text-transform: uppercase;
    background-color: #fff;
}

.status-yellow:before {
    display: inline-block;
    content:"\2022";
    margin-right: 0.5rem;
    color: yellow;
    font-size: 48px;
}

Here's a CodePen showing the issue

I have a div tag and I'm using the :before modifier to inject a bullet before the text. When the bullet increases in size, the vertical centering doesn't work. When the bullet is tiny (too tiny) it's all centered. I'm missing something. What am I missing?


Solution

You can use flexbox.

.status-yellow {
    display: inline-block;
    padding: .35em .65em;
    font-size: .75em;
    font-weight: 400;
    line-height: 1;
    color: #000;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    text-transform: uppercase;
    background-color: #fff;
    display:flex;
    align-items:center;/*for vertical align*/
    justify-content:center;/*for horizontal align*/
}

.status-yellow:before {
        display: inline-block;
        content:"\2022";
        margin-right: 0.5rem;
        color: yellow;
        font-size: 48px;
    }


Answered By - Ercan Güven
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing