CSS can't align middle text inside anchor inline-block [duplicate] - html

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 3 years ago.
I can't vertical align text inside a display block anchor that has to be 100% of container.
I try to use vertical-align: middle but seems to be ignored
https://jsfiddle.net/0cah0jcw/
.outer {
position: relative;
height: 400px;
}
.inner {
position: absolute;
top: 0;
bottom: 0;
text-align: center;
font-size: 28px;
font-weight: bold;
height: 100%;
border: 1px solid #000;
}
.inner a {
text-decoration: none;
display: inline-block;
border: 1px solid #ff0000;
padding-left: 18px;
padding-right: 18px;
height: 100%;
vertical-align: middle;
}
<div class="outer">
<nav class="inner">
link
link
link
</nav>
</div>

You can vertically align inline-block elements using a pseudo element like this:
.inner a:after {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
See demo below:
.outer {
position: relative;
height: 400px;
}
.inner {
position: absolute;
top: 0;
bottom: 0;
text-align: center;
font-size: 28px;
font-weight: bold;
height: 100%;
border: 1px solid #000;
}
.inner a {
text-decoration: none;
display: inline-block;
border: 1px solid #ff0000;
padding-left: 18px;
padding-right: 18px;
height: 100%;
vertical-align: middle;
}
.inner a:after {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
<div class="outer">
<nav class="inner">
link
link
link
</nav>
</div>
Or a more modern approach might be to use a flexbox - use display: inline-flex for an inline flexbox and align vertically using align-items: center - see demo below:
.outer {
position: relative;
height: 400px;
}
.inner {
position: absolute;
top: 0;
bottom: 0;
text-align: center;
font-size: 28px;
font-weight: bold;
height: 100%;
border: 1px solid #000;
}
.inner a {
text-decoration: none;
border: 1px solid #ff0000;
padding-left: 18px;
padding-right: 18px;
height: 100%;
display: inline-flex;
align-items: center; /*align vertically*/
}
<div class="outer">
<nav class="inner">
link
link
link
</nav>
</div>

Solution for you here:
https://jsfiddle.net/0cah0jcw/4/
I used "display: inline-flex" in order to switch to the flex model which is WAY easier to vertical align with. Then I use align-content: center to center it!
Here's the code, with fallbacks for the last 20 versions of browsers.
vertical-align: middle;
display: -webkit-inline-box;
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;

Related

Float left, right with flexbox while vertical aligned width 100%

I'm trying to vertically align my 3 items, that the reason why I use
display: flex;
align-items: center; /* center items vertically, in this case */
Not sure If it is good to practise, but now that my items are aligned correctly I cant spread the items to the left corner (menu), right corner (about), and the middle (logo). How can I do this?
Are there other alternatives to align items without flex, I have tried a lot and none of them worked so far.
canvas {
position: absolute;
background: black;
width: 100%;
height: 100%;
}
#moveItem {
z-index: 1;
opacity: 0.85;
filter: alpha(opacity=85); /* For IE8 and earlier */
}
body, html{
font-family: Arial;
margin: 0px;
height: 100%;
width: 100%;
overflow-y: hidden;
background: black;
}
#navigator {
z-index: 2;
position: relative;
padding: 0.5rem;
width: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
display: flex;
align-items: center; /* center items vertically, in this case */
}
#navIcon{
float: left;
color: white;
font-size: 30px;
cursor:pointer;
}
.about {
float: right;
color: white;
font-size: 30px;
cursor: pointer;
}
#navigator a {
text-decoration: none;
color: white;
}
.logo {
display: inline;
}
.logo img {
vertical-align: middle;
height: 50px;
width: auto;
}
.logoheader{
background: rgba(0, 0, 0, 0.5);
display: inline-block;
border-radius: 20px;
}
.logoText {
letter-spacing: 2px;
text-transform: uppercase;
vertical-align: middle;
display: inline;
font-size: 1.3rem;
margin-left: -12px;
}
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div id="navigator">
<div id="navIcon" onclick="openNav()"> ☰</div>
<div class="logoheader">
<a href="/price/">
<div class="logo"><img src="/images/logo.png"></div>
<div class="logoText">Title</div>
</a>
</div>
<div class="about">
About
</div>
</div>
</body>
Don't use float with flexbox, use justify-content and align-items instead. float should only be used for images in a text paragraph, nothing else.
Also, there are many options to align your content like that, but most of them only exist because flex wasn't always a thing, and the old ways are usually extremely hacky. grid and flex are the modern ways for CSS layouting.
Example: (you can use justify-content: space-between; on the container to achieve the desired alignment, or experiment with margin-right: auto and margin-left: auto)
canvas {
position: absolute;
background: black;
width: 100%;
height: 100%;
}
#moveItem {
z-index: 1;
opacity: 0.85;
filter: alpha(opacity=85); /* For IE8 and earlier */
}
body, html{
font-family: Arial;
margin: 0px;
height: 100%;
width: 100%;
overflow-y: hidden;
background: black;
}
#navigator {
z-index: 2;
position: relative;
padding: 0.5rem;
width: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
display: flex;
align-items: center; /* center items vertically, in this case */
justify-content: space-between;
}
#navIcon{
color: white;
font-size: 30px;
cursor:pointer;
}
.about {
color: white;
font-size: 30px;
cursor: pointer;
}
#navigator a {
text-decoration: none;
color: white;
}
.logo {
display: inline;
}
.logo img {
vertical-align: middle;
height: 50px;
width: auto;
}
.logoheader{
background: rgba(0, 0, 0, 0.5);
display: inline-block;
border-radius: 20px;
}
.logoText {
letter-spacing: 2px;
text-transform: uppercase;
vertical-align: middle;
display: inline;
font-size: 1.3rem;
margin-left: -12px;
}
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div id="navigator">
<div id="navIcon" onclick="openNav()"> ☰</div>
<div class="logoheader">
<a href="/price/">
<div class="logo"><img src="/images/logo.png"></div>
<div class="logoText">Title</div>
</a>
</div>
<div class="about">
About
</div>
</div>
</body>

CSS prev next button show at middle of image [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
CSS vertical alignment of inline/inline-block elements
(4 answers)
Closed 4 years ago.
Can anyone please tell me How can I show the icon in the middle of the image left and right side?
I tried many solutions but they not worked.
.slide {
display: inline-block;
font-size: 16px;
line-height: 20px;
padding: 7px 4px;
margin: 5px;
cursor: pointer;
height: 32px;
width: 32px;
background-color: #2874f0;
border-radius: 50%;
color: #fff;
text-align: center;
}
.image {
height: auto;
width: auto;
max-height: 350px;
max-width: 100%;
display: block;
margin: 0 auto;
}
<div style="width:100%" (click)="open_slider()">
<a class="slide">></a>
<img style="" src="https://www.vsss.co.in/Admin/uploads/472075/POLAN.jpg">
<a class="slide"><</a>
</div>
Use vertical-align: middle; to img and .slide for vertical align. And use text-align: center; to parent div for horizontal align.
Here is the working snippet.
.slide {
display: inline-block;
font-size: 16px;
line-height: 20px;
padding: 7px 4px;
margin: 5px;
cursor: pointer;
height: 32px;
width: 32px;
background-color: #2874f0;
border-radius: 50%;
color: #fff;
text-align: center;
vertical-align: middle;
}
.image {
height: auto;
width: auto;
max-height: 350px;
max-width: 100%;
display: block;
margin: 0 auto;
}
img {
display: inline-block;
vertical-align: middle;
}
<div style="width:100%; text-align: center;" (click)="open_slider()">
<a class="slide">></a>
<img style="" src="https://www.vsss.co.in/Admin/uploads/472075/POLAN.jpg">
<a class="slide">
<</a>
</div>

How to center the text larger than the container?

Here is the code:
#add_cal_in {
width: 30px;
height: 30px;
line-height: 30px;
font-size: 50px;
text-align: center;
background-color: #f38268;
display: inline-block;
vertical-align: top;
cursor: pointer;
color: #fff;
margin-left: 5px;
}
<span id="add_cal_in">M</span>
If you run this code, you can see that the text "M" is not centered as I imagined. It works fine if you change the font-size to 30px or smaller. How did that happen? And how can I center the "M" right in the middle?
Another thing is although the "M" is not HORIZONTALLY centered, it seems that the "M" is still VERTICALLY centered. BUT, if I change the "M" to a "+", it will not be centered neither HORIZONTALLY nor VERTICALLY. BTW, it works perfectly in Chrome 53, I found that after I upgraded my Chrome.
Sorry about the poor English, hope you can understand what I mean.
You can use Flexbox and if you set align-items: center and justify-content: center it will always center text in span.
span {
margin: 20px;
width: 30px;
height: 30px;
font-size: 50px;
text-align: center;
background-color: #f38268;
display: inline-block;
cursor: pointer;
color: green;
display: flex;
align-items: center;
justify-content: center;
}
<span>M</span>
<span>+</span>
<span>A</span>
Another option is to use pseudo-element to add letter and use position: absolute and transform: translate to center it.
span {
margin: 20px;
width: 30px;
height: 30px;
font-size: 50px;
text-align: center;
background-color: #f38268;
display: inline-block;
cursor: pointer;
position: relative;
}
span:after {
position: absolute;
left: 50%;
top: 50%;
content: 'M';
color: green;
transform: translate(-50%, -50%);
}
<span></span>
I think just change the width and height with 100%..
#add_cal_in {
width: 100%;
height: 100%;
line-height: 30px;
font-size: 50px;
text-align: center;
background-color: #f38268;
display: inline-block;
vertical-align: top;
cursor: pointer;
color: #fff;
margin-left: 5px;
}
<span id="add_cal_in">M</span>
You can center letter horizontally by script:
var marginLeft = ($('#add_cal_in').width() - $('.letter').width())/2;
$('.letter').css('margin-left', marginLeft+'px');
#add_cal_in {
width: 30px;
height: 30px;
line-height: 30px;
font-size: 50px;
text-align: center;
background-color: #f38268;
display: inline-block;
vertical-align: top;
cursor: pointer;
color: #fff;
margin-left: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span id="add_cal_in"><span class="letter">M</span></span>
Do you mean like this?
#add_cal_in {
width: 30px;
height: 30px;
line-height: 30px;
font-size: 50px;
text-align: center;
background-color: #f38268;
display: inline;
vertical-align: top;
cursor: pointer;
color: #fff;
margin-left: 5px;
}
<span id="add_cal_in">M</span>

Make <a> tag to take 100% height of inline-block parent

I want to make #menu a tag take up 100% of the height of its parent element. The parent element forms part of a horizontal list, so has display: inline-block set.
However, the a tag does not extend the full height of parent div, despite me setting it to display: block; height: 100%;.
See here: https://jsfiddle.net/wqec16we/12/
Would anyone know if this can be done?
#wrapper {
width: 100%;
border: 1px solid #ccc;
overflow: auto;
text-align: right;
}
#hi, #menu {
display: inline-block;
height: auto;
}
#hi {
font-size: 28px;
}
#menu {
height: 100%;
width: 60px;
}
#menu a {
display: block;
height: 100%;
width: 100%;
text-align: center;
background-color: #ccc;
}
You can add line-height propert with a value, for example,
#menu a {
display: block;
height: 100%;
width: 100%;
text-align: center;
background-color: #ccc;
line-height:40px; /*MODIFICATION*/
}
#wrapper {
width: 100%;
border: 1px solid #ccc;
overflow: auto;
text-align: right;
}
#hi, #menu {
display: inline-block;
height: auto;
}
#hi {
font-size: 28px;
}
#menu {
height: 100%;
width: 60px;
}
#menu a {
display: block;
height: 100%;
width: 100%;
text-align: center;
background-color: #ccc;
line-height:40px; /*MODIFICATION*/
}
<div id="wrapper">
<div id="hi">
HI
</div>
<div id="menu">
Menu
</div>
</div>
You can use flexbox to do the equal height columns for either known or unknown height container, also good for centering both horizontally and vertically. Example:
#wrapper {
border: 1px solid #ccc;
display: flex;
justify-content: flex-end;
}
#hi {
font-size: 28px;
padding: 0 5px;
}
#menu {
display: flex;
}
#menu a {
background-color: #ccc;
padding: 0 5px;
flex: 1;
display: flex;
justify-content: center;
align-items: center;
}
<div id="wrapper">
<div id="hi">HI</div>
<div id="menu">
Menu
</div>
</div>
If you don't need the <div> around the <a>, it can be easier. Example:
#wrapper {
border: 1px solid #ccc;
display: flex;
}
#hi {
flex: 1;
font-size: 28px;
text-align: right;
padding: 0 5px;
}
#menu {
background-color: #ccc;
padding: 0 5px;
display: flex;
justify-content: center;
align-items: center;
}
<div id="wrapper">
<div id="hi">HI</div>
<a id="menu" href="#">Menu</a>
</div>
You can also use CSS table for supporting older browsers. Example:
#wrapper {
border: 1px solid #ccc;
display: table;
width: 100%;
}
#hi,
#menu {
display: table-cell;
vertical-align: middle;
padding: 0 5px;
}
#hi {
text-align: right;
font-size: 28px;
width: 100%;
}
#menu {
background-color: #ccc;
white-space: nowrap;
}
<div id="wrapper">
<div id="hi">HI</div>
<a id="menu" href="#">Menu</a>
</div>

centering text on circle button

I want to create a button circle link with text inside it but I have problem centering the text inside the circle button. The line height is too large. Any suggestion to this problem?
Here's the code: https://jsfiddle.net/hma443rL/
.btn-donate {
background: #97c83e;
text-align: center;
width: 149px;
height: 148px;
border-radius: 100%;
display: inline-block;
font-size: 35px;
line-height: 2.3;
vertical-align:middle;
color: white;
font-weight: bold;
text-decoration: none
}
<a href="" class="btn btn-donate">
Donate <span>Us</span>
</a>
I'm trying to create a button like this
Flexbox can do that:
.btn-donate {
background: #97c83e;
text-align: center;
width: 149px;
height: 149px;
border-radius: 100%;
display: inline-flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 35px;
color: white;
font-weight: bold;
text-decoration: none
}
Donate <span>Here</span>
Use inline-blocks to line them up vertically instead of using line-height like here.
I have moved the full text inside the span in the markup
snippet below:
.btn-donate {
background: #97c83e;
text-align: center;
width: 149px;
height: 148px;
border-radius: 100%;
display: inline-block;
font-size: 35px;
vertical-align: middle;
color: white;
font-weight: bold;
text-decoration: none
}
a.btn.btn-donate span {
display: inline-block;
vertical-align: middle;
}
a.btn.btn-donate:after {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
<span>Donate Us</span>
If you add another element to your markup you can centre using a combination of relative positions and transform
.btn-donate {
background: #97c83e;
text-align: center;
width: 149px;
height: 148px;
border-radius: 100%;
display: inline-block;
font-size: 35px;
vertical-align: middle;
color: white;
font-weight: bold;
text-decoration: none
}
.wrapper {
display: block;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<a href="" class="btn btn-donate">
<span class="wrapper">Donate <span>Us</span></span>
</a>
I'm usually partial to using table display properties, but I believe it would suit your requirements here just fine. It requires very minimal adjustments to style and markup.
.btn-donate span {
vertical-align: middle;
display: table-cell;
}
.btn-donate {
background: #97c83e;
text-align: center;
width: 149px;
height: 148px;
border-radius: 100%;
display: table;
font-size: 35px;
vertical-align: middle;
color: white;
font-weight: bold;
text-decoration: none;
}
<span>Donate Us</span>