How to center a character that is larger than its container - html

I have icons that I want centered both horizontally and vertically.
See codepen and snippet below:
div {
border: 1px solid black;
}
.icon-placeholder {
height: 34px;
overflow: hidden;
width: 34px;
}
.icon {
color: hotpink;
font-size: 400%;
}
.icon::before {
content: '+';
}
<div class="icon-placeholder">
<span class="icon"></span>
</div>
How can I do that regardless of .icon's font size.
I have tried transform, position: absolute, display: table with no luck. I can't use flex.

You can achieve it using transform and absolute positioning
div {
border: 1px solid black;
margin: 10px;
}
.icon-placeholder {
height: 34px;
overflow: hidden;
width: 34px;
position: relative;
}
.icon {
color: hotpink;
font-size: 400%;
margin-top: -10%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
left: 50%;
}
.icon::before {
content: '+';
}
.plus-symbol{
font-size: 400%;
outline: dotted 1px red;
color: hotpink;
}
.left, .right{
width: 45%;
padding: 20px;
box-sizing: border-box;
}
.left{
float: left;
}
.right{
float: right;
}
.custom-plus-icon, .custom-plus-icon:before{
position: absolute;
width: 10%;
border-radius: 1px;
background-color: hotpink;
height: 80%;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.custom-plus-icon:before{
content: '';
width: 100%;
height: 100%;
transform: rotate(90deg);
}
<div class="left">
<h3>Plus symbol using font</h3>
<span class="plus-symbol">+</span>
<br/>
<br/>
<label>Font-size : 400%</label>
<div class="icon-placeholder">
<span class="icon"></span>
</div>
<label>Font-size : 300%</label>
<div class="icon-placeholder">
<span class="icon" style="font-size: 300%;"></span>
</div>
<label>Font-size : 200%</label>
<div class="icon-placeholder">
<span class="icon" style="font-size: 200%;"></span>
</div>
<label>Font-size : 100%</label>
<div class="icon-placeholder">
<span class="icon" style="font-size: 100%;"></span>
</div>
</div>
<div class="left">
<h3>Plus symbol using pseudo element</h3>
<div class="icon-placeholder">
<span class="custom-plus-icon"></span>
</div>
</div>

On thing to note though, a + isn't necessarily centered in the first place depending on the font.
div {
border: 1px solid black;
}
.icon-placeholder {
height: 34px;
overflow: hidden;
position: relative;
width: 34px;
}
.icon {
color: hotpink;
font-size: 400%;
}
.icon::before {
content: '0';
display: inline-block;
position: absolute;
right: 50%;
bottom: 50%;
transform: translate(50%, 50%);
}
<div class="icon-placeholder">
<span class="icon"></span>
</div>

Related

Why position absolute affects opacity?

I have come across an interesting moment for myself and would like to figure out how it works.
Here are 2 examples in the sandbox. On one without position: absolute, the opacity is different from where position: absolute is applied. Why does positioning affect the degree of opacity in this case?
.wrapper {
position: relative;
text-align: center;
width: 300px;
}
.text-wrap {
border-bottom: 3px solid #202122FF;
opacity: 0.5;
}
.text {
right: 40%;
bottom: -7px;
position: absolute;
padding: 0 5px;
color: #202122FF;
background-color: white;
}
<div class="wrapper">
<div class="text-wrap">
<span class="text">
Some text
</span>
</div>
</div>
And the same template without position: absolute
.wrapper {
position: relative;
text-align: center;
width: 300px;
}
.text-wrap {
border-bottom: 3px solid #202122FF;
opacity: 0.5;
}
.text {
right: 40%;
bottom: -7px;
/* position: absolute; */
padding: 0 5px;
color: #202122FF;
background-color: white;
}
<div class="wrapper">
<div class="text-wrap">
<span class="text">
Some text
</span>
</div>
</div>
You have opacity set on the wrapper of that element. If the wrapper has opacity then it will transfer to the children of the element. To fix you need to move the text somewhere on the outside of the element.
.wrapper {
position: relative;
text-align: center;
width: 300px;
}
.text-wrap {
border-bottom: 3px solid #202122FF;
opacity: 0.5;
}
.text {
right: 40%;
bottom: -7px;
position: absolute;
padding: 0 5px;
color: #202122FF;
background-color: white;
}
<div class="wrapper">
<div class="text-wrap">
</div>
<span class="text">
Some text
</span>
</div>
It is not about position, but where stands the span .
.wrapper {
position: relative;
text-align: center;
width: 300px;
}
.text-wrap {
border-bottom: 3px solid #202122FF;
opacity: 0.5;
line-height:0/* span/text will overflow now */
}
.text {
right: 40%;/* no effect on static position*/
bottom: -7px;/* no effect on static position*/
/* position: absolute; */
padding: 0 5px;
color: #202122FF;
background-color: white;
}
<div class="wrapper">
<div class="text-wrap">
<span class="text">
Some text
</span>
</div>
</div>

Container loses top margin when element is made visible

I was trying to participate in a CSS challenge when this occurred. Everything seems working as expected, however when clicking on the plus circle to display div#card the div.container loses it's top margin and I cannot understand why. Please if anyone can help me with this, I'd be really grateful - And maybe we can all learn from it ;)
Thanks!
Codepen: https://codepen.io/albertrf147/pen/LMKKeK
HTML
html,
body {
width: 100%;
height: 100%;
}
body {
margin: 0px;
overflow: hidden;
background: lightblue;
}
.container {
width: 400px;
height: 400px;
margin: 20px auto !important;
position: relative;
display: flex;
flex-wrap: wrap;
background: white;
padding: 2px;
}
.square {
box-sizing: border-box;
padding: 2px;
width: 50%;
height: 50%;
position: relative;
background: white;
}
.square>img {
height: 100%;
width: 100%;
object-fit: cover;
display: block;
margin: auto;
}
.onhover {
box-sizing: border-box;
width: 100%;
height: 100%;
position: absolute;
cursor: pointer;
transition: all .6s ease-in-out;
}
.onhover:hover {
background: rgba(0, 0, 0, 0.5);
}
.circle-aux {
position: relative;
width: 100%;
height: 100%;
transition: all .6s ease-in-out;
}
.circle-aux:hover .circle {
visibility: visible;
opacity: 1;
transform: scale(0.2);
transition: all .6s ease-in-out;
}
.circle {
visibility: hidden;
opacity: 0;
position: absolute;
border-radius: 50%;
width: 100%;
height: 100%;
background: salmon;
}
.circle:before {
content: "";
background: white;
height: 50%;
width: 6px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.circle:after {
content: "";
background: white;
height: 6px;
width: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container-card {
visibility: hidden;
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.container-card>img {
width: 100%;
height: 60%;
object-fit: cover;
}
.container-card:target {
visibility: visible;
}
.avatar {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
box-shadow: 0px 0px 20px black;
width: 25%;
height: 25%;
background: white;
z-index: 2;
text-align: center;
font-family: calibri;
font-weight: bold;
color: white;
font-size: 18px;
}
.avatar>img {
box-sizing: border-box;
width: 100%;
height: 100%;
object-fit: cover;
border-radius: inherit;
padding: 4px;
}
.mini-circle {
display: inline-block;
width: 15%;
height: 15%;
border: 1px solid white;
border-radius: 50%;
margin-top: 12px;
}
.mini-circle:hover {
background: white;
cursor: pointer;
}
footer {
box-sizing: border-box;
position: absolute;
bottom: 0;
left: 0;
background: salmon;
height: 50%;
width: 100%;
z-index: 1;
padding: 2px;
}
.close {
position: absolute;
border-radius: 50%;
width: 8%;
height: 8%;
background: black;
transform: rotate(45deg);
right: 10px;
top: 10px;
cursor: pointer;
}
.close:before {
content: "";
background: white;
height: 60%;
width: 2px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.close:after {
content: "";
background: white;
height: 2px;
width: 60%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<main>
<div class="container">
<div class="square">
<div class="onhover">
<div class="circle-aux">
<a class="circle" href="#card"></a>
</div>
</div>
<img src="https://images.pexels.com/photos/305241/pexels-photo-305241.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div class="square">
<div class="onhover">
<div class="circle-aux">
<div class="circle"></div>
</div>
</div>
<img src="https://images.pexels.com/photos/1546711/pexels-photo-1546711.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div class="square">
<div class="onhover">
<div class="circle-aux">
<div class="circle"></div>
</div>
</div>
<img src="https://images.pexels.com/photos/1800433/pexels-photo-1800433.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div class="square">
<div class="onhover">
<div class="circle-aux">
<div class="circle"></div>
</div>
</div>
<img src="https://images.pexels.com/photos/1757111/pexels-photo-1757111.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div id="card" class="container-card">
<a class="close" href="#"></a>
<img src="https://images.pexels.com/photos/1769331/pexels-photo-1769331.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
<div class="avatar">
<img src="https://images.pexels.com/photos/769772/pexels-photo-769772.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
<span>David Craig</span>
<span>
<div class="mini-circle"></div>
<div class="mini-circle"></div>
<div class="mini-circle"></div>
</span>
</div>
<footer></footer>
</div>
</div>
</main>
Welcome to SO!
As the appearing container is position: absolute no margin takes effect.
You need to work with the top attribute to place it properly.
Check this out: https://codepen.io/anon/pen/pGzVyL
For more informations click here: https://www.w3schools.com/css/css_positioning.asp

How to make left and right arrow in css?

I have a screenshot as shown below which I have to replicate in HTML/CSS. In the following screenshot I wasn't able to make colored left and right arrows in between the squares.
At this moment, I am able to replicate this in fiddle without colored left and right arrows.
The snippets of CSS code which I have used in order to make series of small squares are:
.squares .square {
width: 200px;
text-align: center;
height: 200px;
background-color: white;
border-radius: 15px;
}
Problem Statement:
I am wondering what CSS codes I should add in the fiddle so that colored left and right arrows in between the squares shows up in the fiddle.
You can do that by HTML special characters like below:
.squares {
display: flex;
justify-content: space-between;
align-items:center;
padding: 1rem;
background-color: rgb(238, 238, 238);
flex-wrap: wrap;
}
.squares .square {
width: 200px;
text-align: center;
height: 200px;
background-color: white;
border-radius: 15px;
}
.squares .square p
{
text-align: center;
vertical-align: bottom;
}
.arrows {
text-align:center;
}
.arrows span {
display:block;
font-size:48px;
line-height:32px;
color:green;
font-weight:bold;
}
.arrows span:first-child {
color:#C90;
}
<div class="squares">
<div class="square"><p>Franchise Hub Hierarchy</p><img src="https://s7.postimg.cc/jvu89zp17/Layer_30.png" alt=""/></div>
<div class="arrows">
<span>→</span>
<span>←</span>
</div>
<div class="square"><p>System wide user permissions</p><img src="https://s7.postimg.cc/6ronxc7a3/Layer_33.png" alt=""/></div>
<div class="arrows">
<span>→</span>
<span>←</span>
</div>
<div class="square"><p>Custom Corporate Branding</p><img src="https://s7.postimg.cc/wn8egkbor/Layer_46.png" alt=""/></div>
<div class="arrows">
<span>→</span>
<span>←</span>
</div>
<div class="square"><p>Configurable Workflow</p><img src="https://s7.postimg.cc/k8lmg8rwb/Layer_47.png" alt=""/></div>
<div class="arrows">
<span>→</span>
<span>←</span>
</div>
<div class="square"><p>Orders, C.R.M. and P.O.S</p><img src="https://s7.postimg.cc/9yj7h0hgb/Shape_33.png" alt=""/></div>
</div>
You can try font awesome arrows
https://fontawesome.com/icons/arrow-left?style=solid
or make arrows using CSS borders
#arrow {
width: 0;
height: 0;
border-top: 20px solid transparent;
border-left: 40px solid red;
border-bottom: 20px solid transparent;
margin-left: 20px;
}
#arrow:before {
content: "";
position: absolute;
width: 30px ;
height: 10px;
background: red;
left: 0;
top: 22px;
}
<div id="arrow"></div>
and if you want your text towards the bottom of your box, You should write <p> after <img>.
<div class="square">
<img src="https://s7.postimg.cc/jvu89zp17/Layer_30.png" alt=""/>
<p>Franchise Hub Hierarchy</p>
</div>
Here is the cure CSS and simplest solution for your problem.
I have done this for you, just add a div between every square div
<div class="arrowWrapper">
<span class="arrow redArrow"></span><br>
<span class="arrow greenArrow"></span>
</div>
and this css to your code:
.arrow {
display:block;
height: 2px;
margin: 3px 0;
width: 35px;
position: relative;
}
.arrow.redArrow {background-color: red;}
.arrow.greenArrow {background-color: green;}
.arrowWrapper { margin-right: -20px; margin-left: -20px;}
.arrow.redArrow:after{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: red;
right: -1px;
transform: rotate(45deg);
top: -4px;
}
.arrow.redArrow:before{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: red;
right: -1px;
transform: rotate(135deg);
top: 4px;
}
.arrow.greenArrow:after{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: green;
left: -1px;
transform: rotate(135deg);
top: -4px;
}
.arrow.greenArrow:before{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: green;
left: -1px;
transform: rotate(45deg);
top: 4px;
}
Complete code is in below snippetenjoy
.squares {
display: flex;
justify-content: space-between;
align-items:center;
padding: 1rem;
background-color: rgb(238, 238, 238);
flex-wrap: wrap;
}
.squares .square {
width: 200px;
text-align: center;
height: 200px;
background-color: white;
border-radius: 15px;
}
.squares .square p
{
text-align: center;
vertical-align: bottom;
}
.arrow {
display:block;
height: 2px;
margin: 3px 0;
width: 35px;
position: relative;
}
.arrow.redArrow {background-color: red;}
.arrow.greenArrow {background-color: green;}
.arrowWrapper { margin-right: -20px; margin-left: -20px;}
.arrow.redArrow:after{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: red;
right: -1px;
transform: rotate(45deg);
top: -4px;
}
.arrow.redArrow:before{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: red;
right: -1px;
transform: rotate(135deg);
top: 4px;
}
.arrow.greenArrow:after{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: green;
left: -1px;
transform: rotate(135deg);
top: -4px;
}
.arrow.greenArrow:before{
position: absolute;
content: "";
height: 2px;
width: 12px;
background: green;
left: -1px;
transform: rotate(45deg);
top: 4px;
}
<div class="squares">
<div class="square"><p>Franchise Hub Hierarchy</p><img src="https://s7.postimg.cc/jvu89zp17/Layer_30.png" alt=""/></div>
<div class="arrowWrapper">
<span class="arrow redArrow"></span><br>
<span class="arrow greenArrow"></span></div>
<div class="square"><p>System wide user permissions</p><img src="https://s7.postimg.cc/6ronxc7a3/Layer_33.png" alt=""/></div>
<div class="square"><p>Custom Corporate Branding</p><img src="https://s7.postimg.cc/wn8egkbor/Layer_46.png" alt=""/></div>
<div class="square"><p>Configurable Workflow</p><img src="https://s7.postimg.cc/k8lmg8rwb/Layer_47.png" alt=""/></div>
<div class="square"><p>Orders, C.R.M. and P.O.S</p><img src="https://s7.postimg.cc/9yj7h0hgb/Shape_33.png" alt=""/></div>
</div>

Stacked bar css

I want a stacked bar with inner vertical text. I failed to put it in the right position. This is how I want it:
This is my code and output:
.colWrapper{
height:200px;
width:100px;
position:relative;
border:1px solid #ccc;
}
.barContainer{
position:absolute;
bottom:0;
width:100%;
}
.bar{
widith:100%;
padding:10px;
transform: rotate(90deg);
text-align:center;
margin:0
}
<div class="colWrapper">
<div class="barContainer">
<div class="bar" style="background-color:#b4cde2;">Software</div>
<div style="clear:both;"></div>
<div class="bar" style="background-color:#7ca7cc;">Banking</div>
</div>
</div>
You need to use -90deg and also, use the transform-origin:
.colWrapper {
height: 200px;
width: 100px;
position: relative;
border: 1px solid #ccc;
}
.barContainer {
bottom: 0;
width: 100%;
}
.bar {
padding: 10px;
transform: rotate(-90deg);
text-align: center;
margin: 0;
left: -25px;
transform-origin: right bottom;
position: absolute;
width: 100px;
bottom: 120px;
}
.bar:first-child {
left: -80px;
}
<div class="colWrapper">
<div class="barContainer">
<div class="bar" style="background-color:#b4cde2;">Software</div>
<div class="bar" style="background-color:#7ca7cc;">Banking</div>
</div>
</div>
Second Method:
.colWrapper {
height: 200px;
width: 100px;
position: relative;
border: 1px solid #ccc;
}
.barContainer {
bottom: -5px;
width: 100%;
transform: rotate(-90deg);
position: absolute;
left: 5px;
}
.bar {
padding: 10px;
text-align: center;
margin: 0 0 15px;
sposition: absolute;
}
.bar:first-child {
left: -80px;
}
<div class="colWrapper">
<div class="barContainer">
<div class="bar" style="background-color:#b4cde2;">Software</div>
<div class="bar" style="background-color:#7ca7cc;">Banking</div>
</div>
</div>
You can also take a look at writing-mode and flex-box.
https://developer.mozilla.org/fr/docs/Web/CSS/writing-mode
div {
display: inline-flex;
flex-flow: column wrap;
height: 320px;
width: 270px;
vertical-align: middle;
}
span {
padding: 20px 5px;
background: #1E84C6;
-webkit-writing-mode: vertical-lr;
/* old Win safari */
writing-mode: vertical-lr;
writing-mode: tb-lr;
text-align: right;
margin: 10px;
height: 100px;
width: 60px;
font-size: 30px;
line-height: 60px;
}
span:nth-child(3) {
height: 260px;
}
a {
display: inline-block;
text-decoration: none;
letter-spacing: 2px;
text-transform: uppercase;
font-family: helvetica;
color: white;
}
div + div a {
transform: scale(-1, -1);
/* reverse writing direction optionnal*/
btext-align: left;
transform-origin: 1em 1.25em;
}
span:nth-child(2) {
background: #283F4F;
}
span:nth-child(4) {
background: #1DC685;
}
​-
<div>
<span> <a href>one</a></span>
<span> <a href>two</a></span>
<span> <a href>three</a></span>
<span> <a href>four</a></span>
<span> <a href>five</a></span>
</div>
or
<div>
<span> <a href>one</a></span>
<span> <a href>two</a></span>
<span> <a href>three</a></span>
<span> <a href>four</a></span>
<span> <a href>five</a></span>
</div>
and so on ...

An Inline Relative Div with Absolute Divs In It

The following is my markup:
.play-controls {
.fa-play, .fa-pause {
font-size: 25px;
}
}
.volume-controls {
display: inline-block;
position: relative;
.overlay {
background-color: $highlight;
height: 10px;
border-radius: 5px;
width: 0px;
position: absolute;
z-index: 15;
}
.background {
background-color: $text-color;
width: 100px;
height: 10px;
border-radius: 5px;
position: absolute;
z-index: 10;
}
.circle {
border-radius: 100%;
position:absolute;
background-color: #fff;
border: 1px solid;
height: 15px;
width: 15px;
z-index: 20;
top: -3px;
}
}
.player {
#album-artwork {
width: 80px;
height: 80px;
vertical-align: middle;
display:inline-block;
margin-right: 10px;
}
.wrapper {
display:inline-block;
.information {
margin-bottom: 5px;
#song-title {
font-size: 22px;
font-weight:bold;
margin-right: 5px;
}
#artist-album {
font-size: 18px;
}
}
.progress-bar {
position: relative;
.overlay {
background-color: $highlight;
height: 10px;
border-radius: 5px;
width: 0px;
position: absolute;
z-index: 15;
}
.background {
background-color: $text-color;
width: 600px;
height: 10px;
border-radius: 5px;
position: absolute;
z-index: 10;
}
.circle {
border-radius: 100%;
position:absolute;
background-color: #fff;
border: 1px solid;
height: 15px;
width: 15px;
z-index: 20;
top: -3px;
}
}
}
}
<div class="play-controls">
<i class="fa fa-play" id="playpause"></i>
</div>
<div class="volume-controls">
<div class="background"></div>
<div class="circle"></div>
<div class="overlay"></div>
</div>
<div class="player">
<img id="album-artwork" src="build/images/guero.jpg">
<div class="wrapper">
<div class="information">
<span id="song-title">Go It Alone</span>
<span id="artist-album">Beck - Guero</span>
</div>
<div class="progress-bar">
<div class="background"></div>
<div class="circle"></div>
<div class="overlay"></div>
</div>
</div>
</div>
The divs with classes background, circle, and overlay in volume-controls are all position: absolute; with volume-controls as position: relative;.
Upon making play-controls, volume-controls, and player inline, play-controls is inline with volume-controls, but volume-controls is overlapping the player.
How would I be able to set everything in one line, without any overlapping?
EDIT: JSFiddle
You could float:left; the 3 main parts or display:inline-block; them the issue the player is over the volume-controls is because of the absolute positioned elements in the volume-controls. You could add a width to volume-controls.
.volume-controls {
display: inline-block;
position: relative;
width:150px;
}
Here is the fiddle