Vertically align icon in middle when text falls to two lines - css

I have an icon that I am vertically aligning in middle of a container using flexbox.... it looks like this
but when moving over to a small screen, when the text falls to two lines, the image is not vertically aligned where I want it to be:
I want it to be vertically aligned in the middle to the last line of text only, not vertically aligned in the middle of both lines of text. This is the result I want, with the info icon being inline with "here" and in the middle:
How can I go about achieving this?
.card-heading {
padding: 30px 20px 10px;
display:flex;
font-size:50px;
}
button {
background:none;
border:none;
align-items: center;
}
button img {
height:30px;
}
<h2 class="card-heading"><span>CONTENT IN HERE </span><button class="card-more-imfo" type="button"><img alt="info" src="https://freeiconshop.com/wp-content/uploads/edd/info-circle-outline.png"></button></h2>

You need to change your element structure as below. You need to position your button relative to the latest parent span element which is <span>HERE</span> with help of position: absolute.
.card-heading {
/*padding: 30px 20px 10px; removing extra padding to accommodate both versions in single screen and one not need to scroll*/
display:flex;
font-size:50px;
}
span{
position: relative;
}
button {
position: absolute;
background:none;
border:none;
align-items: center;
right: 0;
top: 50%;
transform: translate(100%, -50%);
}
button img {
height:30px;
}
.demo-2{
max-width: 200px; /*experiment purpose */
}
<h2 class="card-heading">
<span>CONTENT IN
<span>HERE
<button class="card-more-imfo" type="button">
<img alt="info" src="https://freeiconshop.com/wp-content/uploads/edd/info-circle-outline.png">
</button>
</span>
</span>
</h2>
<h2 class="card-heading demo-2">
<span>CONTENT IN
<span>HERE
<button class="card-more-imfo" type="button">
<img alt="info" src="https://freeiconshop.com/wp-content/uploads/edd/info-circle-outline.png">
</button>
</span>
</span>
</h2>

I took the liberty to make some changes on your HTML tags, instead of using h2 as the parent element I decided to use a div instead and place h2 and img tags inside.
Example: https://i.stack.imgur.com/3Jl3r.png
<div class="card-heading">
<h2> CONTENT IN HERE </h2> <img alt="info" src="https://freeiconshop.com/wp-content/uploads/edd/info-circle-outline.png">
</div>
I had to use media queries rules and change your positioning on the icon, changing it to absolute and then added padding-top.
.card-heading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 50px;
height: auto;
width: 100%;
border: 1px solid black;
}
button {
background: none;
border: none;
}
img {
height: 30px;
width: 30px;
position: absolute;
right: 21px;
padding-top: 55px;
}
#media screen and (min-width: 768px) {
.card-heading {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
font-size: 50px;
height: auto;
border: 1px solid black;
}
button {
background: none;
border: none;
}
img {
height: 30px;
width: 30px;
position: relative;
padding: 0;
right: 0;
}
}
```

you can either both the class of the image and of the text float to left to have them aligned as you need... i.e. in .card-heading and whichever is the class of the info image. just write float:left;

Related

Vertical align text to top of a div on Flexbox

I have a flex div with a div and a span, structure that I don't want to modify.
.Flex {
display: flex;
background: lightgray;
height: 300px;
}
.Square {
width: 30px;
height: 30px;
background: black;
}
.Text {
font-family: "Arial";
font-size: 40px;
background: tan;
}
<div class="Flex">
<div class="Square"></div>
<span class="Text">Title</span>
</div>
I'm trying to align the top of the "T" of the text with the top of the Flex div, something like the following image:
But I cant. align-content: flex-start will align all children to the top, but the text within the span will still have some distance to the top of the div.
How is possible to achieve this?
Add one property to your text's css: (you can modify this value according to the positioning of the text you would like, the higher, the more space the text has from the top edge)
line-height: 0.75;
.Flex {
display: flex;
background: lightgray;
height: 300px;
}
.Square {
width: 30px;
height: 30px;
background: black;
}
.Text {
font-family: "Arial";
font-size: 40px;
background: tan;
line-height: 0.75;
}
<div class="Flex">
<div class="Square"></div>
<span class="Text">Title</span>
</div>

CSS Bottom Alignment

I've having a hard time aligning titles of each image at the bottom of the box. Please see the attached image. Here's my code:
Btw, I'm using Joomla.
Box code:
margin: auto;
align-self: center;
height:100px;
width:50%;
margin-top:0.5%;
margin-bottom:0.5%;
border: solid;
Image code:
margin-right:45%;
margin-left:45%;
margin-top:1%;
and title code:
display: block;
text-align: center;
position: absolute;
width: 50%;
This is my HTML code:
<ul class="nav menu navVerticalView nav-pills">
<li class="item-146">1</span></li><li class="item-147">2sdfgfsdgesdfg</span></li><li class="item-148">3cvbgfbnfghnf</span></li><li class="item-149">4</span></li><li class="item-150">5fghfghfg hfghrfghfgh </span></li><li class="item-151">6</span></li><li class="item-152">7bhjm hjmk,hj, hj, hj mjmgjjghjfh jfh hf hdgfhhdfghgfh fg hfgh fh</span></li><li class="item-153">8ghujghjtghj tgjtghjtyg tyjtyjtyj</span></li></ul> </div>
I tried to use bottom: 0px; for the title but all titles would gather above each other. I also tried vertical-align:bottom; but it doesn't work. I tried so many different ways but no luck. If someone can give heads up so I can move on towards, I'll be appreciative.
Thank you,
Image titles are not aligned with the bottom of the box
Set the box to position relative, and the text on absolute. After that set the text to bottom 0.
.box {
margin: auto;
align-self: center;
height:100px;
width:50%;
margin-top:0.5%;
margin-bottom:0.5%;
border: solid;
position: relative;
}
.image {
margin-right:45%;
margin-left:45%;
margin-top:1%;
background: red;
width: 30px;
height: 30px;
}
.box p {
display: block;
text-align: center;
position: absolute;
bottom: 0;
margin: 0;
}
<div class="box"><img class="image"><p>This is not how I would do it but okay 🤷🏼‍♀️</p></div>
You could use display: flex for this:
.example{
display: flex;
background-color: #808080;
flex-flow: column;
justify-content: space-between;
height: 200px; /* for example purpose */
}
.top {
background-color: #cecece;
}
.bottom {
background-color: #cecece;
}
<div class="example">
<div class="top">
<span>This is the title</span>
</div>
<div class="bottom">
<span>This is the title</span>
</div>
</div>

Place box outside of parent

I have an absolutely positioned div containing two divs side by side. When the left div is removed, the right one appears to jump to the left.
Before:
..AABB...
After:
..BB.....
After (desired behaviour):
....BB...
demo:
https://jsfiddle.net/rb3erkqc/10/
To fix this, I want to be able to absolutely position the right div and have the left one follow along.
The easiest way I can imagine is setting the left margin of A to the negative size of A, but I don't think that's possible, because the size of A is unknown and can change.
without knowing more, here is my shot in the dark
$('.container').click(function(){
$('.container div:first-child').remove();
$('.container div:nth-child(1)').remove();
});
.container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.container > * {
position: relative;
display: inline-block;
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div>
<p>Div A</p>
</div>
<div>
<p>Div A</p>
</div>
<div>
<p>Div B</p>
</div>
<div>
<p>Div B</p>
</div>
</div>
Hope this helps
Edit after code snippet was provided:
.abspos {
margin-top: 10%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
width: 100%;
}
.abspos>*:not(input) {
position: relative;
display: inline-block;
border: 1px solid red;
}
.t {
background-color: red;
}
.e {
background-color: yellow;
}
/* Just a hack to be able to toggle asd*/
input {
position: absolute;
top: 0;
margin: 0px auto;
}
input:checked+.t {
display: none;
}
<div class=abspos>
<input type=checkbox>
<div class=t>asd</div>
<div class=e>fasd</div>
</div>
Update 2 with minor changes:
changed display:none to visibility:hidden
this removes the element from the DOM allowing you to manipulate the <div class='.e'></div>
see fiddle below
.abspos {
margin-top: 10%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
width: 100%;
}
.abspos > *:not(input){
position: relative;
display: inline-block;
border: 1px solid red;
}
.t {
background-color: red;
}
.e {
background-color: yellow;
}
/* Just a hack to be able to toggle asd*/
input {
position: absolute;
top: 0;
margin: 0px auto;
}
input:checked + .t {
visibility:hidden; /*remove element from dom to manipulate remaining div*/
}
<div class=abspos>
<input type=checkbox>
<div class=t>asd</div>
<div class=e>fasd</div>
</div>
I found that one can use absolute positioning to make not affect the parent box and then use a css transform to move based on the object's size.
I also added centering for the element outside the box to demonstrate the generality of this approach. Some overflow handling may be necessary in cases where the right side is smaller.
.abspos {
position: absolute;
left: 106px;
top: 57px;
display: flex;
flex-direction: row;
}
.t {
position: absolute;
transform: translate(-100%);
top: 0px;
bottom: 0px;
display: flex;
align-items: center;
}
.t > div {
background-color: red;
}
.e {
background-color: yellow;
}
/* Just a hack to be able to toggle asd*/
input {
position: absolute;
top: -30px;
}
input:checked + .t {
display: none;
}
<body>
<div class=abspos>
<input type=checkbox>
<div class=t><div>asd</div></div>
<div class=e>fasd<br>lova</div>
</div>
</body>

Center text in 100%-screen-width div in small wrapper

I want to center a div and it's text, in a 100%-screen-width div, which is in a smaller wrapper.
.wrapper {
height: 800px;
width: 900px;
margin: auto;
background-color: #000000;
}
.box-wrapper {
width: 1000%;
position: relative;
left: -500%;
background-color: #FF6600;
}
.box {
background-color: #FF0000;
width: 600px;
position: relative;
left: 50%;
color: #00FF00;
}
span {
text-align: center;
display: block;
}
<div class="wrapper">
Random text for wrapper-div
<div class="box-wrapper">
<div class="box">
<span>ABC</span>
<span>DEF</span>
<span>GHI</span>
</div>
</div>
</div>
This code is kind of working but not perfect.
The red div should be moved a bit to the right, also the way
of doing it is not the best in my opinion.
I want a more robust and responsive solution.
To be more clear, it's for the pink division on the bottom
of this website: http://ndvibes.com
There the code is working 99% of the times and reponsive. But on some computers/screens it's 50% off. So I want a less-hacky (without transform etc) and more standard, robust way of getting that effect.
Wrapper 900px > 100%-screen-width coloured div > Centered text in that coloured div.
How can I achieve this the best as possible?
Thanks!
How about this approach, using absolute positioned pseudo elements. The outer-space div with overflow:hidden is to prevent a horizontal scroll bar appearing. I have added padding-top to the .wrapper just so you can see the snippet running in full screen mode.
body {
margin:0;
}
.outer-space {
overflow: hidden;
padding-top:80px;
}
.wrapper {
height: 800px;
width: 100%;
max-width: 900px;
margin: auto;
background-color: #000000;
}
.box {
background-color: #8904B1;
margin:0 auto;
color: #ffffff;
position: relative;
z-index: 2;
padding:10px 0;
}
.box-wrapper {
position: relative;
width:100%;
max-width: 600px;
margin:0 auto;
}
.box-wrapper:before, .box-wrapper:after {
content:"";
position: absolute;
height:100%;
width:100vw;
background-color: #8904B1;
top: 0;
z-index: 1;
}
.box-wrapper:before {
left:-100%;
}
.box-wrapper:after {
right:-100%;
}
span {
text-align: center;
display: block;
}
<div class="outer-space">
<div class="wrapper">
Random text for wrapper-div
<div class="box-wrapper">
<div class="box">
<span>Crazy full width window</span>
<span>absolute positioned pseudo elements</span>
<span>with centered content div and centered text thingy</span>
<span>all inside of a fixed width page wrapper!</span>
<br><span>““”̿ ̿ ̿ ̿ ̿’̿’̵͇̿̿з=(•̪●)=ε/̵͇̿̿/̿ ̿ ̿ ̿ ̿’““</span>
</div>
</div>
</div>
</div>
To center child element, add the following to the parent wrap will center all child.
display: flex;
align-items: center;
justify-content: center;
If you want 100% screen width, use viewport (100vw) for 100% screen width
viewport
The #viewport CSS at-rule contains a set of nested descriptors in a CSS block that is delimited by curly braces. These descriptors control viewport settings, primarily on mobile devices.
1vw = 1% of viewport width
1vh = 1% of viewport height
1vmin = 1vw or 1vh, whichever is smaller
1vmax = 1vw or 1vh, whichever is larger
REF: #viewport
REF: Viewport Sized Typography
body {
margin: 0;
}
.wrapper {
height: 800px;
width: 900px;
margin: auto;
background-color: #000000;
}
.box-wrapper {
width: 900px;
max-width: 900px;
position: relative;
background-color: #FF6600;
display: flex;
align-items: center;
justify-content: center;
}
.outer-wrapper {
width: 100vw;
display: flex;
align-items: center;
justify-content: center;
}
.box {
width: 80%;
background-color: #FF0000;
position: relative;
color: #00FF00;
}
span {
text-align: center;
display: block;
}
<div class="outer-wrapper">
<div class="wrapper">
<p>Random text for wrapper-div</p>
<div class="box-wrapper">
<div class="box">
<span>ABC</span>
<span>DEF</span>
<span>GHI</span>
</div>
</div>
</div>
</div>

Increase a space between img and text inside a href element (word spacing?. padding?)

I am trying to figure out how to make a space between text and image. I've tried everything I learned so far, including word spacing, padding.
Let me show you picture which will give you a better understanding of what I want to achieve.
I want to increase space between the image and the text. Highlighted as yellow.
How can I achieve it using this example code? Link to CodePen
html
<div class="navbar-header">
<div class="container">
<div id="logo">
<a href="#">
Amazing <img src="http://www.clipartkid.com/images/650/image-spiderman-logo-png-spider-man-wiki-wikia-hYo1XM-clipart.png"/> Spiderman
</a>
</div>
</div>
</div>
css
.navbar-header {
border-bottom: 1px solid black;
height: 70px;
margin-bottom: 0;
position: fixed;
width: 100%;
z-index: 100;
}
.container {
align-items: center;
display: flex;
flex-direction: row;
height: 100%;
width: 100%;
}
#logo {
align-items: center;
margin: auto;
}
#logo a {
align-items: center;
color: black;
display: flex;
flex-grow: 2;
margin: auto;
text-decoration: none;
word-spacing: 200px;
}
#logo img {
height: 66px;
margin: auto;
}
I think giving the img a left/right margin should be the best solution. Easiest way to accomplish this:
#logo img {
height: 66px;
margin: auto 20px;
}
If I understand your question..... I would just add left and right margins to the #logo img of about the space you want
#logo img {
height: 66px;
margin: auto;
/*you would want to change this, so as to not have both declarations, I just dont know how much top and bottom margin you want*/
margin-left: 15px;
margin-right: 15px
}

Resources