Reputation: 387
I have a 4 tabs which I have showed below. It keeps moving as the screen size changes and with different browsers. I see about.html going down each time I open it on different computers it doesn't stay on a single line tab. I need to keep changing my width:303px
.
Actually I need to reduce it on different computers and browsers. Whats the best way to fix this so that it stays on a single line with rest of the tabs.
css
ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li
{
float: left;
}
a:link, a:visited {
display: block;
width:303px;
font-weight: bold;
color: #FFFFFF;
background-color: #98bf21;
text-align: center;
padding: 4px;
text-decoration: none;
text-transform: uppercase;
}
html
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="offers.html">Offers</a></li>
<li><a href="contact.html">Contact</a><li>
<li><a href="links.html">Links</a></li>
<li><a href="about.html">About us</a></li>
</ul>
<br>
<br>
Upvotes: 0
Views: 1392
Reputation: 21882
You could use the calc()
function....
width: calc(100% / X);
Where X is the number of tabs.
You can also use....
width: calc((100% / X) - Ypx);
Where Y is the sum amount of padding, margin, or space you want between tabs.
So for 4 tabs with margin: 0 5px;
between them and a 1px border on them, you'd use:
width: calc((100% / 4) - 48px);
Upvotes: 0
Reputation: 115066
You have a couple of options that will space and align these without explicit widths (either % or actual px/em etc values):
CSS Tables (Green Version) - Support IE8 and above
ul.table {
display: table;
table-layout: fixed;
width: 100%;
}
ul.table li {
display: table-cell;
}
Flexbox (Plum Version) - Support IE10 and above
ul.flex {
display: flex;
}
ul.flex li {
flex:1;
}
Demo of both
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
margin-bottom: 1em;
}
ul.table {
display: table;
table-layout: fixed;
width: 100%;
}
ul.table li {
display: table-cell;
}
ul.flex {
display: flex;
}
ul.flex li {
flex: 1;
}
ul.flex a {
background: plum;
}
a:link,
a:visited {
display: block;
font-weight: bold;
color: #FFFFFF;
background-color: #98bf21;
text-align: center;
padding: 4px;
text-decoration: none;
text-transform: uppercase;
}
<ul class="table">
<li><a href="index.html">Home</a>
</li>
<li><a href="offers.html">Offers</a>
</li>
<li><a href="contact.html">Contact</a>
</li>
<li><a href="links.html">Links</a>
</li>
<li><a href="about.html">About us</a>
</li>
</ul>
<ul class="flex">
<li><a href="index.html">Home</a>
</li>
<li><a href="offers.html">Offers</a>
</li>
<li><a href="contact.html">Contact</a>
</li>
<li><a href="links.html">Links</a>
</li>
<li><a href="about.html">About us</a>
</li>
</ul>
Note: This is not a one-stop solution. At some point the screen may not be wide enough to hold all the text. At that point you need media queries.
Upvotes: 1