This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I'm trying to show a text inside a circle and I found the following code.
The problem is that I would like:
the circle aligned horizontally in the center of the page
the text aligned vertically in the middle of the circle
JsFiddle here
code:
.fancy {
/* Within a circle, centered text looks prettier. */
text-align: center;
/* Let's avoid our text touching the border. As
our text will still flow in a square, it looks
nicer that way, giving the feeling that it's a "real"
circle. */
padding: 1em;
/* The border will make the circle visible.
You could also use a background, as
backgrounds are clipped by border radius */
border: 0.5em solid black;
/* Let's make sure we have a square.
If it's not a square, we'll get an
ellipsis rather than a circle ;) */
width: 8em;
height: 8em;
/* and let's turn the square into a circle */
border-radius: 100%;
vertical-align: middle;
}
<div class="fancy">something</div>
You can use a flexbox to center the circle, and the text inside:
body {
display: flex;
justify-content: center;
align-items: center;
margin: 0;
height: 100vh; /* so the body would fill the height */
}
.fancy {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
padding: 1em;
border: 0.5em solid black;
width: 8em;
height: 8em;
border-radius: 100%;
vertical-align: middle;
justify-content: center;
}
<div class="fancy">something</div>
If you want to center the element without styling the body, you can use absolute positoning and transform:
.fancy {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
padding: 1em;
border: 0.5em solid black;
width: 8em;
height: 8em;
border-radius: 100%;
vertical-align: middle;
justify-content: center;
}
<div class="fancy">something</div>
You can use line-height to where line-height will be equal to the full height to center it
.fancy {
/* Within a circle, centered text looks prettier. */
text-align: center;
line-height: 8em;
/* Let's avoid our text touching the border. As
our text will still flow in a square, it looks
nicer that way, giving the feeling that it's a "real"
circle. */
padding: 1em;
/* The border will make the circle visible.
You could also use a background, as
backgrounds are clipped by border radius */
border: 0.5em solid black;
/* Let's make sure we have a square.
If it's not a square, we'll get an
ellipsis rather than a circle ;) */
width: 8em;
height: 8em;
/* and let's turn the square into a circle */
border-radius: 100%;
vertical-align: middle;
}
<div class="fancy">something</div>
Related
I used display: flex and justify-content: center to center the img within the div, it did in fact center the img but it shrank the img to a very small size. How do I center the img without having it shrink? I included a picture of this. I also tried re-sizing it with .img-star img { width: 3em; }, but it doesn't work. Pls help thanks.
.img-star {
border: 1px solid white;
padding: 20px;
margin: 30px;
border-radius: 50%;
width: 45px;
height: 45px;
display: flex;
justify-content: center;
}
Not use flex. Use this CSS:
div {
text-align: center;
}
Don't use flex. just use this on your div
div {
text-align: center
}
Adding the align-items: center property could potentially fix it. It will also center the image vertically, if that is wanted.
I tried it with a div and it worked as intended It also appears to be fixing distorted images, such as this answer
img-star {
border: 1px solid white;
padding: 20px;
margin: 30px;
border-radius: 50%;
width: 45px;
height: 45px;
display: flex;
justify-content: center;
align-items: center;
}
set img display to block and set left + right margins to auto, e.g.
.img-star {
margin: 30px auto;
display: block;
padding: 20px;
border: 1px solid white;
border-radius: 50%;
width: 45px;
height: 45px;
}
You have also set the width of this class at 45px, so when this class is applied to an img it will be shrinking it to 45px. Try upping the width and height. width: 100%; height: 100%; will preserve your original image size to the max size of the block the image is in. Changing it based on px or em will set it to that e.g. width: 300px;.
This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I'm building this app where I want this title centered. I want it such that when I hover my mouse hover the text it increases the font and changes color. At first I chose to use <div> but since it occupies an entire line, the text would get highlighted when I would hover the mouse not necessarily over the text but on any point of the line. So then I decided to use <span>and ran into the problem stated.
So I have this:
.welcome {
border-width: 4px;
border-style: solid;
border-color: red;
border-radius: 50px;
position: fixed;
top: 50%;
left: 50%;
margin-top: -310px;
margin-left: -600px;
height: 600px;
width: 1200px;
}
.button {
color: green;
}
.button:hover {
transform: scale(1.2);
color: blue;
}
.title {
font-size: 120px;
/*
float: center;
align-content: center;
text-align: center;
*/
position: relative;
top: 35%;
}
<div class="welcome">
<span class="button title"> Mancala </span>
</div>
The part which is commented was my last try to center "Mancala", i.e., the span element.
I'm using two classes (button and title) because I will have multiple elements where I would them to highlight when hovered.
Thanks in advance for the help!
Upon debugging your code, here's a solution. Replace your CSS code with this. What I did is I used the flex property. Since .welcome had a width of 1200px and using the commands display: flex; and justify-content: center; all of the content which was in the .welcome div will get centered horizontally.
.welcome {
border-width: 4px;
border-style: solid;
border-color: red;
border-radius: 50px;
position: fixed;
top: 50%;
left: 50%;
margin-top: -310px;
margin-left: -600px;
height: 600px;
width: 1200px;
background: orange;
display: flex;
justify-content: center;
}
.button {
color: green;
}
.button:hover {
transform: scale(1.2);
color: blue;
}
.title {
font-size: 120px;
/*
float: center;
align-content: center;
text-align: center;
*/
position: relative;
text-align: center;
top: 35%;
}
<div class="welcome">
<span class="button title"> Mancala </span>
</div>
I thought this might work to use the div that uses welcome. Then, you can use text-align or use the flexbox to center your span tag inside the div.
I am using icoMoon fonts (https://icomoon.io/) and centre aligning horizontally and vertically inside circle. Its looking in centre in 100% and 80% of browser but when < 80% the icon is not aligning properly. Simliar case, with > 100% of browser(chrome);
Attaching screenshots:-
.uniIcon {
height: 18px;
width: 18px;
line-height: 18px;
border-radius: 100%;
text-align: center;
float: left;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.iconmoon-icon-files {
font-size: 12px;
color: #FFF;
vertical-align: sub;
padding-left: 1px;
}
<div class="uniIcon" style="background-color: #161620;"><i class="iconmoon-icon-files"></i></div>
Try setting line height for icons to align them in div center, something like
.iconmoon-icon-files {
font-size: 12px;
color: #FFF;
line-height:inherit;
}
Inherit will make line-height of icon take on the height of its containing div.
I am building a game that places emoji at various coordinates in a webpage. Sometimes, the emoji is bigger than the container it resides in. For example, here is HTML that places a house with font-size 100px inside a 50px container.
div.emoji-container {
position: absolute;
left: 200px;
top: 200px;
width: 50px;
height: 50px;
background-color: red;
}
div.emoji {
position: relative;
font-size: 100px;
line-height: 100px;
}
<div class="emoji-container">
<div class="emoji">🏠</div>
</div>
Here is the fiddle.
As you can see, the emoji is not centered on the red square.
Is there a way that I can use css to horizontally center the emoji within its container, even if the emoji is wider than the container?
In other words, if the width of the emoji is 100px and the container is 50px, then the emoji should protrude from its container by 25px on both sides.
Part of the challenge is that emoji with font size 100px has a different width on Mac, Windows, Andriod, and so on. On Mac, the width is 100px, but on Windows, the width is around 113px.
If there is no css solution, I know that I can use a JavaScript solution.
You can use flexbox for this. For demonstration i have added a transparence to the emoji, to show its center position. On top of the CSS is a custom property which you can change to test different sizes.
https://jsfiddle.net/uvf2h0sj/
Important for you is that both elements (parent and child) using the following CSS to center everything (vertical and horizontal):
display: flex;
justify-content: center;
align-items: center;
:root {
--size: 100px;
}
body {
display: flex;
height: 100vh;
padding: 0;
margin: 0;
justify-content: center;
align-items: center;
}
div.emoji-container {
width: 50px;
height: 50px;
background-color: red;
display: flex;
justify-content: center;
align-items: center;
}
div.emoji {
font-size: var(--size);
line-height: var(--size);
width: var(--size);
height: var(--size);
opacity: 0.5;
display: flex;
justify-content: center;
align-items: center;
}
<div class="emoji-container">
<div class="emoji">🏠</div>
</div>
How would I make the table-cell 100% width of the parent.
It works perfectly fine when the text is long enough to reach the full width of the element but when the text is not it doesn't want to center whilst using table-cell and vertical-align: middle;
Here is a fiddle:
https://jsfiddle.net/DTcHh/7471/
Update:
Here's a better solution using a flex-box:
jsFiddle
h4 {
height: 50px;
font-size: 1em;
width: 100%;
font-style: oblique;
color: #FFF;
text-align: center;
margin-left: 5px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}