How to center a large character over a smaller container? - html

I am trying to center a single character, using CSS, in a span which is narrower than the character is.
I have:
<span id="A">X</span><span id="B">Y</span><span id="C">Z</span>
Where all three spans have
display: inline-block;
width: 32px;
height: 32px;
and the Y character in the middle span is larger than the span itself. I want the character centred over the span, overlapping both the X and the Z.
When the Y is smaller than the span, the following rules work for centring:
vertical-align: middle;
text-align: center;
When the Y is wider than the containing box this no longer works: the Y is now shifted to the right. How do I horizontally center Y when it is wider than the containing box?
(I have tried using display: table-cell, suggested in other answers, but this failed because the width: 32px was ignored then.)
Here is what I tried: 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>

Centering anything is quite simple with CSS flexbox.
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>
Notes:
Make the primary container a flex container. Now you can use flex alignment properties on child elements. Plus, a flex container lines up all child elements in row, by default.
Make the #B element a (nested) flex container, so you can apply flex properties to the content (which is technically three levels down in the HTML structure).
Use flex properties to horizontally center the content. (If you ever need vertical centering, add align-items: center.)

You can do that with flex-box
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;
}
If you wanna know more about flex-box click here.

You could absolutely position the Y over the other 2.
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>

Related

How can I centre my anchor tag to make my link in the centre of the page (height and width) [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 12 months ago.
I know this is a generic question, but I looked everywhere and couldn't find anything, here's my code :
<a href='/Customers' class='centre'>Start</a>
I tried encasing this in a div tag too but couldn't get that to work. I also want a small grey box around it as a background.
my css code:
a {
width: 100%;
height: 250px;
display: inline-block;
}
.centre {
text-align: center;
display: inline-block;
font-size: 20px;
font-family: 'Roboto', sans-serif;
background-color: grey;
}
Use flex. Also, use from 4 code lines specified for aligned vertically:
*{
box-sizing: border-box;
}
body{
margin: 0;
}
div{
width: 100%;
height: 200px;
background-color: grey;
display: flex;
align-items: center;
justify-content: center;
position: absolute; /*here*/
top: 50%; /*here*/
transform: translateY(-50%); /*here*/
margin: 0; /*here*/
}
.centre {
text-align: center;
line-height: 150px; /*for aligning of text vertically in anchor tag*/
background-color: red;
width: 90%;
display: inline-block;
font-size: 20px;
font-family: 'Roboto', sans-serif;
text-decoration: none;
color: white;
}
<div>
<a href='/Customers' class='centre'>Start</a>
</div>
For more knowing about flex, see the first comment.
With your given code snippet you can do it like below.
Just change display: inline-block to display: flex and add those attirbutes:
justify-content: center;
align-items: center;
.linkContainer {
display: flex;
justify-content: center;
width: 100%;
}
.button {
border-radius: 5px;
text-align: center;
width: 75px;
padding: 5px 10px;
background-color: grey;
}
.button a {
color: white;
font-size: 20px;
font-family: 'Roboto', sans-serif;
text-decoration: none;
}
<div class="linkContainer">
<div class="button">Start</div>
</div>
I did edit this code. Your link is now a "button", so it might be easier for you to understand.
But I'd recommend you to learn a bit on your own: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/HTML_basics

How to make a split left-right text on two lines?

Need to center this text in this way:
Basically, the top part to the left and the bottom part to the right, but with a bit of overlap.
How can I do that in HTML/CSS?
You can center the text vertically by using the centering with transform technique. Then the text should be separated into two lines and aligned to left/right plus a small negative margin so it overflows outside of the circle.
div {
height: 130px;
width: 130px;
background: lightblue;
position: relative;
border-radius: 50%;
}
p {
position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0;
width: 100%;
}
span {
display: block;
}
span:first-of-type {
text-align: left;
margin-left: -7px;
}
span:last-of-type {
text-align: right;
margin-right: -7px;
}
<div>
<p>
<span>La crèativitè</span>
<span>est contagieuse</span>
</p>
</div>
Simply use 2 elements for those 2 lines and add a margin-left to the second element:
.logo p {
margin: 0;
}
.logo p:nth-of-type(2) {
margin-left: 3.5em;
}
/* for demonstration purpose only */
html {
font-size: 40px;
font-family: Arial;
}
<div class="logo">
<p>La crèativitè</p>
<p>est contagieuse</p>
</div>
Another entirely different (and more complicated) approach is to use flex-box. It requires you to use containers and to know the principles of flex-box. But once you know what you're doing, it becomes fairly simple to center things inside of other things.
If you need somewhere to practice flex-box with simple games, you can visit https://flexboxfroggy.com/.
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: gray;
}
.logo {
width: 200px;
height: 200px;
background-color: maroon;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
p {
font-size: 20px;
}
#p1 {
align-self: flex-start;
}
#p2 {
align-self: flex-end;
}
<div class="container">
<div class="logo">
<p id="p1">La crèativitè</p>
<p id="p2"> est contagieuse</p>
</div>
</div>
Bonne Chance!

HTML/CSS: Font does not render vertically centered with inline-block

I have the following HTML/CSS code:
p {
background: lightgray;
}
p#h {
height: 1em;
}
span {
display: inline-block;
margin-left: .5em;
height: 1em;
width: 1em;
background: red;
}
<p>Ay<span></span></p>
<p id="h">Ay<span></span></p>
image
https://jsfiddle.net/e82gzayt/2/
How to get the inline-block having the same height as its parent?
or
How to get the font to be centered vertically with the span block?
You should add that text inside span to be inline. Also, in second case you are restricting the height of p element to be 1 em.
p {
background: lightgray;
}
p#h {
height: 1em;
}
span {
display: inline-block;
margin-left: .5em;
height: 1em;
width: 1em;
}
<p><span>Ay</span></p>
<p id="h"><span>Ay</span></p>
Check the first element. It is aligned fine now.
Fiddle - https://jsfiddle.net/e82gzayt/3/
EDIT: Removing the height and width restriction of span the text fits well inside the p block.
Demo : https://jsfiddle.net/e82gzayt/4/
You could do this with display: table-cell; and vertical-align: middle; and then wrap the two <p> tags in a <div> like this:
p#h { height: 2em; }
span {
display: inline-block;
margin-left: .5em;
height: 1em;
width: 1em;
background: red;
}
.vertical-center {
text-align: center;
display: table-cell;
vertical-align: middle;
height: 2em;
background-color: #dddddd;
width: 400px;
}
<div class="vertical-center">
<p>Ay<span></span></p>
<p id="h">Ay<span></span></p>
</div>
You can use flex value to center vertically. Advanced css class.
Try this
p { display:flex;align-items:center;background: lightgray; height: 2em;}
p#h { display:flex;align-items:center;height: 1em; }
span { display: inline-block; margin-left: .5em; height: 1em; width: 1em; background: red; }
<p>Ay<span></span></p>
<p id="h">Ay<span></span></p>
Or you can try with flex box
p { background: lightgray;
display:flex;
align-items: center;
}
p#h { height: 1em; }
span { display: inline-block; margin-left: .5em; height: 1em; width: 1em; background: red; }
https://jsfiddle.net/e82gzayt/5/

vertically center text in navigation bar

I am trying to make a navigation bar in which the buttons' text will be aligned in the center vertically.
Currently, everything is working fine with the navigation bar besides the vertical align.
I have tried many methods such as with line height, padding to the top and bottom (messes up my heights so the text divs overflow), flex, and table display.
html,
body {
height: 100%;
margin: 0px;
}
#nav {
height: 10%;
background-color: rgb(52, 152, 219);
position: fixed;
top: 0;
left: 0;
width: 100%;
color: white;
font-family: Calibri;
font-size: 200%;
text-align: center;
display: flex;
}
#nav div {
display: inline-block;
height: 100%;
align-items: stretch;
flex: 1;
}
#nav div:hover {
background-color: rgb(41, 128, 185);
cursor: pointer;
}
<div id="main">
<div id="nav">
<div><a>Home</a></div>
<div><a>Page2</a></div>
<div><a>Page3</a></div>
<div><a>Page4</a></div>
<div><a>Page5</a></div>
</div>
</div>
All help is appreciated, thank you!
You can use the table and table-cell method. Basically you need to add the css property display: table to the parent element and display: table-cell; vertical-align: middle to the children ones.
Increased height for demo purpose.
#nav {
height: 50%;
background-color: rgb(52, 152, 219);
position: fixed;
top: 0;
left: 0;
width: 100%;
color: white;
font-family: Calibri;
font-size: 200%;
text-align: center;
display: table;
}
#nav div {
display: table-cell;
height: 100%;
vertical-align: middle;
}
#nav div:hover {
background-color: rgb(41, 128, 185);
cursor: pointer;
}
<div id="main">
<div id="nav">
<div><a>Home</a>
</div>
<div><a>Page2</a>
</div>
<div><a>Page3</a>
</div>
<div><a>Page4</a>
</div>
<div><a>Page5</a>
</div>
</div>
</div>
With flexbox, you were very close.
Because a flex formatting context exists only between parent and child, your display: flex on the #nav container was reaching the divs, but not the anchors.
You need to make the individual divs flex containers, as well, so flex alignment properties can apply to the anchor elements.
html,
body {
height: 100%;
margin: 0px;
}
#nav {
height: 10%; /* This value will hide the nav bar on smaller windows */
background-color: rgb(52, 152, 219);
position: fixed;
top: 0;
left: 0;
width: 100%;
color: white;
font-family: Calibri;
font-size: 200%;
text-align: center;
display: flex; /* Will apply to child div elements, but not anchor elements */
}
#nav div {
/* display: inline-block; */
height: 100%;
align-items: stretch;
flex: 1;
display: flex; /* NEW; nested flex container */
justify-content: center; /* NEW; align anchor elements horizontally */
align-items: center; /* NEW; align anchor elements vertically */
}
#nav div:hover {
background-color: rgb(41, 128, 185);
cursor: pointer;
}
<div id="main">
<div id="nav">
<div><a>Home</a></div>
<div><a>Page2</a></div>
<div><a>Page3</a></div>
<div><a>Page4</a></div>
<div><a>Page5</a></div>
</div>
</div>

How to center :after in a div?

I am currently working on a project in which I am using some divs, and the :after pseudo-element. I would like to be able to center the :after text inside the div. Can anybody help me out here?
Example of problem: http://jsfiddle.net/JohnFish/7ra5t/
Another way to do it:
.example { ... position: relative; }
.example:after { ...
position: absolute;
top: 50%; margin-top: -{50% of the element's height};
left: 50%; margin-left: -{50% of the element's width};
}
I think you just have to add a text-align: center in your CSS:
.example {
background: black;
height: 250px;
width: 500px;
text-align: center
}
.example:after {
content: "+";
color: white;
font-size: 100px;
}
If you also want vertical alignment:
.example {
background: black;
height: 250px;
width: 500px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
.example:after {
content: "+";
color: white;
font-size: 100px;
}
You can also try with flex
.example {
display: flex;
justify-content: center;
}