How can I make a symmetrical navbar? - html

I have this navbar:
There is too much space at the left and I want the same space at the left and at the right. I have this code:
<!--html-->
<nav>
<ul>
<li>Tutorials</li>
<li>News</li>
</ul>
</nav>
/*css*/
nav{
background-color: rgb(255, 86, 86);
margin: auto;
height: 50px;
padding: 10px;
width: fit-content;
margin: auto;
}
nav li{
display: inline;
padding: 10px;
}

Looks like you're butting heads with the browser defaults.
It's common to reset the browser's default padding/margin for all elements to zero because some elements (like ul and li) can have some default margin/padding. Also, you didn't specify the list-style-type of the list to be none, which I assume means the unordered list defaults to bullets, a result you don't want.
Here's what I would personally do to accomplish what you're looking for, excluding colors:
* {
margin: 0;
padding: 0;
}
nav {
padding: 10px;
}
nav ul {
list-style-type: none;
display: flex;
}
nav li {
margin-right: 10px;
flex-shrink: 0;
}
nav li:last-child {
margin-right: 0;
}
This puts a 10px space around all of the items, and then a 10px space between each of them. It also forces the list to display horizontally without needing to inline the list items. The CSS Tricks guide to flex box might be helpful to you.
There's many right answers here, so try to have fun with it and pick the method that feels the best to you.

Related

How to set numerous links in a single line with CSS?

I'm trying to make a bar on the top of the page, but am unsure of how to make all links fall on the same line.
This is what I have:
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
overflow: hidden;
text-align: center;
display: block;
width: 60px;
}
<div class id="tb">
Home
News
Contact
About
</div>
I've tried removing many combinations in the CSS, but so far nothing. If the answer is incredibly obvious, please excuse my ignorance, as I am new to CSS.
Thanks,
-Tysuna
You've set a width on #tb which is causing the as inside it to wrap to new lines. The a tags will automatically be placed on the same line as they are classed as inline elements.
Inline-level elements are those elements of the source document that do not form new blocks of content; the content is distributed in lines (e.g., emphasized pieces of text within a paragraph, inline images, etc.).
(http://www.w3.org/TR/CSS21/visuren.html#inline-boxes)
Removing the width will allow the div to fill the entire width of its container (as it is a block element).
A block-level element occupies the entire space of its parent element (container), thereby creating a "block."
(https://developer.mozilla.org/en/docs/Web/HTML/Block-level_elements)
As #tb is now as wide as the body the a tags will be on the same line, only wrapping if the window size is decreased enough.
#tb {
text-align: center;
}
<div class id="tb">
Home
News
Contact
About
</div>
Your CSS in this particular example can also be simplified as many of the properties specified are already defaults, not applicable or would have no effect.
Update: specifiy for a-tag separatly, isntead of inheritance
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
overflow: hidden;
text-align: center;
display: block; /*property will be inherited by 'a'-tag elements too*/
width: 60px;
}
#tb a{
display: inline-block; /* specifiy for a-tag separatly*/
}
<div class id="tb">
Home
News
Contact
About
</div>
You can use some flexbox magic:
#tb {
display: flex;
justify-content: center;
}
<div class id="tb">
Home
News
Contact
About
</div>
Check the below code.
You can use explicitly display:inline-block or display:inline to make any element to stay in the same line.But Here if you remove the width thats enough.
Instead of div structure better use ul which will make your job easier.Bydefault it will align in a line.
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
overflow: hidden;
text-align: center;
display: block;
}
<div class id="tb">
Home
News
Contact
About
</div>
remove width: 60px - #tb
use ul li and display: inline-block for li
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
text-align: center;
display: block;
}
#tb > li{
display: inline-block;
vertical-align: top;
margin: 0 5px;
}
<ul id="tb">
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

Can't get list to center when using UL

I have several <li> items centered and displaying inline within <ul> tags, but I cannot seem to get the <ul> itself to center on the page. You can see the slight indention problem on this JSFiddle.
Here is the HTML for the list:
<ul>
<li>Home</li> |
<li>Knowing</li> |
<li>Caring</li> |
<li>Working</li> |
<li>Living</li> |
<li>Opportunities</li> |
<li>Medical Staff Services</li>
<br> asdf © 2014
</ul>
Here is the CSS:
body {
font-size: 75%; /* Base browser font size is 14px */
font-family: Arial, Helvetica, sans-serif;
line-height: 1.5;
text-align: center;
margin: 0 auto;
width: 75%;
clear: both;
}
a, a:link, a:hover, a:active, a:visited {
text-decoration: none;
}
ul {
margin: 0 auto;
}
li {
text-align: center;
display: inline;
}
.footer a:link, .footer a:visited, .footer a:active, .footer a:hover, {
text-align: center;
color: #cccccc;
}
Annoyingly, the list centers if I delete the <ul> tags from the HTML, but that is improper markup and potentially dangerous/problematic.
I've tried a few other suggestions from similar questions, such as positioning a container div left at width: 100%; with position: relative;, positioning the <ul> div left and floating it left 50%, and then positioning the <li> items left and floating them right 50%, but that doesn't work with regard to the | separators that I have in-between each <li> item.
Is my markup somehow still incorrect? Do I have some conflicting CSS values that prevent it from working? Do <ul> lists always indent, no matter what?
EDIT: Added CSS code directly to the post.
<ul> elements have a default padding-left of 40px. Remove it with padding: 0; or padding-left: 0;.
JSFiddle Example
You can use your browser's F12 developer tools to inspect elements and their margins or padding in the future.
Have a look at this example it centers horizontally the list where the menu items have no set width.
See here: http://www.cssplay.co.uk/menus/centered.html
The basic CSS you need:
footer {
clear: both;
float: left;
overflow: hidden;
width: 100%;
}
ul {
float: left;
left: 50%;
list-style-type: none;
margin: 0 auto;
padding: 0;
position: relative;
}
li {
float: left;
position: relative;
right: 50%;
}
Note: I specifically removed the | separators between the LIs as this is not valid HTML.
See the changed example: http://jsfiddle.net/eNQyp/1/ The separators are added as styles on the LIs. It can work your example using this technique and is valid HTML.

White-space: nowrap makes elements to be outside of window

I am trying to have an UL on the right of a SPAN (http://jsfiddle.net/Shg9L/24/).
<div>
<span>Categories</span>
<ul>
<li>Book</li>
<li>Computer</li>
</ul>
</div>
When the window is resized down the LIs should get stacked but not under the SPAN.
The problem is when I resize the window some of the LI items on the right becomes hidden.
I think it is because of "white-space: nowrap" but without it I'm not able to make it work.
Can this be solved?
Display your unordered list as a block and hide the overflow. It will then take up all the available width. The list items will stack neatly in line with the edge of the unordered list when there isn't enough room:
div ul {
list-style: none;
padding: 0;
margin: 0;
display: block;
overflow:hidden;
}
The white-space:nowrap on your container <div> isn't needed as far as I can see. You can remove it.
JSFiddle
http://jsfiddle.net/Shg9L/29/
removed a coupple of things and added position:absolute; and inline:display-block to some elements
div {background-color:orange;}
div span {
background-color: #E0E0E0;
margin-right: 8px;
margin-bottom: 8px;
padding: 8px;
}
div ul {
position:absolute;
top:0px;left:100px;
list-style: none;
padding: 0;
margin: 0;
display: inline-block;
}
div ul li {
background-color: #F0F0F0;
margin-right: 8px;
margin-bottom: 8px;
padding: 8px;
display:inline-block;
}

Positioning 6 elements in two rows evenly - having trouble (CSS)

I've been trying a few different ways to position 6 different elements in this way:
I've tried using two separate unordered lists stacked on top of each other but I couldn't get them to scale with page stretch properly. I also tried using a table but I can't seem to get the elements to all position in the center of their individual tds.
Here is my css from my unordered lists:
.button ul {
height: auto;
list-style: none;
}
.button li {
display: inline-block;
list-style: none;
margin: 0 18% 0 0;
padding: 0;
vertical-align: top;
}
and these are contained within this:
.newfooterright {
float: left;
width: 33.333333%;
top: 0%;
left: 0%;
height: 250px;
padding: 0 0 0 0;
margin: 0;
font-family: RobotoLight;
text-decoration: none;
text-transform: none;
font-size: 1.5em;
text-align: center;
color: #ffffff;
vertical-align: middle;
}
here's a jsfiddle with this method:
jsFiddle for unordered list
I think an unordered list is probably the way to go... I'm just not sure how to get all of the elements to align in the center of each li. The elements on the bottom seemed to be stuck in the bottom right corner of the li. The bottom elements are also widgets from google+, twitter, and facebook, so I'm not sure if that is affecting their position.
Basically the elements need to be able to do this:
Scale with window width in terms of their spacing (to a point, I don't need an uber small phone layout or something. Something like padding-right or margin-right?)
When the elements scale the bottom element needs to stay aligned with the top element in the center
positioned like in the picture!
Any suggestions on how to get this positioned cleanly would be appreciated!
Thank you so much!
Here is one way you might do it, I am proposing the following HTML scaffolding:
<div class="newfooterleft">
<ul class="button">
<li><a class="twitterbutton" href="#"></a></li>
<li><a class="facebookbutton" href="#"></a></li>
<li><a class="googleplusbutton" href="#"></a></li>
</ul>
<ul class="widget">
<li>(I put the corresponding widget here)</li>
<li>(I put the corresponding widget here)</li>
<li>(I put the corresponding widget here)</li>
</ul>
</div>
and the following CSS:
.newfooterleft {
width: 40%;
border: 1px solid red;
overflow: auto;
}
.twitterbutton {
background: url("http://www.placekitten.com/100/100") no-repeat;
background-size: 100%;
}
.twitterbutton:hover {
}
.facebookbutton {
background: url("http://www.placekitten.com/100/100") no-repeat;
background-size: 100%;
}
.facebookbutton:hover {
}
.googleplusbutton {
background-image: url("http://www.placekitten.com/100/100");
background-size: 100%;
}
.googleplusbutton:hover {
}
.newfooterleft ul {
display: table;
width: 100%;
margin: 0;
padding: 0;
}
.newfooterleft ul li {
display: table-cell;
width: 33.3333%;
margin: 0;
padding: 0;
text-align: center;
vertical-align: middle;
border-left: 1px solid red;
}
.newfooterleft ul li:first-child {
border-left: none;
}
ul.button li {
height: 100px;
}
.button li a {
display: inline-block;
width: 62px;
height: 62px;
}
.button {
border-bottom: 1px solid red;
}
ul.widget li {
background-color: white;
height: 150px;
}
In this case, I am adjusting the unordered lists to behave like tables and table cells.
It works reasonably well except if the width gets too narrow, but this may be okay depending on your application.
You could use a min-width to constrain it.
You could also try some variations with display: table-row.
Demo Fiddle: http://jsfiddle.net/audetwebdesign/2U3D9/
In general, if you want to select every 3 elements, you have to use :nth-child(). In the parenthesis, you can put any combination of n and a digit. There are also some keywords, such as odd and even. So in this case, you are going to have 3 different :nth-child() selectors. It will look like this
li:nth-child(6n+1), li:nth-child(6n+2), li:nth-child(6n+3) {
color:red;
}
The 6n selects every sixth element, and the +1 adds that number. So if you plug in 1, you will get back 7 for the first selector, 8 for the second, and 9 for the third.
Here is a fiddle demonstrating this in use
Here is an article explaining nth-child in more depth
It looks like your li's should be all ready to collapse.You could give your li's a fixed height and width (creating boxes if you will), then add a style for each image to have a relative position and use top and left to get them into position (remember, percentages can scale for you). I've dropped hints on how to accomplish this as you wanted to learn by doing, but let me know if you need the CSS!

ul auto indents as well as only pads one side?

I know there have been many questions about ul auto indents, I've tried with no success many of those answers and I have the additional problem of when adding a container with padding, it seems to only pad one side.
Basically I'm trying to get a ul bar fixed to the bottom of the window. This bar is inside a container (main) to give it padding from either side of the window). It is designed to auto expand with the width of the window (liquidish) so there is no defined width beyond the initial width=device width.
When written by itself, I get a small auto indention on the left side of the ul. I've tried adding 0 padding and margins to the ul and li elements and that seems to have no effect as well as others. When I add in the container it seems to pad only the side with the indent problem. I'm sure its something simple and stupid, but I appreciate any help.
As an aside.. the width of the li elements being 33.3% is due to using a spacing box hack found on stackoverflow. The use of it doesn't change/help/hurt anything in this problem, so I omitted it to keep this a little more simple.
JSFiddle Link http://jsfiddle.net/XdHXf/1/
HTML
<div class="main">
<nav>
<ul>
<li>Main</li>
<li>Second</li>
<li>Third</li>
</ul>
</nav>
</div>
CSS
.main{
width: 100%;
padding: 0 1em;
}
nav{
position: fixed;
bottom: 0px;
background: #455868;
width: 100%;
z-index: 1000;
}
nav ul {
list-style: none;
width: 100%;
padding: 0;
margin: 0;
}
nav li{
position: relative;
float: left;
width: 33.3%;
text-align: center;
background: #455868;
padding: 0;
margin: 0;
}
nav li a{
text-decoration: none;
display: inline-block;
width: 100%;
padding: 15px 0;
}
This comes from the browser's builtin stylesheet. If you add
body {
margin: 0;
}
the small extra space goes away.
As an alternative, you get similar results, when you check Normalized CSS under Fiddle Options.