Maëlle Jumel Maëlle Jumel -4 years ago 70
CSS Question

Center <span> element under <i> element

I'm trying to center several spans under their fontawesome icons. If I set margin it does not work obviously because all the spans (with text) do not all have the same width.

Codepen here



$(document).ready(function(){
$("#navi").click(function(){
$("#navi .fa-chevron-down").toggleClass("rtoate180");
$("#navigation").slideToggle(500);
});
});

#navi {
cursor: pointer;
margin-top: 10px;
margin-left: 20px;
width: 170px;
height: 30px;
line-height: 30px;
padding-left: 10px;
overflow: hidden;
color: #000000;
background: #f67e7d;
font-size: 12px;
text-align: left;
}

#navi i {
position: absolute;
margin-left: 77px;
margin-top: 10px;
color: #000000!important;
font-size: 12px;
}

#navi .fa-chevron-down {
-webkit-transition: all 0.7s ease;
-moz-transition: all 0.7s ease;
-ms-transition: all 0.7s ease;
-o-transition: all 0.7s ease;
transition: all 0.7s ease;
}

.rtoate180 {
transform: rotate(180deg);
}

#navigation {
margin-top: 10px;
margin-left: 20px;
width: 180px;
overflow: hidden;
display: none;
background: #f67e7d;
}

#navigationin span {
color: #ffb997;
font-size: 10px;
position:fixed;
margin:35px 0 0 -35px;
text-transform:lowercase;
text-align:center;
}

#navigation i {
display: inline-block;
color: #000000!important;
margin: 10px;
font-size: 15px;
padding:5px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<div id="navi"> NAVIGATION <i class="fa fa-chevron-down"></i></div>

<!--Navigation-->
<div id="navigation">

<div id="navigationin"><center>
<a href="{text:Link One Url}"><i class="fa fa-user-circle"></i><span>my stuff</span></a>
<a href="{text:Link Two Url}"><i class="fa fa-question-circle"></i><span>tutorials</span></a>
<a href="{text:Link Nine Url}"><i class="fa fa-folder-open-o"></i><span>archive</span></a>
<a href="{text:Link Three Url}"><i class="fa fa-star"></i><span>actors</span></a>
<a href="{text:Link Four Url}"><i class="fa fa-film"></i><span>tv shows</span></a>
<a href="{text:Link Eight Url}"><i class="fa fa-link"></i><span>links</span></a>
<a href="{text:Link Five Url}"><i class="fa fa-file-video-o"></i><span>gifsets</span></a>
<a href="{text:Link Six Url}"><i class="fa fa-file-image-o"></i><span>photosets</span></a>
<a href="{text:Link Seven Url}"><i class="fa fa-square-o"></i><span>icons</span></a>
<a href="{text:Twitter url}" target="_blank"><i class="fa fa-twitter-square"></i><span>twitter</span></a>
<a href="{text:Instagram url} target="_blank""><i class="fa fa-instagram"></i><span>instagram</span></a>
<a href="{text:Youtube url}" target="_blank"><i class="fa fa-youtube"></i><span>youtube</span></a>
</center>
</div>

</div>





For now it's not very nice, the bottom text is too big and is over the edge but I will take care of that later.. ^^ I just want first to center vertically. Thank you!

Answer Source

Like this?

Added:

#navigation a {
    display: inline-block;
    text-decoration: none;
}

Change #1

#navigationin span {
    color: #ffb997;
    font-size: 10px;
    position:fixed;
    margin:35px 0 0 -35px;
    text-transform:lowercase;
    text-align:center;
}

To:

#navigationin span {
    color: #ffb997;
    font-size: 10px;
    display: inline-block;
    text-transform:lowercase;
    text-align:center;
}

Change #2

#navigation i {
    display: inline-block;
    color: #000000!important;
    font-size: 15px;
    margin: 10px 10px 0 10px;
      padding:5px;
}

To:

#navigation i {
    display: block;
    color: #000000!important;
    font-size: 15px;
    margin: 10px 10px 0 10px;
      padding:5px;
}

The position of the #navigationin span was fixed. When scrolling, it was overflowing the icons. I don't know if that is the desired behavior?

$(document).ready(function(){
  $("#navi").click(function(){
$("#navi .fa-chevron-down").toggleClass("rtoate180");
$("#navigation").slideToggle(500);
  });
});
#navi {
    cursor: pointer;
    margin-top: 10px;
    margin-left: 20px;
    width: 170px;
    height: 30px;
    line-height: 30px;
    padding-left: 10px;
    overflow: hidden;
    color: #000000;
    background: #f67e7d;
    font-size: 12px;
    text-align: left;
  }

  #navi i {
    position: absolute;
    margin-left: 77px;
    margin-top: 10px;
    color: #000000!important;
    font-size: 12px;
  }

  #navi .fa-chevron-down {
    -webkit-transition: all 0.7s ease;
    -moz-transition: all 0.7s ease;
    -ms-transition: all 0.7s ease;
    -o-transition: all 0.7s ease;
    transition: all 0.7s ease;
  }

  .rtoate180 {
    transform: rotate(180deg);
  }

  #navigation {
    margin-top: 10px;
    margin-left: 20px;
    width: 180px;
    overflow: hidden;
    display: none;
    background: #f67e7d;
  }
  
  #navigation a {
    display: inline-block;
    text-decoration: none;
  }

  #navigationin span {
    color: #ffb997;
    font-size: 10px;
    display: block;
    text-transform:lowercase;
    text-align:center;
  }
  
    #navigation i {
    display: block;
    color: #000000!important;
    font-size: 15px;
    margin: 10px 10px 0 10px;
      padding:5px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<div id="navi"> NAVIGATION <i class="fa fa-chevron-down"></i></div>
 
<!--Navigation-->
<div id="navigation">
 
<div id="navigationin"><center>
<a href="{text:Link One Url}"><i class="fa fa-user-circle"></i><span>my stuff</span></a>  
<a href="{text:Link Two Url}"><i class="fa fa-question-circle"></i><span>tutorials</span></a>
<a href="{text:Link Nine Url}"><i class="fa fa-folder-open-o"></i><span>archive</span></a>
<a href="{text:Link Three Url}"><i class="fa fa-star"></i><span>actors</span></a>
<a href="{text:Link Four Url}"><i class="fa fa-film"></i><span>tv shows</span></a>
<a href="{text:Link Eight Url}"><i class="fa fa-link"></i><span>links</span></a>
<a href="{text:Link Five Url}"><i class="fa fa-file-video-o"></i><span>gifsets</span></a>
<a href="{text:Link Six Url}"><i class="fa fa-file-image-o"></i><span>photosets</span></a>
<a href="{text:Link Seven Url}"><i class="fa fa-square-o"></i><span>icons</span></a>
<a href="{text:Twitter url}" target="_blank"><i class="fa fa-twitter-square"></i><span>twitter</span></a>
<a href="{text:Instagram url} target="_blank""><i class="fa fa-instagram"></i><span>instagram</span></a>
<a href="{text:Youtube url}" target="_blank"><i class="fa fa-youtube"></i><span>youtube</span></a>
</center>
</div>
 
</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download