I'm trying to display an image in an icon next to a piece of text. It sits slightly below the text and I'd like it to at least be the same height as the text aka vertically aligned to the middle.
.text {
font-family: 'Roboto', sans-serif;
font-size: 20px;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
line-height: normal;
color: black;
}
.em-image{
background-image: url(https://i.imgur.com/r8hkG1o.png);
}
[class^="em-"], [class*=" em-"], .em-png {
height: 1em;
width: 1em;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
vertical-align: middle;
}
<p class="text"><i class="em-image"></i> Name</p>
It's currently looking like this
Whereas I'd prefer it to look like this
Is there a way to at least make the image match the line height? The image sits at the bottom now but I'd like it the full height and aligned vertically to the text, I'm just not sure how to go about this, could the same be done with a span or a DIV containing the image as a background instead?
I Suggest that use vertical-align:baseline and use 'rem' instead 'em' that should work.
.text {
font-family: 'Roboto', sans-serif;
font-size: 2.5rem;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
line-height: normal;
color: black;
}
.em-image{
background-image: url(https://i.imgur.com/r8hkG1o.png);
}
[class^="em-"], [class*=" em-"], .em-png {
height: 2rem;
width: 2rem;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
vertical-align: baseline;
}
<p class="text"><i class="em-image"></i> Name</p>
I would use Flexbox and <span> instead of <p> to achieve this. With the Flexbox approach you can center all the items horizontally and vertically.
Why <span>? Because it is a generic inline container for phrasing content, which does not inherently represent anything.
.wrapper {
display: flex;
align-items: center;
font-family: 'Roboto', sans-serif;
font-size: 75px;
line-height: 1;
}
span {
margin: 0 0 0 .25em;
}
.em-image {
background-image: url(https://i.imgur.com/r8hkG1o.png);
height: 1em; /* Change this value to match the desired height */
width: 1em; /* Change this value to match the desired width */
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
}
<div class="wrapper">
<i class="em-image"></i>
<span>Name</span>
</div>
Simply, Try to find the height of the text and make max height to the icon equal to the height of the text
you may try to add this CSS code:
[class^="em-"], [class*=" em-"], .em-png {
max-height: 200px;
}
if the icon is larger smaller than the text height try to increase or decrease the icon height
i hop this answer helps
My suggestion is to use inline-flex to the class .text (instead of inline-block)
then you can just give the icon a align-self: center and it should be what you are looking for. You will probably need to adjust the spacing but should be good.
.text {
display: inline-flex;
}
[class^="em-"] {
align-self: center;
}
I'd suggest several things:
Use "em", as you were doing. "rem" will take the root font-size, and will break the component if that changes, while "em" will keep the image size proportional to the label text.
Use flexbox. It's shorter, more clear and modern.
No font actually occupies the whole vertical space. There is some extra space above and bellow. The typography you are using, Roboto, occupies approximately 80% of the vertical space, so that should be reflected in your css if want to be really precise.
Optional: the font is also not exactly centered, it has slightly more space bellow than above, so you'll need to correct this, by a factor of 2%.
The final code would be:
.text {
font-family: 'Roboto', sans-serif;
font-size: 20px;
display: flex;
align-items: center;
}
.em-image{
background-image: url(https://i.imgur.com/r8hkG1o.png);
}
[class^="em-"], [class*=" em-"], .em-png {
height: .8em;
width: .8em;
background-position: center;
background-size: contain;
margin-right: .25em;
margin-top: -.02em;
}
<p class="text"><i class="em-image"></i> Name</p>
Related
Fiddle URL: https://jsfiddle.net/e8h0jb4a/1/.
<article><span>1</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>2</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>3</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>4</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>5</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>6</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
html{
font-size: 18pt;
}
body{
background-color: black;
font-family: "PT Sans", "Arial", sans-serif;
font-size: 20pt;
color: white;
padding: 0.5rem;
margin: 0.8rem;
display: flex;
flex-flow: row wrap;
gap: 0.5rem;
justify-content: center;
}
article{
border-style: solid;
border-color: #202020;
border-width: 1.5pt;
padding: 0;
width: 9.5rem;
height: 7.5rem;
background-color: #101010;
transition: 202ms;
text-align: center;
box-sizing: border-box;
font-size: 50pt;
cursor: pointer;
}
img{
object-fit: cover;
height: 100%;
width: 100%;
display: block;
opacity: 0;
transition: 202ms;
}
article:hover{
background-color: #1f1f1f;
}
.open{
cursor: default;
}
.open span{
display: none;
}
.open img{
opacity: 1;
}
.open:hover{
transform: scale(1.8);
}
$('article').click(function() {
$(this).addClass('open');
});
You see that each card has some text on it. Once you click a card an image is shown and the text disappears. So far so good. Now I want to get the text vertically centered in each box. And I want the box which you can click for the image to be shown just the visibile box (at the moment you can click below too because of the text which takes up space).
I tried to solve this with absolute position of the text. The problem then is that the animation when you hover images brings the images below the other boxes.
How would you solve the problems? I'm looking for css only changes. Is there even a nice way with my html code or does the html code need to changed as well?
Try using Flexbox. You can use the align-items: center property to vertically align your text, and justify-content: center to horizontally align your text. You could also use text-align: center for this. Don't forget to set display: flex, otherwise, Flexbox will not be used.
This website is very useful for understanding Flexbox.
So for aligning text vertically center you can use CSS property.
article{
line-height:7.5rem; //value same as the height
}
And for hiding the image when user clicks on different element
$('article').click(function() {
for(var i=0;i<$("article").length;i++){
$("article").eq(i).children().eq(0).css("display","block");
$("article").eq(i).children().eq(1).css("opacity","0");
}
$(this).children().eq(0).css("display","none");
$(this).children().eq(1).css("opacity","1");
});
Instead of adding a class you can use this method.
Edit
You were able to click beneath the article because there was image with opacity:0; so to fix this problem you can use display:none;
Here is the code
img{
display: none;
}
.open img{
display:block;
}
And you can use the default JS code i.e
$('article').click(function() {
$(this).addClass('open');
});
Fiddle https://jsfiddle.net/vrf58L4s/5/
So vertically centering text seemed simple enough with justify-content and align-items center but when I looked closely I can see that the text isn't truly centered. It has less spacing at the top of the character. I tried to investigate further by searching online and I found this https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align but there must be a simpler solution to this.
Example
https://jsfiddle.net/z7cy487o/
html,
body {
height: 100%;
}
.box {
height: 10%;
width: 400px;
background: #000;
font-size: 11vh;
color: #FFF;
text-align: center;
padding: 0 20px;
margin: 20px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
<div class="box">
C
</div>
The way you perceive that depends on which characters you are using. I copied your example twice to show different situations:
In the second version I only used the letter "y", which has a descender, i.e. a part that extends below the baseline, to the lower border of the area which is defined as line-height. On the other hand, it doesn't go up the whole way, so it seems exactly the opposite of the first version (letter "C") concerning vertical alignment.
In the third version I used both of those letters combined in a word. Here you can see that the different characters/letters together indeed do extend across the whole width, so the vertical centering is correct as it is.
Line-height (and in relation to that, vertical alignment of letters) does not depend on which letters are used - it always applies to all possible letters/characters, even if they are not used in that particular situation.
html, body { height: 100%; }
.box
{
height: 10%;
width: 400px;
background: #000;
font-size: 11vh;
color: #FFF;
text-align: center;
padding: 0 20px;
margin: 20px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
<div class="box">
C
</div>
<div class="box">
y
</div>
<div class="box">
Cyborg
</div>
This solution based off a modified version of Center text character ☢ vertically and horizontally within a circle (CSS)
It seems to work with dynamic heights, but as Johannes mentions in the comment of his answer. I believe the solution will only work well with my situation.
html,
body {
height: 100%;
}
.box {
height: 10%;
width: 400px;
background: #000;
font-size: 11vh;
color: #FFF;
text-align: center;
padding: 0 20px;
margin: 20px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
.char {
line-height: 1px;
font-family: sans-serif;
font-weight: 500;
position: relative;
top: 0.05em;
}
<div class="box">
<span class="char">C</span>
</div>
I've read through several posts regarding both overlaying text over an image, AND on centering. None of the answers work for my situation. Below is both the HTML and the CSS I'm using. Frankly - the only thing I've found that will work to center my image UNDER my text is to revert to using tags! I'm frustrated to say the least.
It "should" be really simple, but it's not!
#header
{
height: 140px;
position: relative;
/*background: url('http://i105.photobucket.com/albums/m229/ayliea/ColeensDA_Avatar_zps7d63fb7c.jpg');
background-color: #E7D4DE; for testing background colors*/
background-repeat: no-repeat;
background-position: center top;
font-family: Segoe Script, Lucida handwriting, brush script mt,
monotype cosira, Apple Chancery, comic Sans MS Italic;
top: 10px;
}
}
#header p
{font-size: 1.25em; font-weight: normal;}
#header .imgcenter
{
display: inline-block;
margin-left: auto;
margin-right: auto;
z-index:100;
}
.topcenter
{
position: absolute;
top: -20px;
text-align: center;
width: 100%;
font-weight: bold;
color: #0200a1;
font-size: 1.5em;
text-decoration: none;
}
.topmiddle
{
position: absolute;
top:3.5em;
text-align: center;
width: 100%;
padding: 4px;
color: #c0c0c0;
font-weight: bold;
line-height:1.1em;
}
<div id="header">
<img class="imgcenter" src="http://i105.photobucket.com/albums/m229/ayliea/ColeensDA_Avatar_zps7d63fb7c.jpg" alt="Aylissa" />
<div class="topcenter">
<p>Testing My text overlay with<br />trademark symbol and link<sup>®</sup></p>
</div>
<p class="topmiddle">Second line of text that<br />
needs centered on top of image</p> </div>
But unless I use the old-fashioned tag, I just can't seem to get the darn image to both center AND either show (sometimes it disappears!) or have the text overlay it correctly!
Can anyone help me with this issue?
P.S. I used the suggestions from here: http://www.the-art-of-web.com/css/textoverimage/ to do the overlay.
Thanks for any help.
You are just missing to add text-align: center; to your #header
and, as a note, background-repeat: no-repeat; and background-position: center top; are useless properties in your css as it would just affect any image you add as background-image. It won't do anything with a html img
Second note: you can also get rid of margin-left: auto;, margin-right: auto; and z-index:100; in your .imgcenter for a cleaner css sheet (z-index won't ever work on any element unless you add a position NOT stactic in that element, position:static is the position of every single html element by default.)
What would be the easiest way to center align an inline-block element?
Ideally, I don't want to set a width to the elements. This way depending on the text inputted within the elements, the inline-block element will expand to the new width without having to change the width within the CSS. The inline-block elements should be centered on top of one another (not side by side), as well as the text within the element.
See code below or see on jsFiddle.
The current HTML:
<div>
<h2>Hello, John Doe.</h2>
<h2>Welcome and have a wonderful day.</h2>
</div>
The current SCSS:
#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);
body {
margin: 0 auto;
background: rgba(51,51,51,1);
font-family: 'Open Sans', sans-serif;
}
div {
width: 100%;
height: auto;
margin: 15% 0;
text-align: center;
h2 {
margin: 0 auto;
padding: 10px;
text-align: center;
float: left;
clear: left;
display: inline-block;
&:first-child {
color: black;
background: rgba(255,255,255,1);
}
&:last-child {
color: white;
background: rgba(117,80,161,1);
}
}
}
Adding a br between the two elements and taking out the float: left/clear: left may be the easiest way; however, I was curious if there was another way going about this.
Like this? http://jsfiddle.net/bcL023ko/3/
Remove the float:left left and add margin: 0 auto to center the element. Or is it something else that your are looking for?
I'm trying to align the text in a h1 vertically to the middle, seeing as the text might wrap it needs to look nice whether it's 1 line or 2.
This is the css I use:
h1 {
font-size: 12pt;
line-height: 10pt;
min-height: 30px;
vertical-align: middle;
}
The html is quite simply:
<h1>title</h1>
No matter what value I enter for vertical-align, the text is always at the top of the h1 element.
Am I miss-understanding the vertical-align property?
No CSS hacks needed. If I understand you correctly, then you can use this CSS:
h1 {
font-size: 12pt;
line-height: 10px;
padding: 10px 0;
}
See demo fiddle which equals a minimum height of 30px;
A note about vertical-align: that style only works in conjunction with - and is calculated with regard to - the line-height style. So setting line-height at 10px, putting text with height 12pt leaves no space to align at all. But setting line-height to 30px would result in too much space between more lines of text. This shows a trick for vertical aligning several lines of text, but that is only needed when you have a fixed height container. In this case the container's height (the h1 element) is fluid, so you can use this simple padding solution.
I dont know about vertical align, but if you add height property and set height and line-height properties same you get the vertical align: center effect
h1
{
font-size: 12pt;
line-height: 50px;
height: 50px;
}
Center the H1 title using flexbox align items center and justify content center, see this example:
div {
padding: 1em;
border: 1px dashed purple;
}
h1 {
display: flex;
align-items: center;
justify-content: center;
}
<div>
<h1>Center this h1</h1>
</div>
Just add a float property and use padding-top: 50% for example:
h1 {
font-size: 12pt;
line-height: 10pt;
min-height: 30px;
position: absolute;
float: center; /* If you want it to be centered */
padding-top: 50%;
}
I used a CSS custom property (variable) and calc
:root {
--header-height: 100px;
}
* {
margin: 0;
padding: 0;
}
header {
font-size: 16px;
height: var(--header-height);
justify-content: space-evenly;
display: flex;
border-bottom: 1px solid #000;
}
h1,i {
font-size: 1.2rem;
display: inline-block;
padding-top: calc(var(--header-height) - 1.2rem);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" rel="stylesheet"/>
<header>
<img src="https://placekitten.com/100/100" alt="logo" height="100">
<h1>
Kitten Stories
</h1>
<i class="fas fa-lock"></i>
</header>