Centering pictures and text with Flexbox - html

I'm having trouble centering pictures alongside text inside a container. Everything I try does something to the text but the images always remain with a gap in the bottom.
I tried every single flexbox property and none of them do anything with the icons. My hint is that the problem lies on the way I architectured the flexbox, since it's only my second time using it. Another option could be something inherited from html or body styling.
<div class="phone">
<div class="nightmare">
<img src="../C&C/img/email.png" width="18" height="18">
</div>
<div>
contador#gmail.com
</div>
<div class="nightmare">
<img src="../C&C/img/Call-Icon.png" width="18" height="18">
</div>
<div>
(303) 499-7111
</div>
</div>
.phone {
flex-wrap: wrap;
display: flex;
color: #ffffff;
background-color: #004c00;
justify-content: flex-end;
flex-direction: row;
align-items: center;
font-size: 1;
}
.nightmare {
align-items: center;
}
The green banner is my banner, the blue banner is the way I want it to look (not the colour, just the tidiness of the display).

Add the following CSS to your img tags and remove the height and width attributes from them.
img {
height: 1rem; /* Set it to your liking */
margin-right: 10px;
vertical-align: middle
}
This isn't a flexbox solution but will help you for sure.
Here's the codepen
https://codepen.io/faisalrashid/pen/BaBLKaB

You should use justify-content: center; to center align the items horizontally and
align-items: center; to center align the items vertically. Secondly, you can wrap the icon and text in one div, instead of two separate divs and then you can use display: flex and align-items to handle the alignment of items.
See the below example
.phone {
flex-wrap: wrap;
display: flex;
color: #ffffff;
background-color: #004c00;
justify-content: flex-end;
flex-direction: row;
font-size: 1;
justify-content: center; /** this will align items horizontaly **/
align-items: center; /** this will align items vertically **/
min-height: 50vh; /** Remove this. Added for demonstration only **/
}
.phone > div{
align-items: center;
display: flex;
}
.phone > div a{
display: flex;
align-items: center;
color: #fff;
font-family: Arial;
}
<div class="phone">
<div class="c-item">
<img src="https://img.icons8.com/pastel-glyph/2x/worldwide-location--v1.png" width="18" height="18" />
contador#gmail.com
</div>
<div class="c-item">
<img src="https://img.icons8.com/pastel-glyph/2x/worldwide-location--v1.png" width="18" height="18" />
(303) 499-7111
</div>
</div>
I have added necessary comments to the code snippet. This will help you.

Try the below snippet:
.phone{
flex-wrap: wrap;
background-color: #004c00;
flex-direction: row;
font-size: 1;
display:flex;
padding-left:20px;
padding-right:20px;
padding-top:5px;
padding-bottom:5px;
}
a{
color: #ffffff;
text-decoration: none
padding-left:10px;
padding-right:10px;
}
.nightmare2{
margin-left: auto;
}
<div class="phone">
<div class="nightmare">
<img src="https://cdn2.iconfinder.com/data/icons/sem_labs_icon_pack/icons/mail2.png" width="18" height="18">
</div>
<div>
contador#gmail.com
</div>
<div class="nightmare2">
<img src="https://www.iconsdb.com/icons/download/white/phone-69-48.png" width="18" height="18">
</div>
<div>
(303) 499-7111
</div>
</div>

Hey the problem with your code is that you are trying to apply align-items to a class which doesn't have a display: flex property.
Also use this:
.nightmare a{
display:flex;
align-items: center;
}
Also using the chrom developer tool that, the child in the flex div doesn't have extra margins, which can be a cause of them not aligning.
Also I would write my markup this way.
<div class="phone">
<div class="nightmare">
<a href="mailto:cplusc#gmail.com"><img src="../C&C/img/email.png" width="18" height="18">
<p>contador#gmail.com</p>
</a>
</div>
<div class="nightmare">
<a href="tel:+1-303-499-7111"><img src="../C&C/img/Call-Icon.png" width="18" height="18">
<p>(303) 499-7111</p></a>
</div>
</div>
and the CSS goes like this :
.phone{
display: flex;
}
.phone .nightmare a {
display: flex;
align-items : center;
}
.phone .nightmare a p{
margin-bottom : 0px;
}

Related

Flexbox - Don't move centered columns if width changes

I have a frustrating problem with the otherwise great flexbox capabilities. I have the following layout with a centered column
The problem is, if one of the texts in the row grows or shrinks, then the FB/IG icons will not be in the same column line anymore. See what happens:
How can I achieve for variable width texts to not make the element grow to the left, but make the FB and IG icons actually stay in the same line no matter the variable texts??
Here is the code
.test-links {
display: flex;
flex-direction: column;
}
.test-link {
display: flex;
justify-content: center;
align-items: center;
}
<div class="test-wrapper">
<img class="test-img" src="path" />
<div class="test-links">
<a class="test-link">
<i class="fb"></i>
<span>Test</span>
</a>
<a class="test-link">
<i class="ig"></i>
<span>LongerText</span>
</a>
</div>
</div>
Thank you for your help!
.test-links {
display: flex;
flex-direction: column;
}
.test-link {
display: flex;
justify-content: left;
align-items: center;
width:100px;
margin:0 auto;
}
.test-wrapper
{
text-align:center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="test-wrapper">
<img class="test-img" src="https://dummyimage.com/100x100/000/fff" />
<div class="test-links">
<a class="test-link">
<i class="fb fa fa-facebook"></i>
<span>Test</span>
</a>
<a class="test-link">
<i class="ig fa fa-instagram"></i>
<span>LongerText</span>
</a>
</div>
</div>
I used <img src="http://via.placeholder.com/20x20" /> instead of icons, so to render something. position: absolute for text does the job.
Following MDN's definition:
[position] absolute: the element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left.
... so left:100% moves it to the left for whole width of parent's element, which is only an icon. You can create a distance between icon/image and text using margin-left for text. position: relative for link makes it a hook for position of absolute child.
You can adjust precise values.
Snippet
.test-wrapper {
display: flex;
flex-direction: column;
align-items: center;
}
.test-links {
display: flex;
flex-direction: column;
align-items: center;
margin: 10px 0 0 0;
}
.test-link {
position: relative;
}
.test-link span {
position: absolute;
top: 0;
left: 100%;
}
<div class="test-wrapper">
<img class="test-img" src="http://via.placeholder.com/50x50" />
<div class="test-links">
<a class="test-link">
<img src="http://via.placeholder.com/20x20" />
<span>Test</span>
</a>
<a class="test-link">
<img src="http://via.placeholder.com/20x20" />
<span>LongerText</span>
</a>
</div>
</div>
Since, span cannot take width, use p instead and then add the following class to you code:
.test-link p{
width: 50px;
height:auto;
word-break:break-all;
text-align:left;
}
And then you will achieve the desired format.
Here is a link to the fiddle supporting this answer.
Here is the snippet:
.test-links {
display: flex;
flex-direction: column;
}
.test-link {
display: flex;
justify-content: center;
align-items: center;
}
.test-link p{
width:50px;
height:auto;
word-break:break-all;
text-align:left;
}
<div class="test-wrapper">
<div class="test-links">
<a class="test-link">
<img src="http://via.placeholder.com/20x20">
<p>Test</p>
</a>
<a class="test-link">
<img src="http://via.placeholder.com/20x20">
<p>LongerText</p>
</a>
</div>
</div>
Hope this was helpful.

How can I center content without content stretching?

I want to center the 2 images but the images are being stretched out of proportion because of display: flex but justify-content wont work without it
is there another solution to centering the images?
<div class="column-photo">
<div class="center">
<img src="img/2.%20GenerationAnxiety%20page%202,2.png"
style="width:40vw;">
<img src="img/3.%20GenerationAnxiety%20page%203,3.png"
style="width:40vw;">
</div>
column-photo {
flex-wrap: wrap;
flex: 33.33%;
}
.center {
display: flex;
justify-content: center!important;
align-content: center!important;
}
You have missed dot with class column-photo.
Alternate way without specific width
.column-photo {
text-align:center;
display:block;
}
.center {
display:inline-block;
float:none;
}
If you want stretch the images but keep the aspact ratio you should wrap the images in a div with a individual width. Then you can give the image a width of 100% to keep the aspact.
If you should use vw in this case is another question
<div class="column-photo">
<div class="center">
<div class="img-container" style="width: 40vw">
<img src="http://via.placeholder.com/350x150">
</div>
<div class="img-container" style="width: 40vw">
<img src="http://via.placeholder.com/350x150">
</div>
</div>
</div>
img{
width: 100%;
}
Looks like you've messed up some properties:
.center {
display: flex;
justify-content: center;
align-items: center;
}
<div class="center">
<img src="https://pbs.twimg.com/profile_images/843090368837042176/Nl-rCb9c_400x400.jpg" style="width:10vw;">
<img src="https://pbs.twimg.com/profile_images/843090368837042176/Nl-rCb9c_400x400.jpg" style="width:20vw;">
</div>

Adjusting an Object on Center , works only when I determine width

Currently I'm working on sort of Images , I want them to be in Center of page So I'm using margin-left and margin-right set to auto to center it.
and here's the problem :
it does not work when i let browser find the width , if i set Width to auto ,
it'll be in left side of page
this problem can result responsibility problem , and makes page not compatible with Retina
Here's My HTML code:
<div class="lang">
<img src="english.png" title="Visit Our English Community" /><a><img id="shw" onClick="document.getElementById('box').style.display= 'block'" src="persian.png"/>
</div>
and CSS Class :
.lang {
margin-left:auto;
margin-right:auto;
width:96px;
height:32px;
margin-bottom:3px;
}
You can do it with Css flex as follows:
.lang {
flex-direction: column;
justify-content: center;
align-items: center;
display: flex;
}
<div class="lang">
<img src="english.png" title="Visit Our English Community" />
<img id="shw" onClick="document.getElementById('box').style.display= 'block'" src="persian.png"/>
</div>
For horizontal center alignment:
.lang {
justify-content: center;
align-items: center;
display: flex;
}
<div class="lang">
<img src="english.png" title="Visit Our English Community" />
<img id="shw" onClick="document.getElementById('box').style.display= 'block'" src="persian.png"/>
</div>
Try this or Check fiddle
https://jsfiddle.net/g42rtk8s/7/
.lang {
display: block;
height: 32px;
margin-bottom: 3px;
text-align: center;
}

HTML/CSS align multiple elements within div

I wonder how to align three elements in one div. Basically, it should look like this
Solved.
For future readers, the solution for my problem looks like this
<div style="text-align: center;">
<span style="float: left;">LEFT</span>
<span style="margin: auto;">CENTER</span>
<span style="float: right;">RIGHT</span>
</div>
.container{
display: flex; // working with all latest browsers
display: -webkit-flex; // for old version of safari
display: -ms-flex; // for old versions of IE
justify-content: space-between;
}
.container span{
flex: 1;
text-align: center;
}
<div class="container">
<span>LEFT</span>
<span>CENTER</span>
<span>RIGHT</span>
</div>
The best thing is if you are adding more data inside container. it will give equal spacing. And inline styling is not a better option
Demo here
Try using float:left and float:right for left and right see this fiddle:
https://jsfiddle.net/u924gptz/
Please do follow certain things like not have any styles inline
.wrapper {
text-align: center;
}
.left {
float:left;
}
.center {
margin: auto;
}
.right {
float: right;
}
<div class="wrapper">
<span class="left">LEFT</span>
<span class="center">CENTER</span>
<span class="right">RIGHT</span>
</div>
Use DIVs instead of spans and this simple CSS for the container element:
.x {
display: flex;
justify-content: space-between;
}
display: flex does the equal distirbution, justify-content: space-between; makes the outmost elements align at the borders.
.x {
display: flex;
justify-content: space-between;
}
<div class="x">
<div>LEFT</div>
<div>CENTER</div>
<div>RIGHT</div>
</div>
All 3 spans should have separate stylings.
Here:
<div style="text-align: center;">
<span style="float: left;">LEFT</span>
<span style="margin: auto;">CENTER</span>
<span style="float: right;">RIGHT</span>
</div>
This should do it.
well there are a number of ways to do this. but you could use something like this
div { display:flex; align-items: center;}
span:first-child {float:left;}
span:nth-child(2) {margin:0 auto}
span:last-child {float:right}
<div>
<span>LEFT</span>
<span>CENTER</span>
<span>RIGHT</span>
</div>

Vertically justify content

Hopefully this isn't an unsolved task, but I'm trying to vertically justify an unknown (ish) number of divs inside of a container.
Each div should be equal distances from each other, and, additionally, the same distance from the edges. (Assuming the last part can be accomplished using ghost elements before and after)
The divs will each fill the width of the container, and the container is a set height, but the number of elements inside the container is unknown.
I'm assuming it can be done using Flexbox to some degree, but have been unsuccessful in my attempts thus far.
Yep, flexbox is the simplest way to do it.
On the container element:
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
}
On the child elements:
.container div {
flex: 1;
width: 100%
}
For the spacing between the elements, just add padding to the container and bottom margins to the children.
The style would look like this:
.container {
/* Same as above, and */
padding: 20px;
}
.container div {
/* Same as above, and */
margin-bottom: 20px;
}
.container div:last-of-type{
margin-bottom: 0;
/* So that spacing is even at bottom and top of container */
}
(I was typing this when you posted your answer, so I put it up anyway)
Fiddle
I use justify-content:space-evenly.
HTML:
div.container {
display: flex;
}
div.one_item_container {
display: flex;
flex-direction: column;
justify-content: space-evenly;
}
<div class="container">
<div class="one_item_container">
<img height="30" src="hello.jpeg" style="background-color:lightblue;" />
</div>
<div class="one_item_container">
<img height="50" src="hello2.jpeg" style="background-color:lightblue;" />
</div>
<div class="one_item_container">
<img height="40" src="hello2.jpeg" style="background-color:lightblue;" />
</div>
</div>
As usual, no matter how long I search, I find the answer only immediately after I ask the question. :D
For those curious, or for my own future reference: Flexbox's justify DOES work, you just need a few more options:
HTML:
<div id="outer-container">
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
</div>
CSS:
#outer-container {
height: 250px;
width: 200px;
background: red;
display: flex;
justify-content: space-around;
flex-direction: column;
}
.inner-element {
width: 200px;
height: 10px;
background: blue;
}
https://css-tricks.com/almanac/properties/j/justify-content/
https://jsfiddle.net/WW3bh/