I've created a navigation using the CSS below to achieve equal distance between the nav elements. However, I've run into an issue where the ::after element creates empty space below the list area, the height of which I cannot seem to adjust.
See this image:
Rendered List with wasted space
ul.formnav {
padding: 0px 8%;
justify-content: space-between;
text-align: justify;
}
ul.formnav::after {
background-color: red;
content: 'This is wasted Space';
display: inline-block;
width: 100%;
height
}
ul.formnav li {
display: inline-block;
color: #FFF;
background-color: #999; /*Circle Formatting*/
-webkit-border-radius: 50%;
border-radius: 50%;
text-align: center;
width: 30px;
height: 30px;
line-height: 30px;
}
<div>
<ul class="formnav">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
I can adjust the contents themselves, e.g. I can set height:0px and the red background will disappear or simply set content:''; but the space taken up by the ::after element remains unchanged. Any idea how to fix this without dropping display:inline-block ? (can't use flexbox due to compatibility)
The extra white space that appears is due to the pseudo-element forming a line box in the new line, and the vertical spacing is the line leading divided above and below the baseline.
If you set line-height: 0 on the parent container and then vertical-align: top on the pseudo-element, this will get shrink the white space to zero height.
ul.formnav {
padding: 0px 8%;
justify-content: space-between;
text-align: justify;
border: 1px dotted blue; /* demo only */
line-height: 0;
}
ul.formnav::after {
content: '';
display: inline-block;
width: 100%;
vertical-align: top; /* gets rid of space below the baseline */
}
ul.formnav li {
display: inline-block;
color: #FFF;
background-color: #999; /*Circle Formatting*/
-webkit-border-radius: 50%;
border-radius: 50%;
text-align: center;
width: 30px;
height: 30px;
line-height: 30px;
}
<div>
<ul class="formnav">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
Or you could just use flexbox:
ul.formnav {
padding: 0px;
display: flex;
justify-content: space-between;
border: 1px solid grey;
}
ul.formnav li {
color: #FFF;
background-color: #999;
/*Circle Formatting*/
-webkit-border-radius: 50%;
border-radius: 50%;
text-align: center;
width: 30px;
height: 30px;
line-height: 30px;
}
<div>
<ul class="formnav">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
Related
I have this button I am trying to make, but I am unable to get the text to be centered. I have previously looked at what past people have answered on how to align text within a button, but the ones I have tried do not seem to work.
.btn1 {
width: 160px;
height: 40px;
align-items: center;
text-align: center;
display: inline;
border-radius: 60px;
background-color: white;
border-color: #05434a;
border-width: 3px;
box-shadow: 5px 6px #05434a;
text-transform: uppercase;
font-size: 1.7vh;
margin-left: auto;
margin-right: auto;
vertical-align: middle;
font-family: poppins;
padding: 0px;
}
.nd {
text-decoration: none;
}
.social-link {
text-align: center;
margin-top: 20px;
margin-bottom: 0 !important;
}
<div class="social-link">
<a class = "nd" href = "">
<button class="btn1">
<p>text</p>
</button></a>
</div>
While one thing that seems OK is centering of the text, your HTML has these problems:
Error: The element button must not appear as a descendant of the a
element.
Error: Element p not allowed as child of element button in this
context
So this snippet removes these two elements and moves the CSS button styling onto the anchor element. It makes this inline-flex to help center the text.
Note: the text is centered though it can sort of appear as if it's a bit high because of the visual strength of the shadow. This snippet puts a 1px width border on the element just so you can assure yourself the text is centered.
.btn1 {
width: 160px;
height: 40px;
align-items: center;
justify-content: center;
text-align: center;
display: inline-flex;
border-radius: 60px;
background-color: white;
border-color: #05434a;
border-width: 1px;
border-style: solid;
box-shadow: 5px 6px #05434a;
text-transform: uppercase;
font-size: 1.7vh;
margin-left: auto;
margin-right: auto;
font-family: poppins;
padding: 0px;
}
.nd {
text-decoration: none;
}
.social-link {
text-align: center;
margin-top: 20px;
margin-bottom: 0 !important;
}
<div class="social-link">
<a class="nd btn1" href="">
text
</a>
</div>
Well, pretty easy and always works:
display: flex;
justify-contect: center;
align-item: center;
that will get the job done
for the element, set:
text-align: center;
I am using the css line-height propriety to position the :beforetext of the li element vertically in the middle of it's border as you can see in the snippet. My problem is that I want it to stay at the middle if the font size changed (for example if the user used the zoom text only functionality of the firefox browser). I thought of using line-height: calc(1 / 2em) but this wouldn't work since the / operator accepts only a number at the right side. here is my code
li {
list-style-type: none;
width: 20%;
float: left;
font-size: 10px;
position: relative;
text-align: center;
text-transform: uppercase;
color: purple;
}
li:before {
width: 40px;
height: 40px;
border: 4px solid purple;
content: counter(step);
counter-increment: step;
line-height: 40px;
font-size: 15px;
display: block;
text-align: center;
margin: 0 auto 10px auto;
border-radius: 50%;
}
<ul>
<li> element</li>
<li> element</li>
<li> element</li>
<ul>
you can use flexbox to align items inside your li which I think is a better solution than line-height
Try :
display: flex;
align-items: center;
justify-content: center;
in your li:before and remove the line-height. I think you will get the desired result.
Hope this helps
I'm trying to make a side menu and i want to use flexbox model. But i have a problem that i haven't solved.
In my menu there are some list element (like dashboard, componenets, ...). This list elements are in <a> tag. When you look with inspector tool the height of <a> tag is normal but containing <li> tags height is strangely long. There is some padding in <li> but not that much. There is no reaseon for the <li> is long like this.
The reason that i use flexbox model in <a> is there is some other element is <a> to be positioned in my original project. But i didn't put them in this example.
The height of <a>: https://postimg.org/image/7cqo2pazf/
The height of <li>: https://postimg.org/image/kgw8feaqz/
Why do you think this strangely long height of <li> is happened?
.left-menu {
position: fixed;
top: 0px;
bottom: 0px;
left: 0px;
width: 220px;
background-color: #eee;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
.left-menu .menu-container>ul {
margin: 0px;
padding: 0px;
}
.left-menu .menu-container>ul>li {
padding: 2px 8px;
}
.left-menu .menu-container>ul>li>a {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
position: relative;
padding: 6px 8px;
text-decoration: none;
font-size: .875em;
border-radius: 4px;
font-weight: bold;
color: #111;
}
.left-menu .menu-container>ul>li>a:hover {
background: rgba(212, 212, 212, 0.5);
}
<div class="left-menu">
<div class="menu-container">
<ul>
<li>Dashboard</li>
<li>Components</li>
<li>Forms</li>
<li>Tables</li>
</ul>
</div>
</div>
Simply add display:block to your list.
.left-menu .menu-container>ul>li {
padding: 2px 8px;
display: block;
}
Have a look at this snippet
.left-menu {
position: fixed;
top: 0px;
bottom: 0px;
left: 0px;
width: 220px;
background-color: #eee;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
.left-menu .menu-container>ul {
margin: 0px;
padding: 0px;
}
.left-menu .menu-container>ul>li {
padding: 2px 8px;
display:block;
}
.left-menu .menu-container>ul>li>a {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
position: relative;
padding: 6px 8px;
text-decoration: none;
font-size: .875em;
border-radius: 4px;
font-weight: bold;
color: #111;
}
.left-menu .menu-container>ul>li>a:hover {
background: rgba(212, 212, 212, 0.5);
}
<div class="left-menu">
<div class="menu-container">
<ul>
<li>Dashboard</li>
<li>Components</li>
<li>Forms</li>
<li>Tables</li>
</ul>
</div>
</div>
I'm trying to get the Nav Bars ul and li's to go to the end (right) and there I'm going to push them 150px away with margin to center it. It worked but then I wanted to center the logo and nav bar's ul and li's using Flex Align-items: center; but then justify - flexend wont work. Can't see too figure out why, I usually don't use flex although I should I guess.
Also I can't use things like <header>, <b> so I have to make a div around it when using flex, is that normal?
Here is the HTML that's wrong.
<div class="header">
<img src="img/logo.png" alt="" class = "logo" />
<ul class = "nav">
<li>PROJECTS</li>
<li>TESTIMONIALS</li>
<li>OUR PROCESS</li>
<li>ABOUT US</li>
<li>CONTACT US</li>
</ul>
</div>
And here's the CSS that's wrong.
.header{
display: flex;
align-items: center; /* Here's the align items im talking about*/
background-color: #d40050;
height: 112px;
width: 100%;
}
.logo{
display: inline-block;
margin-left: 150px;
width: 11rem;
}
.nav{ /*Think this is what's bugging*/
display: flex;
justify-content: flex-end; /*This is the flex end I'm talking about*/
margin-left: 35%;
}
.nav li{
display: inline-block;
position: relative;
color: white;
margin-left: 30px;
font-family: 'Lato';
font-size: 15px;
font-weight: bold;
}
.nav li a.active{
border-radius: 3px;
background-color: #aa0040;
padding: 3px 12px 3px 12px;
}
.nav li a.active:after{
content: ' ';
width: 0px;
height: 0px;
border-top: 10px solid #aa0040;
border-left: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid transparent;
position:absolute;
left: 50%;
top: 100%;
margin-left: -10px;
}
P.S - Here's the website live: tsuts.tskoli.is/2t/2809984199/skapalon/
Use the justify-content property on the flex container.
.header{
display: flex;
align-items: center;
justify-content: space-around; /*Add this line of code*/
background-color: #d40050;
height: 112px;
/*Remove this
width: 100%;
*/
}
.logo{
display: inline-block;
/*
Remove this:
margin-left: 150px;
*/
width: 11rem;
}
/*
Remove this:
.nav{
display: flex;
justify-content: flex-end;
margin-left: 35%;
}
*/
I'm working on my first web page and ran into an issue with finding the right code for my sticky nav bar. The nav bar already has a jQuery code attached to it so I'm wondering if that may affect the code, or if I'm not using the right one.Here's the HTML for the nav bar...
<div id="tab_container">
<nav id="tabs">
<ul id="nav">
<li class="active">About</li>
<li class="inactive">Services</li>
<li class="inactive">Our Staff</li>
<li class="inactive">book</li>
<li class="inactive">Gift Cards</li>
<li class="inactive">Reviews</li>
</ul>
</nav>
</div>
Heres my css for it...
#tab_container
{
background-color: #1E1E1E;
display: -webkit-box;
-webkit-box-flex: 1;
display: block;
position: relative;
max-width: 970px;
width: 100%;
text-align: center;
}
#tabs
{
float: left;
margin-top: 0px;
width: 100%;
max-width: 970px;
background-color: #1E1E1E;
padding-top: 20px;
text-align: center;
}
#nav
{
width: 100%;
max-width: 970px;
text-align: center;
background-color: #1E1E1E;
}
ul
{
float: left;
max-width: 970px;
display: -webkit-box;
-webkit-box-flex: 1;
width: 100%;
padding-left: 0px;
margin-bottom: 0px;
margin-top: 0px;
margin-right: 0px;
text-align: center;
background-color: #1E1E1E;
}
ul li
{
display: inline-block;
text-align: center;
width: 158px;
height: 70px;
background-color: #1a1a1a;
font-size: 18px;
text-transform: uppercase;
text-align: center;
margin:0 auto;
padding: 0;
}
ul li a
{
color: #54544b;
display: block;
height: 100%;
width: 100%;
text-decoration: none;
text-align: center;
margin: 0px auto;
line-height: 70px;
}
a:hover
{
color: #CF7BA1;
}
.active a
{
text-decoration: underline;
color: #CF7BA1;
background-color: #222;
}
And for the jQuery I've already included....
$(document).ready(function(){
$('ul#nav li').click(function(){
var number = $(this).index();
$('.sec').hide().eq(number).show();
$(this).toggleClass('active inactive');
$('ul#nav li').not(this).removeClass('active').addClass('inactive');
});
$('.sec').not(':first').hide();
});
To make it sticky you would use position: fixed in the css. That makes it stay in the same position relative to the browser window. You don't have to use any jQuery for this part.
Try position:fixed;
#tab_container
{
background-color: #1E1E1E;
display: -webkit-box;
-webkit-box-flex: 1;
display: block;
position: fixed; //change this to fixed....to stick to top
max-width: 970px;
width: 100%;
text-align: center;
}
You have to compensate for the height of the header though, in your container div, so notice in my demo I have padding of 200px added to "bodydiv" to compensate
DEMO HERE
It is possible to have a nav bar that is sticky even without using javascript, jquery or any other scripting languages. You only need to add another attribute to your CSS code.
Everything that does the magic is the position: fixed attribute. The position attribute denotes the type of the position of a particular division in HTML. It is followed by the attribute(s) that mention the pixel position. They are top, bottom, right, and left.
Here is the code:
#tab_container
{
background-color: #1E1E1E;
display: -webkit-box;
-webkit-box-flex: 1;
display: block;
position: relative;
max-width: 970px;
width: 100%;
text-align: center;
position: fixed;
top: 0px;
}
#tabs
{
float: left;
margin-top: 0px;
width: 100%;
max-width: 970px;
background-color: #1E1E1E;
padding-top: 20px;
text-align: center;
}
#nav
{
width: 100%;
max-width: 970px;
text-align: center;
background-color: #1E1E1E;
}
ul
{
float: left;
max-width: 970px;
display: -webkit-box;
-webkit-box-flex: 1;
width: 100%;
padding-left: 0px;
margin-bottom: 0px;
margin-top: 0px;
margin-right: 0px;
text-align: center;
background-color: #1E1E1E;
}
ul li
{
display: inline-block;
text-align: center;
width: 158px;
height: 70px;
background-color: #1a1a1a;
font-size: 18px;
text-transform: uppercase;
text-align: center;
margin:0 auto;
padding: 0;
}
ul li a
{
color: #54544b;
display: block;
height: 100%;
width: 100%;
text-decoration: none;
text-align: center;
margin: 0px auto;
line-height: 70px;
}
a:hover
{
color: #CF7BA1;
}
.active a
{
text-decoration: underline;
color: #CF7BA1;
background-color: #222;
}
<div id="tab_container">
<nav id="tabs">
<ul id="nav">
<li class="active">About</li>
<li class="inactive">Services</li>
<li class="inactive">Our Staff</li>
<li class="inactive">book</li>
<li class="inactive">Gift Cards</li>
<li class="inactive">Reviews</li>
</ul>
</nav>
</div>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
<p> Some Text <p>
Here we are using position: fixed followed by top: 0px because we want the nav bar to always be fixed at 0 pixels from the top of the screen.
If you are looking for a STICKY one, as the question says, you better opt for position: sticky. Hope it helps.
If You want to know more about the position attribute in CSS, please visit https://www.w3schools.com/css/css_positioning.asp