Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Have an image floated to the left and two unordered lists floated to the right. For some reason the lists float side by side and not on top of each other. I can't get list-2 to float underneath float 1.
Would anybody have any ideas?
<div class="container">
<img src="yogapic1.png"/>
<ul class="list-1">
<li>Home</li>
<li>Influences</li>
<li>About Me</li>
<li>Classes</li>
<li>Andrews Video Blog</li>
<li>Photography</li>
</ul>
<ul class-"list-2">
<li>Find Us</li>
<li>Contact</li>
<li>Facebook</li>
</ul>
</div><!--container-->
.container {
max-width: 1075px;
margin: auto;
}
.container img {
float: left;
}
.list-1 {
float: right;
}
.list-2 {
float: right;
}
.list-2 {
float: right;
clear: right;
}
float: right will, if there's enough room, put the element to the left of any other elements which have been floated to the right. To override that behaviour and guarantee that it will fall below the most recent float:right element you need to use clear:right.
You've got a typo on list 2 (- instead of =). If you also add clear:right; to the lists it should do what you want:
Example: http://jsfiddle.net/nkYun/
Wrap the two lists in a single div element and float that to the right instead.
By floating the two lists individually, you are taking them out of the normal flow of the document, so they no longer force elements to appear below them.
To float two lists on top of each other you simple need 3 steps:
The container should have position as relative position:relative
One of the 2 lists should be float for example to the right float:right
The second list should have position absolute position:absolute and the right of it is 0px so that it start from the right and be on the first list
Here is an alternative approach. Sometimes trying to figure out what should be floated left, or what should be floated right can be confusing in code. But if you have a general idea of what elements you want to be floated, (and perhaps later on you will decide to add more to that section), then contain the items within a parent container (in our case a div element with a css class called lists) and float the entire parent where you want to be. Here's an example...
The CSS:
.container {
max-width: 1075px;
margin: auto;
}
.container img {
float: left;
}
.lists {
float: right;
border:solid 1px pink;
}
The HTML:
<div class="container">
<img src="yogapic1.png"/>
<div class="lists">
<ul class="list-1">
<li>Home</li>
<li>Influences</li>
<li>About Me</li>
<li>Classes</li>
<li>Andrews Video Blog</li>
<li>Photography</li>
</ul>
<ul class-"list-2">
<li>Find Us</li>
<li>Contact</li>
<li>Facebook</li>
</ul>
</div>
</div><!--container-->
And my fiddle
Notice too with this approach, you are no longer leaving the headache of ordering your lists with floats, but now, within this parent container, you can simply go into your HTML code and sore the lists, in any order you want them to be.
Enjoy!
Try to assign width to your list:
.list-1 {
float: right;
width:100%;
}
.list-2 {
float: right;
width:100%;
}
Edit
If you are giving same CSS to both lists, try something like this
.container ul{
float:left;
width:100%;
}
If you float list-2 to the right and clear: right too that should work. Like:
list-2 {
float: right;
clear: right;
}
Related
I am trying to create a navigation bar for my website. Beside the cart option, I would like to incorparate a shopping cart image. I am trying to use float:right; and margin to be able to position this element. For some reason, the margin is disregarded and doesn't work. I have looked over countless questions regarding this topic, but none of the answers have been relevant to my problem. Any help is greatly appreciated. Once again, I understand their are many questions regarding my problem, but none of them have resulted in a fix. I have also tried padding, but that didn't work either. Thanks in advance for any help. It is greatly appreciated. Below is my code:
.cart {
clear: both;
margin-left: 975px;
margin-top: -25px;
float: right;
display: block;
overflow: auto;
}
<body>
<nav>
<ul>
<li>Categories</li>
<li>Sale</li>
<li>Contact</li>
<li>Login/Sign Up</li>
<li>Cart</li>
</ul>
</nav>
<img src="shoppingcart.png" class="cart" height=25 width=25>
</body>
When you use float:right, it is logically to use margin-right property because float: right will push the item to the most right and if you want to set some margin between the element and the most right boundary you have to use the margin-right.
The opposite way won't work because you are setting the element to float right, but at the same time you are setting its left margin which will be overriden by the float property.
Also setting the left margin is a hard-coded solution, i.e. you don't know how big margin is required on different screen size. But instead it is most probably that you know how big the margin should be from the right side.
Please take a look at the snippet below.
.cart {
clear: both;
margin-top: -25px;
margin-right: 50px;
float: right;
display: block;
overflow: auto;
}
<nav>
<ul>
<li>Categories</li>
<li>Sale</li>
<li>Contact</li>
<li>Login/Sign Up</li>
<li>Cart</li>
</ul>
</nav>
<img src="https://d30y9cdsu7xlg0.cloudfront.net/png/28468-200.png" class="cart" height=25 width=25>
I'm working on a single page web design, i want to use hrefs with # to link different places in the same archive, the desired functionality is to move to the link location when clicking, this works BUT, only if i float left A,B divs,
i dont understand this, A and B divs are containing other stuff already, but if i dont put the float: left in the css, links dont work. Why is that?
#A,
#B {
float: left;
}
.cont1 {
width: 100%;
height: 1500px;
background-color: #2077a5;
float: left;
}
<div id="nav">
<ul>
<li>About
</li>
<li>Portfolio
</li>
</ul>
</div>
<div id="A">
<div id="about" class="cont1">
About page content goes here.
</div>
</div>
<div id="B">
<div id="portfolio" class="cont1">
Portfolio page content goes here.
</div>
</div>
EDIT: Wow, that are some fast answers, thanks a lot, i think im not being understood, my question is WHY if i dont set any style for A and B divs links dont work, dont they expand a locate automatically by being containers of style defined divs?
If you insist on using the float:left; for #A and #B, add clear:both; to that first rule to have them NOT begin at the same line(in which case the local anchors wouldn't make any sense):
#A,
#B {
float: left;
clear: both;
}
http://codepen.io/anon/pen/NRRxGy
ADDITION AFTER COMMENT AND EDIT OF QUESTION:
To try and see, delete the float for A and B (http://codepen.io/anon/pen/bwwEAr) and have a look at it in the developer tools: Both #A and 'B will have a size of 1406 x 0 (!) and will be at the very same position.
That's because DIVs that only contain floated elements will have no "official" height (i.e. they won't wrap their actual contents) - search for questions about floating to get examples. So vertically the "unfloated" #A and #B are at the same height, which is why the links lead to the same scroll position. Strange stuff, but it all has to do with floated elements and how floating elements affects the height of their containers.
problem is you set .cont1 class for both section and make them float left with a height of 1500px. try with this css
#A, #B {
min-width: 100%;
}
.cont1 {
height: 100vh;
background-color: #2077a5;
}
you can use the id="about" and id="portfolio" for link so you don't need the extra div #A , B
<div id="nav">
<ul>
<li>About</li>
<li>Portfolio</li>
</ul>
</div>
<div id="about" class="cont1">
About page content goes here.
</div>
<div id="portfolio" class="cont1">
Portfolio page content goes here.
</div>
.cont1 {
width: 100%;
height: 1500px;
background-color: #2077a5;
float: left;
}
This question already has answers here:
Float:right reverses order of spans
(16 answers)
Closed 6 years ago.
So I've run into this problem a few times, how come my navigation menu is reversed?
.navigation li a {
display: inline-block;
text-decoration: none;
float: right;
margin-right: 10px;
}
ul {
list-style-type: none;
}
<div class="container-fluid">
<nav class="navigation">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</nav>
</div>
When put into action, the menu order is reversed, why is this happening?
You let the elements float to the right. Try to picture that..
The first element enters the document and floats all the way to the right, bumping into the side of the screen. The second enters and floats to the right as well, but bumps into the left side of the first, and stays there. And so on, and so on.
A better solution would be to use display: inline-block for the elements, and float-right for the parent (the ul).
But personally I'm not a big fan of floating at all, so I would use text-align on the ul. text-align keeps it a normal line of text, which can also include images or other inline and inline-block elements. By right-aligning the text, the order of the words is not changed, but any remaining white space is just added to the left instead of to the right of the line:
.navigation li a {
display: inline-block;
text-decoration: none;
float: right;
margin-right: 10px;
}
.navigation li {
display: inline-block;
}
.navigation ul {
text-align: right;
}
<div class="container-fluid">
<nav class="navigation">
<ul>
<li>About
</li>
<li>Portfolio
</li>
<li>Contact
</li>
</ul>
</nav>
This is happening because that's how float was designed. It will "float" the elements in the direction specified, in the order specified. If order is important, you can fix this in three ways:
Use the built-in Bootstrap alignment tools.
Apply float: right to the parent <ul> rather than each link.
Reverse the order of the elements.
In this case, you are floating elements to the right, in the order About > Portfolio > Contact. This is what happens:
About is floated right with no previous elements, so it sits against the farthest right wall of the parent container, <ul>.
Portfolio is floated right, but About is there already, so it gets as far right as it can, which puts it to the left of the previous element.
Contact is last to the party, so it ends up at the end of the line, farthest left.
<nav>
<div class="row">
<img src="resources/img/logo-white.png" alt="logo" class="logo">
<ul class="main-nav">
<li>Food delivery</li>
<li>How it works</li>
<li>Our cities</li>
<li>Sign up</li>
</ul>
</div>
</nav>
I have the code above for a navigation bar in an Udemy tutorial I am following.
The CSS Source Code for the navbar is the following:
.row {
max-width: 1140px;
margin: 0 auto;
}
.logo {
height: 100px;
width: auto;
margin-top: 20px;
}
.main-nav {
float: right;
list-style: none;
margin-top: 60px;
}
The part I am confused on is for the img in the div with the class="row" why is it that the image is put off to the top left even though I didn't set the float property of the img to float: left;.
As you can see in the image below, that white logo is positioned to the top left even though I never set the float property to left. But for the ul with the class="main-nav" I had to set the float property to right.
img is a inline element & ul is a block level element. that means that ul would take the 100% width and be on a new line whereas img would take its specific width. and by default the direction is ltr so we have all inline elements floating to the left.
The logo displays on the left side as the default direction for every HTML element has the "rule" LTR, so that means even if you haven't made a float: left line it will, though display at the left side.
Have a look at this, it should help you with something, happy programming!
I have a div called NAV and inside of NAV I have an UL with 5 li which I float to the left, the li's that is but when I do that the NAV collapses. I know this because I put a border around NAV to see if it collapses and it does. Here is the example.
collapsed http://img401.imageshack.us/img401/8867/collapsedze4.png
no collapsed http://img71.imageshack.us/img71/879/nocollapsedkx7.png
as you can see in the first image, the links in the NAV div are floated left and that
black border ontop is the actual div called NAV.
in this image you can see how it has top and bottom border and it not collapsed.
here is some of the html and css I used.
alt text http://img301.imageshack.us/img301/5514/codejc8.png
#nav #ulListNavi a {
float: left;
}
Add any overflow value other than visible to your container:
div#nav { overflow:auto; }
Then add width to restore the width
div#nav { width: 100%; overflow:auto; }
One solution is to add a "clear:both" style to an element after the last floated anchor, for instance:
<div id="nav">
<ul id="ulListNavi">
<li>Home</li>
<li>Search</li>
<li>Flowers</li>
<li>My Account</li>
<li>Contact Us</li>
</ul>
<div style="clear:both;"></div>
</div>
This causes the containing element to clear all floating elements before closing the containing box.
A few other options for clearing floats here:
http://www.quirksmode.org/css/clearing.html
http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/
As to the best way of doing it, that's almost a holy war, the purists would freak about the extra div, if you are not fussed by a little extra markup, the addition of the cleared div as suggested by Joshua and AJ will work fine, and is a reliable technique, but there are at least 17 other ways of doing it...
add this code after your ul:
<div style="clear: both"></div>
Try floating the containing element to the left too.
Don't bother with clearing elements or overflow. Add this:
#nav {
float: left;
}
When you float the LI's, the #nav no longer "contains" anything so it collapses. But if the #nav is floated also, it contains anything floated inside it, so it expands again.
(Also consider removing the #nav div and just applying the same styles to the UL.)
Your problem is because you are floating the <A> elements, but each of them is inside an <LI> element. LIs display as blocks by default, so each <LI> is forcing it's child <A> to begin on a new line.
If you float the <LI>s, I think you'll solve your problem.
#nav #ulListNavi li {
float: left;
}
The quickest solution would be to add overflow:hidden to clear the float on the parent element:
#nav{overflow:hidden;}
Without changing your HTML:
#nav
{
width: 100%;
overflow: auto;
border: solid 1px red;
}
#ulListNavi
{
margin: 0;
padding: 0;
list-style: none;
}
#nav #ulListNavi li
{
float: left;
}
#nav #ulListNavi li a
{
margin-left: 5px;
}
Works in IE8 and FF 3.5