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.
Related
I am having an issue with positioning in CSS. Now I have an icon and text like this :
Icon
But I want to make the icon go to the middle of the text. How do I do that?
You can use Flex.
Live code: Codepen
HTML:
<div class='alert'>
<span class='alert-icon'>icon</span>
<div class='alert-content'>
<div>text</div>
<div>text</div>
<div>text</div>
</div>
<span class='alert-close-icon'> close icon </span>
</div>
CSS:
.alert {
background-color: red;
color:#fff;
display: flex;
align-items: center;
justify-content: space-between;
width: 250px;
border-radius: 6px;
padding: 10px 10px
}
Just add the images and replace them with the text inside .alert-icon .alert-close-icon
Just use display: flex and align-items: center on the parent container of two block of elements to aligns.
.inlineElements {
display: flex;
}
.alignItemsCenter {
display: flex;
align-items: center; /* Pack items around the center */
}
.content {
margin: 0 10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet"/>
<div class="inlineElements">
<div class='alignItemsCenter'>
<i class="fa-regular fa-circle-xmark"></i>
<div class="content">
<div>
Hello
</div>
<div>
Display
</div>
<div>
Flex
</div>
</div>
</div>
<i class="fa-solid fa-xmark"></i>
<div>
I'm having difficulty vertically centering some SVGs from FontAwesome in a navigation component. I've recreated the problem in a much simpler example here. What I'm trying to achieve is to have the two icons and the text all vertically aligned regardless of their individual height. As you can see, the Apple logo is not quite at the same position as the other elements. I feel like maybe my problem lies in having everything nested in too many elements, but the SVGs also need to link to other pages, and I like to be able to have different spacing between the icons compared to between the icons and the title. Any advice is greatly appreciated!
.tool-bar {
background-color: gray;
display: flex;
height: 3em;
align-items: center;
}
.icons {
display: flex;
align-items: center;
margin-left: 0.5em;
}
.icons a {
margin-right: 0.2em;
display: flex;
align-items: center;
}
.icons a img {
height: auto;
}
<link href="https://pro.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet"/>
<div class="tool-bar">
<a class="title">Title</a>
<div class="icons">
<a href="https://www.google.com/" target="_blank">
<i class="fab fa-google"></i>
</a>
<a href="https://www.apple.com/" target="_blank">
<i class="fab fa-apple"></i>
</a>
</div>
</div>
Wrap the <i> element containing the fontawesome icons in a <div>.
I have done that here in this snippet and added a style background red to show the div rendering. The issue is that in fontawesome icons, the Google G logo and the Apple logo don't appear proportionate although their containing divs are identical in sizes. That's partly why these don't look visually aligned. You can see this if you run the snippet.
.tool-bar {
background-color: gray;
display: flex;
height: 3em;
align-items: center;
}
.icons {
display: flex;
align-items: center;
margin-left: 0.5em;
}
.icons a {
margin-right: 0.2em;
display: flex;
align-items: center;
}
.icons a img {
height: auto;
}
<link href="https://pro.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet"/>
<div class="tool-bar">
<a class="title">Title</a>
<div class="icons">
<a href="https://www.google.com/" target="_blank">
<div style="background-color: red;">
<i class="fab fa-google"></i>
</div>
</a>
<a href="https://www.apple.com/" target="_blank">
<div style="background-color: red;">
<i class="fab fa-apple"></i>
</div>
</a>
</div>
</div>
I'll try to align 3 Logos with the same height but with different widths on a row over the hole screen-width: The first image should be aligned at the left end of the screen, the third one should be aligned at the right end of the screen and the second one should float in between the other two images with the same space between them. The space should get smaller when the display gets smaller until it hit a defined minimum space. From there on if the display gets further smaller the hole row should scale down. I hope the image helps in clarify what I have in mind.
how it should look like
It's for a MailChimp Newsletter.
That is how far I got:
.my-logo-container{
width: 100%;
height: 80px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: flex-start;
}
.my-logos{
flex: 1;
border: 1px solid red;
height: auto;
max-height: 100px;
}
<div class="my-logo-container">
<div class="my-logos">
<a href="https://via.placeholder.com/100x80.png">
<img src="https://via.placeholder.com/100x80.png">
</a>
</div>
<div class="my-logos">
<a href="https://via.placeholder.com/195x80.png">
<img src="https://via.placeholder.com/195x80.png">
</a>
</div>
<div class="my-logos">
<a href="https://via.placeholder.com/175x80.png">
<img src="https://via.placeholder.com/175x80.png">
</a>
</div>
</div>
Any Help is highly appreciated.
I have an alternative for you. Did you mean this?
.my-logo-container{
width: 100%;
margin:0;
}
ul.my-logos{
margin: 0;
padding: 0;
list-style: none;
}
ul.my-logos li{
width: 32.33333%;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
margin:0;
float: left;
text-align: center;
border:1px solid red;
}
ul.my-logos li a img{
max-width: 130px;
height:80px;
}
<div class="my-logo-container">
<ul class="my-logos">
<li>
<a href="https://via.placeholder.com/100x80.png">
<img src="https://via.placeholder.com/100x80.png">
</a>
</li>
<li>
<a href="https://via.placeholder.com/195x80.png">
<img src="https://via.placeholder.com/195x80.png">
</a>
</li>
<li>
<a href="https://via.placeholder.com/175x80.png">
<img src="https://via.placeholder.com/175x80.png">
</a>
</li>
</ul>
</div>
I came up with this solution: Instead of defining a minimum space in css I added more space into the images themselves. Now they can touch eachother and it still looks good. With display: flex; and justify-content: space-between; I arranged them in a row. With max-width: 100%; and height: auto; the Images auto-resizes on smaller screens. It works for me now.
.logo-container {
display: flex;
justify-content: space-between;
}
.logo {
max-width: 100%;
height: auto;
}
<div class="logo-container">
<img class="logo" src="https://via.placeholder.com/100x80.png">
<img class="logo" src="https://via.placeholder.com/195x80.png">
<img class="logo" src="https://via.placeholder.com/175x80.png">
</div>
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;
}
I am trying to center my logo it is in a div with the id "logocont" here is my css code no matter how i tried is stay stuck at the left if i put display block my navigation bar goes in another line and logo will be centered.
#logocont {
display: inline-block;
float: none;
align-content: center;
text-align: center;
margin : 0 auto;
overflow : visible;
}
Note: I am using grid layout I created the grids in another css file and i am linking it.
Here is my HTML code:
<div class="gr-16" id="menu">
<div id="logocont" class="gr-2">
<a href="#">
<img id="logo" src="images/mylogo21.png" alt="Logo" runat="server" />
</a>
</div>
<div id="navbar" class="gr-5">
<nav>
<a href="#">
Home
</a>
<a href="#">
Programmers
</a>
<a href="#">
Request Program
</a>
</nav>
</div>
</div>
Use display inline-flex for your logo container
#logocont {
display: inline-flex;
align-items: center;
}
This example centers a div in a div:
<html>
<style>
#outer {
width: 100%;
text-align: center;
}
#inner {
display: inline-block;
}
</style>
<div id="outer">
<div id="inner">Foo foo</div>
</div>
I used the following CSS to make a vertically and horizontally aligned div accordingly to what i understood from your HTML. May you check if this helps? (link to JSFiddle: https://jsfiddle.net/est02o5m/1/)
CSS:
.gr-16 {
display: grid;
}
.gr-2 {
grid-column: 2;
background-color: red;
border: 1px solid black;
grid-column-start: 1;
grid-column-end: 2;
}
.gr-5 {
grid-column: 5;
grid-column-start: 3;
grid-column-end: 8;
}
#logocont {
width:100%;
height: 200px;
display: inline-flex;
float: none;
align-content: center;
text-align: center;
margin : 0 auto;
overflow : visible;
border: 2px solid black;
background: #ccc;
align-items: center;
justify-content: center;
}
If the css that i used doesn't correspond to what you're using may you complement it and update your question?
I fixed this problem by removing the nav tag from the div as well as the logo so my code became:
<div class="gr-16" id="menu">
<nav id="navbar">
Home
Programers
Request Program
</nav>
<a href="#" id="logocont">
<img id="logo" src="images/mylogo21.png" alt="Logo" runat="server" />
</a>
</div>
and changed the css to:
#logocont {
width : 100px;
display : block;
margin : 0 auto;
}