I want to float my ul to the left and the list items to the right so that they look like this inside a div:
Item 1 Item 2 Item 3
CSS:
.body-nav {
width: 1090px;
margin: 0 auto 0 auto;
background-color: lightblue; }
.body-nav ul {
margin: 0;
padding: 0;
float: left;
list-style: none; }
.body-nav ul li {
float: right;
padding-right: 15px; }
I got the links to look how I want them to look. The problem here is I'm losing my background color. It's like these links are outside of the div.
Here is my HTML:
<header>
<div class="header-content">
<img src="images/logo.png" class="logo" alt="Site Logo">
<ul>
<li>24/7 Support (513) 571-7809</li>
<li>Manage my account</li>
</ul>
</div>
</header>
<div class="body-nav">
<ul>
<li>Web Hosting</li>
<li>Reseller Hosting</li>
<li>Domain Names</li>
<li>SSL Certificates</li>
</ul>
</div><!--end body div-->
The following will fix your issue (http://jsfiddle.net/76y7wbf6/1/):
.body-nav {
overflow:hidden;
}
The issue stems from using floats, which takes a slight step outside of the normal DOM flow. Your .body-nav element loses track of its children, and occupies a height of 0 (or 1px).
Another alternative is to apply a clearfix class to body-nav, which would look something like (http://jsfiddle.net/76y7wbf6/):
.clearfix:after {
clear:both;
display: block;
content: ' ';
}
A metaphore I like to use:
Using floats is like traveling through hyperspace. They exist, kinda, and can impact other DOM elements... but they are also travelling at a different dimensional plane (left-right).
To bridge the float hyperspace travel, you can apply clear:both on itself or overflow:hidden on its parent.
... And if you apply float on a floating element's parent, it can provide a self-clear, but then that parent is traveling through hyperspace too.
This is just to show you that there is simple ways of doing what you want to achieve ( a horizontal unordered list ) instead of using limited approaches such as display:inline-flexor complicated/tricky approaches
Bottom line let's not over-complicate what is simple.
So,
remove the float:left from your .body-nav ul (there is no point on being there)
set your .body-nav ul li to display inline (with this the li's will display as it states - inline - instead of the default behavior display:list-item
Snippet below:
.body-nav {
width: 1090px;
margin: 0 auto 0 auto; /* you can shorthand this to - margin:0 auto - */
background-color: lightblue;
}
.body-nav ul {
margin: 0;
padding: 0;
list-style: none;
}
.body-nav ul li {
display:inline;
padding-right: 15px;
}
<header>
<div class="header-content">
<img src="images/logo.png" class="logo" alt="Site Logo">
<ul>
<li>24/7 Support (513) 571-7809</li>
<li>Manage my account
</li>
</ul>
</div>
</header>
<div class="body-nav">
<ul>
<li>Web Hosting
</li>
<li>Reseller Hosting
</li>
<li>Domain Names
</li>
<li>SSL Certificates
</li>
</ul>
</div>
<!--end body div-->
Instead of mucking around with floats, why don't you make use of CSS3 and use the flexbox layout. Setting the UL display to "inline-flex" will give you the desired result with the lightblue background.
Related
I previously had the anchor element inside of the list item and could target it using "nth-child(4)" but as soon as I placed the anchor element outside of the list item to make the clickable area bigger it became untargetable. Any suggestion on how to target that particular element?
section.social-section ul a li {
width: 22%;
display: inline-block;
float: left;
border: 1px solid #e2e0e0;
margin-right: 30px;
padding: 10px;
}
section.social-section ul.group a:nth-child(4) {
margin-right: 0;
}
<!-- Social section -->
<section class="social-section">
<ul class="group">
<a href="#">
<li>facebook</li>
</a>
<a href="#">
<li>twitter</li>
</a>
<a href="#">
<li>pinterest</li>
</a>
<a href="#">
<li>google+</li>
</a>
</ul>
</section>
Two problems here:
You can't swap the positions of your a and li elements like that. The only children a ul can contain are li elements. The only valid structure for such a list is ul > li > a.
Validity aside, the reason your rules no longer match is because your first rule is still targeting li elements, but your second rule targets a elements. They're targeting different elements, so no overriding is taking place.
You can make the clickable area bigger without altering your HTML by moving some of the CSS properties such as padding to the a elements, as well as making them blocks:
section.social-section ul li {
width: 22%;
display: block;
float: left;
border: 1px solid #e2e0e0;
margin-right: 30px;
}
section.social-section ul.group li:nth-child(4) {
margin-right: 0;
}
section.social-section ul li a {
display: block;
padding: 10px;
}
<!-- Social section -->
<section class="social-section">
<ul class="group">
<li>facebook
<li>twitter
<li>pinterest
<li>google+
</ul>
</section>
(Note that moving the padding declaration causes the width of your li elements to be reduced, but for the purposes of this answer I'm ignoring this side-effect.)
As the comments rightly point out, the immediate children of ul tag must always be zero or more li tags.
However, you say that you are doing this to increase the clickable area. You can do that by putting the a tags back inside the li tags, and then make the a tags display : inline-block. You could then apply width : 100% and height : 100% on a to increase the clickable area (which would then be equal to the entire area of the li). In this manner, you can use nth-child() to target the 4th child.
Your code becomes :
HTML
<!-- Social section -->
<section class="social-section">
<ul class="group">
<li>facebook</li>
<li>twitter</li>
<li>pinterest</li>
<li>google+</li>
</ul>
</section>
CSS
section.social-section ul li {
display: inline-block;
position: relative;
}
section.social-section ul.group li:nth-child(4) a {
display: inline-block;
height : 100%; //not absolutely required
height : 100%; //not absolutely required
}
You can add CSS properties to position the list as you require.
i am trying on a footer to put an element on the far left:
--MY WEB SITE (aligned on the right)--
On the far right (the end of the list must be aligned with the right border of the browser), i want to put in order:
twitter, facebook, newsletter
By floating left, what i get is the opposite of the html file
Thanks all
jsfiddle
<div class=footer>
<UL><H4>MY WEB SITE (aligned on the right)</H4>
<LI><a href=https://www.linkedin.com/pub/ric style='border-bottom: 0px'></A><LI>
<LI>twitter</LI>
<LI>facebook</LI>
<LI><font color='#0894D2 '>NEWSLETTER</FONT></LI>
</UL>
</DIV>
.footer {
border: 2px solid grey;
width:90%;
height:auto;
text-align: center;
}
.footer:after {
content: '';
display: inline-block;
width: 100%;
}
.footer ul {
margin: 0;
padding: 0;
list-style: none;
}
.footer ul li {
margin: 0;
padding: 0;
list-style: none;
display:inline-block;
font-size:12px;
float:right;
}
.footer h4{
margin: 0;
padding: 0;
list-style: none;
display:inline-block;
font-size:12px;
float:left;
}
If you want to change an order of your list items on the right side, just try to change an order of list items tags in html...
<div class="footer">
<ul>
<h4>MY WEB SITE (aligned on the right)</h4>
<li><a href="https://www.linkedin.com/pub/ric" style='border-bottom: 0px'></a></li>
<li><font color='#0894D2 '>NEWSLETTER</font></li>
<li>facebook</li>
<li>twitter</li>
</ul>
</div>
Change your HTML as such:
<div class="footer">
<H4>MY WEB SITE (aligned on the right</H4>
<ul>
<li><font color="#0894D2 ">NEWSLETTER</font></li>
<li><img src="images/facebook.png" />Facebook</li>
<li><a href="https://www.linkedin.com/pub/ric" style='border-bottom: 0px'><img src="images/linkedin.jpg" />LinkedIn</a><li>
<li>twitter<div id="logo_twitter"></div></li>
</ul>
</div>
First things first....
In order to achieve what you are looking for (hope I understood corectly), you should put the H4 tag outside UL. Then, in CSS, set the UL (footer ul) to float:right and the the LIs (footer ul li) to float:left.
See this example: https://jsfiddle.net/w6pa3L30/4/
.footer ul{float:right;}
.footer ul li {float:left;}
Now, you should also review your code. FONT tag is not supported in HTML5. You should also avoid using divs for displaying social icons (I guess that is what you wanted to do inside Twitter LI), instead use span tag. I would also recommend you to avoid using inline styling; style them inside your stylesheet.
I'm doing some practice with layout using css, and I've come across a weird thing that I don't know how to explain. Where does the space highlighted in red in the following image come from, and how do I eliminate it?
HTML:
<body>
<div class="menu-bar">
<ul>
<li>
Home
<ul>
<li>Beach House</li>
<li>Ski Cabin</li>
<li>Country Cottage</li>
</ul>
</li>
<li>
News
<ul>
<li>World News</li>
<li>National News</li>
<li>Local News</li>
</ul>
</li>
<li>
Contact
<ul>
<li>Email Address</li>
<li>Phone Number</li>
<li>Postal Address</li>
</ul>
</li>
<li>
About
<ul>
<li>About Me</li>
<li>About You</li>
<li>About Us</li>
</ul>
</li>
</ul>
</div>
</body>
CSS:
body {background: #77c4d3; padding:1%; }
div.menu-bar{position: relative; max-width: 700px;}
/*Styles for both menu and submenus: */
div.menu-bar ul { list-style-type: none; margin: 0; padding:20px; background: gray;}
div.menu-bar li { background:white; text-align:center; display:inline-block; padding:10px;}
/*Menu-specific styles: */
div.menu-bar > ul {width: 100%;}
div.menu-bar > ul > li {width:20%; border:0; margin: 0; padding-top: 10px; padding-bottom: 10px;}
/* Submenu-specific styles */
div.menu-bar ul ul {background-color: blue; padding-left: 10px; padding-right: 10px;}
/*Hide any unodered lists that are descendents of a list element by default*/
div.menu-bar li ul {
display: none;
}
/*Select any unordered lists that are children of list elements that are being hovered on.*/
/* The <ul> is display:block, but the individual <li> elements are inline-block, which is what matters.*/
div.menu-bar li:hover > ul {
display: block;
position: absolute;
}
That comes from the wrapping <ul> below <div class="menu-bar">. It's width is set to 100% in your css where you say:
div.menu-bar > ul {
width: 100%;
}
Since the buttons don't fully take up the space in that <ul> there is some extra grey space. If you add a text-align: center; to that style, it will nicely center your buttons, as so:
div.menu-bar > ul {
width: 100%;
text-align: center;
}
Or check out my JSFiddle for this.
There are a couple of things going on here to be aware of.
1.) You have space in your code, between your top-most <li>'s. This is a funky issue with whitespace. CSS-Tricks has a great summary of the issue. Essentially, you have to put your closing </li> tag back-to-back with the next opening <li> tag, to get rid of those tiny gaps.
2.) Secondly, your width is set to 20%. You can bump this up to even quarters, at 25%...although you'll notice that this pushes the last of your <li>s down a line. This is because...
3.) There is a 10px padding on div.menu-bar li which applies 10px of padding to the left, right, top and bottom. Your div.menu-bar > ul > li rules specify a top and bottom padding, but the left and right are left the same. My personal approach for this?
4.) Use box-sizing. By setting this CSS property to border-box, your padding is included in the width of your elements. In other words, you can set the following CSS:
div.menu-bar > ul > li {
width: 25%;
margin: 0;
padding: 12px;
box-sizing: border-box;
}
...and you'll end up with a set of list items that have a) no funky, tiny space between them and b) are all on the same line.
Hope that helps!
Each nav item's width is dependent on it's text content. It has no knowledge of how wide it's parent is. Each nav item is just shoved as far left as it can go next to it's neighbor.
If you are looking to have the nav items fill the bar evenly, you will need to use a flex solution. See here for a complete explanation.
So what I need help with, is how do I remove the newline after a <li> and or <ul>
This is my css:
#ranks li {
background: url(/img.png) no-repeat top left;
}
#ranks .sprite-admin{ background-position: 0 0; width: 157px; height: 44px; }
#ranks .sprite-banned{ background-position: -207px 0; width: 157px; height: 44px; }
and this is the html:
<ul id="ranks"><li class="sprite-admin"></li></ul>
It all works well while only one of the <ul id ="etc"> is there, but if there are multiple, it will make a new line and 'stack' them.. is it possible to make them not stack, and just go left to right?
Thanks
EDIT:
Demo : /removed/
You have a few options:
#ranks li {
float: left;
}
This will float all of your list items to the left, without wrapping, until horizontal screen space is no longer available. Alternatively,
#ranks li {
display: inline-block;
}
Which will also put your elements side-by-side, but handle them as bock level elements. If you don't care about block-level styling, you could go with straight inline-display:
#ranks li {
display: inline;
}
Which will treat the list items like any other inline element (such as <span> or <a>).
There are some other inherent styles that exist on list items, as well as their list parent, that you may need to do away with. Be sure to check out margin, and padding.
Demo: http://jsbin.com/iconud/edit#html,live
Look Out Ahead!
You may find that there is an unsightly gap between your list items when they're positioned side-by-side. This is a common problem with inline-lists. One solution is to remove the newline space between closing and opening list item tags:
<ul id="ranks"><li>
Index</li><li>
Contact</li><li>
Portfolio</li>
</ul>
Or have them all inline, a little less discernible:
<ul id="ranks">
<li>Index</li><li>Contact</li><li>Portfolio</li>
</ul>
This is a little tough on the eyes. With HTML, since closing tags aren't always required, you can also leave off the closing tag (though this makes me a bit nervous):
<ul id="ranks">
<li>Index
<li>Contact
<li>Portfolio
</ul>
Multiple Lists Inline Too!
From some of the OP's comments, it appears they might be trying to get not only list items inline, but lists themselves. If that's the case, apply the same aforementioned rules to the lists themselves:
#ranks,
#specs {
margin: 0;
padding: 0;
display: inline-block;
}
#ranks li,
#specs li {
display: inline-block;
border: 1px solid #CCC;
padding: 5px 10px;
}
Here were have identified two sets of rules using selectors that search for id's, and then tags. You could simplify this by apply a common class to the lists, or by basing the selectors off of a common parent element. Next is the markup:
<ul id="ranks">
<li>Index</li>
<li>Contact</li>
<li>Portfolio</li>
</ul>
<ul id="specs">
<li>Foo</li>
<li>Bar</li>
</ul>
This results in both lists, and their items, being displayed in a horizontal fashion.
Demo: http://jsbin.com/iconud/2/edit
with some css
<style type="text/css">
#ranks li { display:block; float:left; }
</style>
updated as comments: with display:block
ul li{ display:inline;} do the trick?
<li> by default is display:block;
if you give it display:inline; or diplay:inline-block; that should remove the linebreak
This is a basic example of horizontal UL's
HTML
<ul id="list">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
</ul>
<span class="clearFloats">
CSS
.item {
float: left;
}
.clearFloats {
clear: both;
}
JSFiddle Example: http://jsfiddle.net/peterf/DEUBf/
Another option is to set font-size: 0 in the ul, then restore the desired font-size in the li tags. I prefer this as it's contained within the ul tag, doesn't need further hacks like clear:both, and explains better what the styling is meant to do (hide anything not inside a list item).
ul {
list-style-type: none;
font-size: 0;
}
li {
display: inline-block; /* Or inline, as you like */
font-size: 16px;
}
I am trying to center my navigation links inside the div but no matter what I've tried it won't work. I've tried margin-left:auto, margin-right:auto, but nothing...
Here is the section of CSS code:
#nav {
display:block;
background-color:#505050;
height:17.5px;
box-shadow: 0px 0px 15px 5px #CCCCCC inset;
border:1px solid #EEEEEE;
border-radius:20px;
padding:1.5%;
}
#nav li {
padding:0px 20px 0px 20px;
display:inline;
/*float:left;*/
list-style:none;
position:relative;
}
#nav li a {
padding:0px 0px 20px 0px;
color:#FFFFFF;
text-decoration:none;
}
and here is my ul code:
<ul id="nav">
<li>Home</li>
<li>About Us</li>
<li>Current Litters</li>
<li>Gallery
<ul>
<li>Bandi</li>
<li>Studs Used</li>
<li>Test Dog2</li>
<li>Test Dog3</li>
</ul>
</li>
<li>Contact Us</li>
</ul>
Here is the rest of my code
actually without it i noticed that my drop down menu under (gallery) doesn't display correctly, ...here is the rest of that css file...that shows what happens to the drop down...maybe you can tell me why the float screws it all up...
...and the text align did great....but only after removing the float...
#nav li a:hover {
text-decoration:underline;
}
#nav li ul{
padding:10px;
font-size:medium;
display:none;
position:absolute;
left:0px;
top:30px;
background-color:rgba(50,50,50,0.8);
}
#nav li:hover ul {
display:block;
border-radius:20px;
border:1px solid;
width:150px;
}
This is actually quite simple, since your list items are display:inline. Add this style:
#nav {
text-align:center;
}
Demo: http://jsfiddle.net/fH6f5/
There are many other ways to do it, but this appears to be all you need. Just make sure not to float the <li>s (I see you have it commented out).
Adding text-align: center to the nav unordered list seems to work for me in chrome
#nav {
text-align: center;
}
To center a block element, you also need to explicitly set the width to some value, like this:
#nav {
width: 50%;
margin: 0 auto;
}
There are quite a few changes you're going to need to make to your code in order for it to display properly. Your list elements are currently inline elements. inline elements have a lot of restrictions, including not being able to explicitly set their width, height, and their top and bottom margin. Keep in mind that per the W3 spec:
Generally, inline elements may contain only data and other inline elements.
That being said, you can use display: inline-block with no problems for your current code. There is one very important thing to keep in mind about using inline-block elements: whitespace. Any space between inline-block elements in your code will be shown as a space on your browser. So, if you want the elements to be touching, their tags must be touching also:
<!-- Version A: This will produce a gap between the two elements -->
<li>Home</li>
<li>About Us</li>
<!-- Version B: This will not produce a gap between the two elements -->
<li>
Home
</li><li>
About Us
</li>
If you choose Version A from the code above, I'd recommend you float the elements rather than relying on inline-block for positioning. Centering a floated list is a bit more difficult than centering an inline list. Here's a way that I like to center floated elements:
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
</nav>
CSS:
nav { overflow: hidden; }
nav ul {
position: relative;
float: left;
left: 50%;
list-style: none;
padding: 0; }
nav ul li {
position: relative;
float: left;
right: 50%;
margin: 0 5px; }
nav ul li a { display: block; }
Preview: http://jsfiddle.net/Wexcode/rsDbY/
You should post the design that you want for your dropdown menu, I don't really know what you want your final result to look like so I can't really help you with that.
You need to set a fixed width on your ul for margin-right:auto and margin-left:auto
Have you tried to add margin: 0 auto; to #nav style? You also have to set the ul width to get this working.
It's a bit more complicated then simply "text-align" as you have the text inside of a . You need to add "margin: 0px auto;" to your element in your css file. This will then center the divider on the screen first, then center the next element within the divider and so on.