make posistion elements stick to each other - html

I want position elements to flow under each other when resizing the window, I have a header that height increases as window get smaller.
have a submenu displayed when click on on element on the menu, the menu will appear under the main menu and it should stick to the bottom of the header when resizing the window.
I try to the problem by detecting screen size by js and change the top dynamically, but it suddenly while resizing the window, the top is different from what it should be and it becomes at the center of the window...
I make simple example here
http://jsfiddle.net/xv8hS/1/
the auto hight is not working on this version! I want the green bar to become under the items bar when resizing the window closer and larger.

This is not a positioning, but a floating issue. Use an empty <div> with clear:both in your header (demonstration):
<div id="container">
<div id="header">
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
<div class="clearfix"></div>
</div>
<div id="submenu"></div>
</div>
#container{
position:relative;
min-height:300px;
height:auto;
width:100%;
}
#header{
width:100%;
min-height:80px;
height:auto;
background-color:red;
posistion:absolute;
top:0;
right:0;
}
#header ul{
width:100%;
min-height:20px;
height:auto;
}
#header ul li{
min-height:50px;
height:auto;
display:inline-block;
float:left;
width:200px;
}
#submenu{
width:70%;
min-height:20px;
height:auto;
height:5%;
background-color:green;
posistion:absolute;
top:10%;
right:0;
}
.clearfix{clear:both;}
See also:
CSS 2.1: 9.5 Floats
All About Floats

Related

Why does a floated left div intersect with a left-fixed div?

I have a fixed div on the left of my screen and I want to put there the categories, and near it I want to put 2 more other divs, the first one to wider that the second, and both of them to be floated left, but the problem is that the 2 divs intersect with the fixed one, and I don't want that, how can I solve this ?
I've made a fiddle here:
https://jsfiddle.net/rn11jxh2/
HTML:
<div id="meniu">
<div id="cat" >
<div class="catico">
<img src="img/Categories.png"/>
</div>
<div class="cattext">
Categories:
</div>
</div>
<ul>
<li>sdncsldncsd </li>
<li>sdncsldncsd </li><li>sdncsldncsd </li><li>sdncsldncsd </li><li>sdncsldncsd </li><li>sdncsldncsd </li><li>sdncsldncsd </li>
</ul>
</div>
<div id="invitapemail">
</div>
#invitapemail {
float:left;
position:relative;
height:200px;
width:620px;
right:30px;
background-color:white;
border:2px solid #797979;
border-radius:1em;
padding:10px;
margin-top:10px;
font-family:Verdana, Geneva, sans-serif;
font-size:1.2em;
color:#797979;
z-index:2;
}
#meniu{
position:fixed;
height:420px;
width:140px;
line-height:30px;
font-size:1.15em;
margin-left:0px;
padding-right:75px;
padding-left:30px;
text-decoration:none;}
This is expected behavior. position:fixed removes the element from the document flow. That means it will ignore other elements as if they aren't even there, this it can cause overlaps. The float:left is shifting your content over to the left as if the fixed menu isn't there.
You can set a margin-left to offset the elements by the width+padding of the menu, in this case it looks like 200px: https://jsfiddle.net/rn11jxh2/1/
You can read more about positioning and floats here: https://css-tricks.com/all-about-floats/

How to make a navbar stay in one line?

I have a working navbar in the top center of my page. However, when I reduce my resolution, my navbar spreads itself into separate lines. How can I force it to stay in one line?
Here's my css:
#navbar {
display:inline-block;
width:100%;
position:absolute;
margin-left:25%;
margin-top:30px;
}
.navbar-li {
display:inline-block;
white-space:nowrap;
background-color:orange;
font-family:Arial;
margin-right:100px;
padding:20px 40px;
}
li {
font-size:20px;
}
HTML:
<div id="navbar">
<ul>
<li class="navbar-li">text1</li>
<li class="navbar-li">text2</li>
<li class="navbar-li">text3</li>
</ul>
</div>
Add white-space: nowrap; to your navbar
Set width: to the amount in pixels that you want your nav bar to always be, do not use a percent. This makes it stay the same size no matter what the screen resolution is.

Images to appear at BOTTOM of a DIV

I am trying to add two images (ul,li) at the end of a DIV. I use position ABSOLUTE, RELATIVE and left:0, bottom:0, and it does it, but it doesnt remain on the div.
The images appear in the "MainDiv", and not in "container".
The css:
#MainDiv{
background:url(../img/background.jpg) no-repeat;
background-size:cover;
width:100%;
height:600px;
}
#container{
width:980px;
height:600px;
margin:0 auto;
position:relative;
}
#list{
width:260px;
height:40px;
position:absolute;
left:0;
bottom:0;
}
#list li{
width:130px;
height:40px;
border:1px solid white;
}
The Html:
<div id="MainDiv">
<div id="container">
<ul id="list">
<li id="image1">Example1</li>
<li id="image2">Example2</li>
</ul>
</div>
</div>
The absolutely positioned element is positioned with respect to the corresponding edges of it's parent... from the look of it your container doesn't have any height set...
When you add a height to container it does move your <ul> to the bottom of the div. In fact, it is always at the bottom of the div, but because the height of the div is 0 it doesn't look like it is at the bottom of it.
http://jsfiddle.net/s5aE3/
#container{
width:980px;
margin:0 auto;
position:relative;
height: 800px;
}
The list is correctly positioned at the bottom of the container div, but there is nothing that gives the container div any height. As the height of the container div becomes zero, the list is placed with the bottom where the container div is.
To put the list inside the container div, you need to give the container div a height. Either by specifying a height style, or putting something inside it that isn't absolutely positioned or floating.
Edit:
With your updated code that has a height for the container div, the list is placed at the bottom.
Try setting the vertical align to bottom.
<style>
#MainDiv{
background:url(../img/background.jpg) no-repeat;
background-size:cover;
}
#container{
width:980px;
margin:0 auto;
position:relative;
}
#list{
width:260px;
height:40px;
position:absolute;
left:0;
bottom:0;
}
#list li{
width:130px;
height:40px;
border:1px solid white;
vertical-align:bottom;
}
</style>
<body>
<div id="MainDiv">
<div id="container">
<ul id="list">
<li id="image1>Example1</li>
<li id="image2>Example2</li>
</ul>
</div>
</div>

Menu block attached to the side of the center aligned content block

I need the content block to be center-aligned, while the menu block has to be "attached" to the left side of content block. So the distance between these blocks should remain constant while risizing of the browser window. Could you tell me how to implement this, please? :)
Here some sample pictures of what I'd like to implement:
Browser window is maximized
Browser window was made small
Browser window was made smaller, and scrollbar appeared
Whoops I missed the "constant while risizing" bit, updated example to solve problem.
This what you're looking for?
http://jsfiddle.net/r8YQc/1/
HTML:
<div id="header"></div>
<div id="content">
<div id="menu"></div>
</div>
<div id="footer"></div>
​
CSS:
html,
body{
margin:0;
padding:0;
}
#header{
margin:10px;
background-color:orange;
height:50px;
}
#content{
position:relative; /*Create new offset context*/
display:block;
width:300px; /*Define width*/
margin: 0 auto; /*center horizontally in available space*/
height:400px;
background-color:green;
}
#menu{
background-color:lightgreen;
position:absolute; /*Use left/right/top/bottom in relation to #content's offset context*/
right:310px; /*move the right edge of the element 310px left*/
width:100px;
height:200px;
}
#footer{
background-color: blue;
margin: 10px;
height: 50px;
}
P.S.
if you add a min-width of 540px (300px content width + 4 * 10px margins + 100px gutter on left and right for menu and empty space) to the body element, it won't clip the layout when resized too small.
I can't see your picture... but your description appears evident enough:
HTML
<div id="contentBlock">
<ul>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
</div>
CSS
#contentBlock {
width: 500px;
display: block;
margin: 0 auto;
}
#contentBlock ul {
/* You really don't need anything in here because it should be left aligned in the first place */
}
If you want however for text and other elements inside your contentBlock to wrap-around the menu, then I'd suggest the following CSS remedy:
#contentBlock {
width: 500px;
display: block;
margin: 0 auto;
overflow: hidden; /* This is important, it clears a heights on the contentBlock and allows the creation of floated children to be taken out of the DOM */
}
#contentBlock ul {
float: left;
/* Again... the menu's text should by default be left-aligned here */
}
1 make your website center layout ...so that can view good on every resolution since your website is not looking like a vertical design.
for making the layout in the picture you need to do
1, html
<div id="header_wrapper">
<div id="header"></div>
</div>
<div id="wrapper">
<div id="menu"></div>
<div id="content">
</div>
</div>
<div id="footer"></div>
​
2, css for the header_wrapper
#header_wrapper{
width:100%; //so you can set the background to the header
}
3, css for the header
#header{
max-width:1200px;
min-width:1000px;
}
4, now make a wrapper which will have the menu and content
css for the content
#wrapper{
margin:0 auto; //make it on the center of the page
width:1000px;
display:block;
}
5 now add the menu and content in the wrapper
css for menu
#menu{
width:100px;
height:200px;
float:left;
}
6, and now for the content
#content{
width:300px; /*Define width*/
float:left;
}

How can I make a menubar fixed on the top while scrolling

I'd like to make a menubar, which is fixed on the top of the page while scrolling. Something like the top menu in Facebook.
Also, I want a div holding the logo float at the left of menubar, and a nav float at the right of the menubar.
This should get you started
<div class="menuBar">
<img class="logo" src="logo.jpg"/>
<div class="nav">
<ul>
<li>Menu1</li>
<li>Menu 2</li>
<li>Menu 3</li>
</ul>
</div>
</div>
body{
margin-top:50px;}
.menuBar{
width:100%;
height:50px;
display:block;
position:absolute;
top:0;
left:0;
}
.logo{
float:left;
}
.nav{
float:right;
margin-right:10px;}
.nav ul li{
list-style:none;
float:left;
}
#header {
top:0;
width:100%;
position:fixed;
background-color:#FFF;
}
#content {
position:static;
margin-top:100px;
}
to set a div at position fixed you can use
position:fixed
top:0;
left:0;
width:100%;
height:50px; /* change me */
The postition:absolute; tag positions the element relative to it's immediate parent.
I noticed that even in the examples, there isn't room for scrolling, and when i tried it out, it didn't work.
Therefore, to pull off the facebook floating menu, the position:fixed; tag should be used instead. It displaces/keeps the element at the given/specified location, and the rest of the page can scroll smoothly - even with the responsive ones.
Please see CSS postion attribute documentation when you can :)