middle aligning icon-fonts inside css circles - css

I am trying to middle align icons inside a circle. I am using icon fonts by font-awesome. My code is as follows
<ul>
<li><i class="icon-5x icon-camera"></i></li>
<li><i class="icon-5x icon-camera"></i></li>
<li><i class="icon-5x icon-camera"></i></li>
</ul>
CSS
ul {
list-style: none;
}
ul li {
display: inline-block;
margin: 15px;
height: 100px;
width: 100px;
border-radius: 50%;
}
ul li a {
font-size: 1em;
color: #000;
display: table-cell;
vertical-align: middle;
text-align: center;
text-decoration: none;
}
and also I tried
a {
line-height: 100%;
text-align: center;
}
But these approaches does not work.

Your solution is valid, you just need to move the width and height declarations into the a:
ul {
list-style: none;
li {
display: inline-block;
background-color: pink;
margin: 15px;
border-radius: 50%;
a {
color: #000;
display: table-cell;
vertical-align: middle;
text-align: center;
height: 100px;
width: 100px;
&, &:hover, &:active {
text-decoration: none;
}
}
}
}
Result:

You can do this with flexbox quite easily. That is my go to and then fallback to the above solution for browsers that don't support flexbox. Flexbox support is awesome these days especially with IE 8 9 & 10 going away.
The trick is to use justify-content: center to align the icon center in the circle and use align-items: center to vertically align the icon in the circle.
Check out this great resource on flexbox. See here for an example pen http://codepen.io/celsowhite/pen/pgVegE.
The HTML:
<ul class="social_links">
<li><a href="" target="_blank">
<i class="fa fa-envelope"></i>
</a></li>
<li><a href="" target="_blank">
<i class="fa fa-twitter"></i>
</a></li>
<li><a href="" target="_blank">
<i class="fa fa-facebook"></i>
</a></li>
</ul>
The SCSS:
ul.social_links {
display: block;
padding: 20px 0px 0px;
li {
display: inline-block;
font-size: 23px;
padding: 0px 10px;
}
}
ul.social_links i {
background: black;
border-radius: 50%;
width: 50px;
height: 50px;
color: #fff;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
transition: all .5s ease-in-out;
&:hover{
background: #555555;
}
}

Use line-height property, that's best, I had same problem I used line-height and it's done.
Example
height:20px;
width:20px;
line-height:20px;
good to go

Example of list :
<ul class="list-unstyled list-coordonne">
<li><i class="fa fa-coordonne" aria-hidden="true"></i><p> 293, Boulevard Abdelmoumen 20360 - Casablanca Maroc</p></li>
<li><i class="fa fa-coordonne" aria-hidden="true"></i><p> 293, Boulevard Abdelmoumen 20360 - Casablanca Maroc</p></li>
<li><i class="fa fa-coordonne" aria-hidden="true"></i><p> 293, Boulevard Abdelmoumen 20360 - Casablanca Maroc</p></li>
</ul>
CSS code to center icon in circle :
.footer-text .fa-coordonne {
color: white;
background-color: #dad918;
border-radius: 50%;
font-size: 25px;
width: 40px;
height: 40px;
text-align: center;
}
.footer-text .list-coordonne>li:first-child .fa-coordonne:before{
content: '\f041';
text-align: center;
font-weight: 600;
vertical-align: sub;
z-index: 12;
}
.footer-text .list-coordonne>li:nth-child(2) .fa-coordonne:before{
content: '\f003';
text-align: center;
font-weight: 600;
vertical-align: sub;
z-index: 12;
}
.footer-text .list-coordonne>li:last-child .fa-coordonne:before{
content: '\f095';
text-align: center;
font-weight: 600;
vertical-align: -webkit-baseline-middle;
z-index: 12;
}

What I simply like to do is first set the height and width of the icon. Plop it in a div. Apply border radius of 50%(to get a circle) to the div. And give it a background-color(obviously). Set the display property of the div to "flex". justify-content: center and align-items: center. And there you go! Works out for me!

add this padding in 'li'
li{
padding:10px; //or anyvalue
}
or use specific padding
li{
padding-top:10px; //or any value
}
remember when you add padding value then the size would also increases, adjust and balance them.

Related

Content won't move to assigned grid areas

I have a 2-column grid, and I'm trying to put my in the left column, and the (#footer-info) text in the right column. I have assigned grid-areas, but they won't move to their respective columns. Both the h1 and article are stuck on top of each other in the left column.
Text is all stacked on top in the left column, I want to move the below text to the right (indicated in red)
footer {
background-color: $navy;
color: $white;
text-align: center;
>div {
position: relative;
#include md {
display: grid;
grid-template-columns: 50% auto;
grid-template-areas: "glazetitle location";
}
section {
position: relative;
z-index: 20;
h1 {
font-family: 'Caveat', cursive;
font-size: rem-calc(35);
font-weight: 700;
color: $white;
z-index: 1;
#include md {
grid-area: glazetitle;
}
}
#footer-info {
#include md {
grid-area: location;
}
p {
font-family: 'Padauk', sans-serif;
font-size: rem-calc(16);
line-height: rem-calc(22);
color: $white;
}
ul {
display: flex;
justify-content: center;
padding: 15px 0px;
li {
margin: 0px 10px;
a:link,
a:visited {
color: $white;
&:hover,
&:active {
color: $gray;
}
}
}
}
}
}
#footer-scribble {
background-image: url('../img/Footer/footer-scribbles.svg');
height: 300px;
background-repeat: no-repeat;
position: absolute;
top: 0;
left: 0;
width: 120%;
z-index: 10;
margin-left: -200px;
#include md {
width: 100%;
height: 250px;
margin-left: -150px;
}
}
}
}
<footer class="grid-container content-spacing subhead-spacing">
<div>
<section>
<h1>Glaze Studio</h1>
<article id="footer-info">
<p>1488 Lunetta Street<br> Philadelphia, PA 19106</p>
<ul>
<li><i class="fab fa-twitter"></i></li>
<li><i class="fab fa-facebook-square"></i>
</li>
<li><i class="fab fa-instagram"></i>
</li>
<li><i class="fab fa-youtube"></i></li>
</ul>
<p>215-925-3453<br> info#glazestudio.org
<br> Icons: icons8.com</p>
</article>
</section>
<aside id="footer-scribble"></aside>
</div>
</footer>
Please let me know if I need to supply more info, I'm still new to this platform. **Also the address/phone number are phony! No worries.
A couple of things I have noticed.
You defined a grid-template-areas: "glazetitle location" in a medium breakpoint, but then never assigned them to there respected tags. In this case, it would be your section tag and your aside tag at the medium breakpoint size.
Why is the div directly inside of the footer tag set to relative. A follow-up question would be why is the aside tag set to an absolute position?
I would say fix those 2 issues first and this might then solve your overall grid problems.

What is the correct way to center a menu?

I am adding a responsive navigation bar to a website, closely based on a w3 school tutorial (https://www.w3schools.com/howto/howto_js_topnav_responsive.asp).
All is working fine, except that I am trying to center the menu links on the page (for the desktop version), instead of having it on the left side like the tutorial explains.
I tried modifying the value of each property, including float, but without success so far. It must be extremely simple, but what am I missing?
html:
<div class="nav" id="nav">
Home
About
Menu
Drinks
Values
Gallery
Booking
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
css:
.nav {
background-color: gold;
overflow: hidden;
}
.nav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.nav a:hover {
background-color: pink;
color: green;
}
.nav .icon {
display: none;
}
I saw that this question has already been asked with bootstrap, but as a beginner, I am only using plain css here.
you can use flex with center layout, give it a try
.topnav {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
background-color: #333;
}
Another approach if you wanted to avoid using flexbox for whatever reason would be to set the menu items to display: inline-block instead of using floating. Then a simple text-align: center on the nav wrapper will be sufficient.
On a side note, it would make more sense semantically to use the <nav> element as a wrapper and put the menu items in a <ul> list as <li> elements.
.nav {
background-color: gold;
overflow: hidden;
text-align: center;
}
.nav a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.nav a:hover {
background-color: pink;
color: green;
}
.nav .icon {
display: none;
}
<div class="nav" id="nav">
Home
About
Menu
Drinks
Values
Gallery
Booking
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>

How to vertical align this span element?

This should be very simple, and apologies if it's a duplicate. I can't get some text in a span to vertically align beside an icon.
Example:
.box {
width: 100px;
text-align: center;
background-color: #f0f0f0;
margin: 50px auto;
padding: 20px;
font-family: sans-serif;
cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="box">
<a>
<span>Search</span>
<i class="fa fa-search fa-2x" aria-hidden="true"></i>
</a>
</div>
CSS I've tried:
Setting the span to display: inline-block and assigning padding and margin. This also moves the icon up.
Setting the link to position: relative and positioning the span. This causes the icon to move, as the span is now taken out of the flow.
Adjusting the line-height of the span. Again, this affects the icon.
Floating the span. This doesn't work.
Is there something I'm missing? I'm not very familiar with flex, would that be a solution? (Note I have to support very old browsers...)
Suggestions much appreciated!
Add following css:
.box a i,
.box a span {
display: inline-block;
vertical-align: middle;
}
.box {
width: 100px;
text-align: center;
background-color: #f0f0f0;
margin: 50px auto;
padding: 20px;
font-family: sans-serif;
cursor: pointer;
}
.box a i,
.box a span {
display: inline-block;
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="box">
<a>
<span>Search</span>
<i class="fa fa-search fa-2x" aria-hidden="true"></i>
</a>
</div>
Give the <i> an id (or class, I named it #k), then add this ruleset:
#k { vertical-align: middle; }
.box {
width: 100px;
text-align: center;
background-color: #f0f0f0;
margin: 50px auto;
padding: 20px;
font-family: sans-serif;
cursor: pointer;
}
#k {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="box">
<a href='#/'>
<span>Search</span>
<i id='k' class="fa fa-search fa-2x" aria-hidden="true"></i>
</a>
</div>
Use CSS Flexbox. Apply display: flex property to .box a and use align-items: center (this will align your items vertically centered).
Have a look at the snippet below:
.box {
width: 30%;
text-align: center;
background-color: #f0f0f0;
margin: 50px auto;
padding: 20px;
font-family: sans-serif;
cursor: pointer;
}
.box a {
display: flex;
align-items: center;
justify-content: center;
}
.box a span {
padding-right: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="box">
<a>
<span>Search</span>
<i class="fa fa-search fa-2x" aria-hidden="true"></i>
</a>
</div>
Hope this helps!
You can easily adjust the positioning of your text in the span if you first make it a block element, and then apply a float. Once this is done, you can apply line-height as you initially mentioned, but without affecting the icon.
Here is a JSFiddle to show what to do. Incredible easy, and you don't have to touch your original CSS: https://jsfiddle.net/pgkjaa8c/
Solution using Floats
.box span {
display: block;
float: left;
line-height: 40px;
}
And you can change the float from left to right if you want the text on the right. Additionally, you can apply left and right padding to push the text away from the icon if you so desire: https://jsfiddle.net/rz4y4696/
.box span {
padding-left: 15px;
display: block;
float: right;
line-height: 40px;
}
Additionally, I advise against using flex. People are constantly pushing flex as a solution, but it eliminates many legacy browsers from support. This would be one of the more traditional ways of implementing this, with fully cross browser, and legacy browser support.
Solution without using Floats
If you want a solution that does not require floats, and will work for varying widths, then you'd have to remove the <i> tag and add your FontAwesome icon to your CSS. You can see the solution here: https://jsfiddle.net/rwkypte8/2/
You can get the value of the FontAwesome search icon here: http://fontawesome.io/icon/search/
The HTML and CSS is below:
HTML
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="box">
<a>
<span>Search</span>
</a>
</div>
CSS
.box {
width: 50%;
text-align: center;
background-color: #f0f0f0;
margin: 50px auto;
padding: 20px;
font-family: sans-serif;
cursor: pointer;
}
.box span {
display: block;
line-height: 40px;
text-align: center;
}
.box span:after {
padding-left: 20px;
font-family: 'FontAwesome';
font-size: 30px;
content: '\f002';
}

Vertically center link text in flexbox layout

I have a menu made up of 5 links. I need each link to be 33% of the available width with spacing between the links. The horizontal and vertical spacing must be the same.
Links must be the same height as the tallest link in there row, even though the length of the link text will vary per link. Ideally all links would be the same height but I doubt this is possible.
The link text is dynamic and will change. This for a responsive website so the page width will vary.
I cant change the HTML at all.
This is for a mobile only website so I dont need to worry about older browsers. I should be fine to use flexbox.
<ul>
<li>
<a href="#">
<span>Link 1</span>
</a>
</li>
<li>
<a href="#">
<span>Link 2 has much longer text than the other links</span>
</a>
</li>
<li>
<a href="#">
<span>Link 3</span>
</a>
</li>
<li>
<a href="#">
<span>Link 4</span>
</a>
</li>
<li>
<a href="#">
<span>Link 5</span>
</a>
</li>
</ul>
My code below works fine in Chrome. The only thing I still need to do is vertically center the link text (see the image below).
The align-items: center property looked promising however If I apply it to the ul then the lis' stop being the same height.
*, *:before, *:after {
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
ul {
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
width: 50%;
border: 2px solid black;
margin: auto;
}
li {
list-style-type: none;
float: left;
width: 32%;
background: grey;
text-align: center;
overflow: hidden;
margin-bottom: 2%;
vertical-align: middle;
}
li:nth-of-type(2),
li:nth-of-type(5)
{
margin-left: 2%;
margin-right: 2%;
}
a {
display: inline-block;
margin: -10em;
padding: 10em;
}
a {
background: gold;
}
a:hover {
background: green;
}
http://codepen.io/anon/pen/CeiqK
I don't know if this solution that I post here will be valid for you. Anyway, I changed your css a bit to get it:
ul {
margin: 0;
padding: 0;
width: 50%;
border: 2px solid black;
margin: 0 auto;
font-size: 0; /* this fix inline-block margins */
}
li {
font-size: 14px;
list-style-type: none;
display: inline-block;
width: 32%;
min-height: 34px;
background: grey;
text-align: center;
overflow: hidden;
margin-bottom: 2%;
}
a {
display: table-cell;
width: 100em;
height: 34px;
vertical-align: middle;
}
Check it at codepen
Creates this:

overflow:hidden hiding borders but not the element that overflows

I'm working on a header with a transition. But something is not working.
I made the ul 120px and li 60px.
And I gave the li:hover a translateY(-60px). So that it pops up when you hover over the li.
I want to hide the content that is overflowing until you hover over it. But it doesn't seem to work. It does hover hide the border of the li that is overflowing.
Does anybody know why?
thank you in advance!
<header>
<ul>
<li>
<a id="p1" href="#">Vibe</a>
<a id="p2" href="#">Vibe</a>
</li>
<li>
<a id="p1" href="#">Creations</a>
<a id="p2" href="#">Creations</a>
</li>
<li>
<a id="p1" href="#">Vision</a>
<a id="p2" href="#">Vision</a>
</li>
<li>
<a id="p1" href="#">Just tell us</a>
<a id="p2" href="#">Just tell us</a>
</li>
</ul>
</header>
And this is the css
header {
height: 60px;
width: 100%;
background-color: #34495e;
}
header ul {
margin-right: 20px;
float: right;
height: 60px;
overflow: hidden;
}
header ul li {
display: inline-block;
height: 120px;
padding-left: 40px;
padding-right: 40px;
overflow: hidden;
-webkit-transition: -webkit-transform 0.2s ease-in-out;
overflow: hidden;
border-left: 1px solid #2c3e50;
}
header ul li a {
text-decoration: none;
line-height: 60px;
font-size: 14px;
font-family: "lato", sans-serif;
font-weight: 700;
color: #e74c3c;
text-transform: uppercase;
-webkit-transform: rotate(10deg);
}
#p2 {
position: absolute;
text-align: center;
color: #c0392b;
line-height: 60px;
}
header ul li:hover {
-webkit-transform: translateY(-60px);
}
Add
position: relative;
To your ul class
Here is the jsfiddle http://jsfiddle.net/2jabu/
Here is one solution that may work for you:
Example Fiddle
First id's should be reserved for single uses. Your #p1 & #p2 would be better written as classes. To solve your problem you only need overflow:hidden; on the highest level container, in this case the header. Also since you've given #p2 absolute position, you also need to give the header position:relative;.
CSS:
header {
// existing styles
position: relative;
overflow: hidden;
}

Resources