I am trying to change my old website menu to be mobile friendly and have a problem how the "menu elements" are displayed on small screen.
The menu consists of 3 elements (left, middle, right), now on small screen, they go vertically in same order.
But i would like to know if its possible with css to have them position like that, where the middle keeps its position and the left and right are moved under it, or does it need some more tricks.
How to achieve it?
http://img834.imageshack.us/img834/2203/menuse.jpg
EDIT, Added simplified code what i used to play around:
<div id="header">
<div id="Bholder">
<div id="AButton"></div>
<div id="HButton"></div>
<div id="CButton"></div>
</div>
</div>
CSS
#Bholder{
margin-left: auto;
margin-right: auto;
max-width: 750px;
}
#AButton{
float: left;
width: 250px;
height:100px;
background-color: red;
}
#CButton{
float: left;
width: 250px;
height:100px;
background-color: green;
}
#HButton{
float: left;
width: 250px;
height:100px;
background-color: yellow;
}
#media only screen and (max-width: 767px){
#Bholder{
margin-left: auto;
margin-right: auto;
max-width: 250px;
}
}
You could maybe try floating the left and right element to the right, and the middle element to the left. Then make the width of the wrapper element the same size as each element to get them to 'jig' about visually.
But yea, code would be helpful to see how you have the menu set up.
[EDIT]
#media only screen and (max-width: 767px){
#Bholder{
margin-left: auto;
margin-right: auto;
max-width: 250px;
position: relative;
}
#AButton {
position: absolute;
top: 100px;
left: 0;
}
#HButton {
position: absolute;
top: 0px;
left: 0px;
}
#CButton {
position: absolute;
top: 200px;
left: 0;
}
}
A note: I'd stick that in an <ul> with <li> instead of div's. And I'd use classes instead of #id's for your css.
Related
I really want to know how to achieve this kind of structure using HTML CSS including media queries. See image below:
On smaller screens section 1 will be on top overlapping little portion of the picture contained by section 2.
You must see the picture above to understand my question. The main problem is I really want to do something like this but I don't know where start. I don't want to break any CSS structure using negative margins and padding.
Is this what you are after? Resize the browser window (make it smaller) to see the effect.
HTML:
<div id="section1">
</div>
<div id="section2">
<div id="overlay">
Overlay
</div>
</div>
CSS:
#section1 {
float: left;
width: 50%;
height: 200px;
background: grey;
}
#section2 {
width: 50%;
height: 200px;
background: black;
float: right;
position: relative;
}
#overlay {
position: absolute;
top: 90px;
left: -20px;
background: red;
}
#media screen and (max-width: 480px) {
#section1, #section2 {
float: none;
}
#overlay {
position: absolute;
top: -10px;
left: 90px;
background: red;
}
}
JSFiddle
Basically I have 3 floating elements, they are next to each other and are scaled down when you resize the browser window.
Now under a certain width (#media (max-width: 800px)) the elements get a fixed width and a clear:both so that they are below eachother and no longer scale.
What I'm trying to do now is centering those 3 divs inside their parent.
margin:0 auto is not helping
Above 800pixel width:
Below 800pixel width:
--> Div should be centered inside the yellow one
Code I have for the media query:
#media (max-width: 800px){
.data .data_column{width:215px;background-color:red;clear:both; margin: 0 auto;}
.data{width:90%;background-color:yellow;}
}
Replacing your clear:both with a float: none should be enough to let margin: auto do the work.
Try following:
<div class="data">
<div class="data-column"></div>
</div>
And CSS:
.data {
position: relative;
width:90%;
height: 250px;
background-color:yellow;
}
.data-column {
clear:both;
position: absolute;
height: 150px;
width: 215px;
/* horizontal centering */
right: 0;
margin-right: auto;
left: 0;
margin-left: auto;
/* vertically centering */
top: 0;
margin-top: auto;
bottom: 0;
margin-bottom: auto;
background-color: red;
}
Here is a FIDDLE
I am trying to create 2 side banners (left and right) with fixed positioning, and a centered container for the content.
The problem is that when minimizing the screen, the 2 side banners cover the centered container. I need a CSS solution to set the minimum width of the view to 860px; after which, the window becomes scrollable and divs do not overlap. The perfect solution is:
The HTML I am using is as such:
<div class="left" style="position:fixed; height:100%; background-color:#7fb4dd; top:43px; left:0px; width:180px;">
</div>
<div class="center" style="margin:100px 180px 0 180px;">
<div style="width:100%;">
<div style="width:500px; margin:0 auto;">
</div>
</div>
</div>
<div class="right" style="position:fixed; height:100%; background-color:#7fb4dd; top:43px; right:0px; width:180px;">
</div>
The above code prevents the left bar from overlapping the center container; but the problem is still present with the right bar.
This is a fiddle of the code: preview
You need to wrap the three DIVs in a wrapping DIV and set the min-width to prevent the overlap. This prevents it from getting narrower than the three columns. Add up the widths, set that as the minimum.
Here is a pure HTML/CSS solution for you , tell me if it is not exactly what you needed.
<html>
<head>
<style type="text/css">
body{
margin:0;
padding:0;
line-height: 1.5em;
}
b{font-size: 110%;}
em{color: red;}
#topsection{
background: #EAEAEA;
height: 90px; /*Height of top section*/
}
#topsection h1{
margin: 0;
padding-top: 15px;
}
#contentwrapper{
float: left;
width: 100%;
}
#contentcolumn{
margin: 0 200px 0 230px; /*Margins for content column. Should be "0 RightColumnWidth 0 LeftColumnWidth*/
background-color : red;
width : 400px;
margin-left : auto;
margin-right : auto;
}
#leftcolumn{
float: left;
width: 200px; /*Width of left column*/
margin-left: -100%;
background: #C8FC98;
}
#rightcolumn{
float: left;
width: 200px; /*Width of right column*/
margin-left: -200px; /*Set left marginto -(RightColumnWidth)*/
background: #FDE95E;
}
#footer{
clear: left;
width: 100%;
background: black;
color: #FFF;
text-align: center;
padding: 4px 0;
}
.innertube{
margin: 10px; /*Margins for inner DIV inside each column (to provide padding)*/
margin-top: 0;
height : 700px;
}
.innertubetop{
margin: 10px; /*Margins for inner DIV inside each column (to provide padding)*/
margin-top: 0;
}
</style>
</head>
<body>
<div id="maincontainer" style = "min-width : 800px;"> <!-- this will be sum of width of all three columns-->
<div id="topsection"><div class="innertubetop"><h1>Hello iam navigation bar</h1></div></div>
<div id="contentwrapper">
<div id="contentcolumn">
<div class="innertube"><b>Center Column </b></div>
</div>
</div>
<div id="leftcolumn">
<div class="innertube"><b>Left Column: <em>200px</em></b></div>
</div>
<div id="rightcolumn">
<div class="innertube"><b>Right Column: <em>200px</em></b></div>
</div>
</div>
</body>
</html>
The problem you are in is because of position: fixed; since that object is taken out of the workflow the other objects can't push it away. I was able to get a nice and fully responsive layout to work. (Let me know how it is)
Fixed positioned elements are removed from the normal flow. The
document and other elements behave like the fixed positioned element
does not exist.
Fixed positioned elements can overlap other elements.
Updated answer to better suit his needs (JSFIDDLE, remove the show, in the url, to see code)
Okay what I am doing here is using css media queries to change the layout.
Here is the html,
<div class="wrap">
<nav></nav>
<div class="content"></div>
<section class="lSide"></section>
<section class="rSide"></section>
</div>
Now the media query,
#media only screen and (max-width: 680px) {
.content {
width: 90%;
margin-bottom: 10px;
}
.lSide, .rSide {
position: relative;
width: 90%;
height: 100px;
margin: 10px auto;
bottom: 0;
}
}
Don't forget to add this to your head on your html file,
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0;">
OLD answer
The CSS, (JSFIDDLE, remove the show to see code)
html, body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
background: tan;
}
.wrap.active {
min-width: 750px;
}
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 20%;
background: brown;
z-index: 101;
}
.lSide {
background: #3b3b3b;
position: fixed;
left: 0;
top: 20%;
width: 200px;
height: 80%;
}
.content {
width: 300px;
height: 600px;
background: #c1c1c1;
margin: 0 auto;
position: relative;
z-index: 100;
top: 20%;
}
.rSide {
background: #3b3b3b;
position: fixed;
right: 0;
top: 20%;
width: 200px;
height: 80%;
}
.rSide.active {
display: none;
}
The JS, (updated)
$(window).resize(function() {
if ($(window).width() < '750') {
$('.wrap, .rSide').addClass('active');
}
else {
$('.wrap, .rSide').removeClass('active');
}
});
One solution I have, refer to fiddle next to css, is to remove the right side when a screen size is to small.
I have a problem with my site's logo. It sits in the upper left hand corner. On a lower resolutions, it looks fine. However, at larger resolutions, you can see the logo shift off to the right. How can I adjust my css to make this stationary, just to the left of the "Home" link?
<div id="header">
<div class="headerright">
<img id="logo" src="img/logo.png" alt="" />
<!-- snip -->
</div>
</div>
.headerright {
left: 140px;
position: absolute;
width: 966px;
height: 100px;
}
This should solve it!
Change .headerright to this:
.headerright {
margin-left: auto;
margin-right: auto;
width: 966px;
height: 100px;
}
and #nav to this:
#nav {
display: block;
position: absolute;
top: 10px;
left: 30px;
height: 79px;
width: 906px;
}
The container element, headerright, is declared with an absolute position and a left value of 140 pixels.
.headerright {
left: 140px;
position: absolute;
}
When you resize the window most elements will adjust to a new position except for the headerright layer (or other absolutely position elements). The headerright, which contains the logo, will always be 140 pixels to the left, causing it to be misaligned on smaller screens.
You need to centre the div. This will allow it to adjust its position as the window resizes. Remove position: absolute; left: 140px;.
.headerright {
margin: 0 auto;
width: 966px;
height: 100px;
}
Change .headerright to position: relative; and add margin: 0 auto;
See below:
.headerright {
position: relative;
margin: 0 auto;
}
Then if you want to move the logo a little bit to the left, add this to #logo:
#logo {
margin-left: -60px;
}
Without relying on tables, what would be a good solution to accomplish a tile layout such as this: that automatically adapts to the screen size of the user. That is, the whole screen should be filled by the tiles, regardless of the with and height of the resolution.
I appreciate any ideas.
~ Robert
Here is a working example:
jsfiddle
Html:
<div class="container">
<div class="first">first</div><div class="third">third</div>
<div class="second">second</div><div class="fourth">fourth</div><div class="last">last</div>
</div>
CSS:
html, body, .container
{
height: 100%;
min-height: 100%;
}
.first {
float: left;
width: 20%;
height: 30%;
background-color: red;
}
.second{
float: left;
width: 20%;
height: 70%;
background-color: green;
}
.third{
float: right;
width: 80%;
height: 80%;
background-color: blue;
}
.fourth {
float: right;
width: 40%;
height: 20%;
background-color: #DDDDDD;
}
.last{
float: right;
width: 40%;
height: 20%;
background-color: yellow;
}
I would go with some div using absolute positionning. And specify for each tile the width/height/top/left using % unit.
Hint:
Use a "content div" with 100% of width and height
Use into the "content div" two divs: one for the left column and one for the right one. Remember to give those "%" dimension (to "content" div also)
Remember that a floated right div have to come BEFORE a left floated div
With this three point, you should be able to try yourself.
Disclaimer
This will not mean the needs of your project. That has been answered alread by other uses.
For future reference, I would look into an oocss-approach to layout classes. You may have pages that have a different amount of tiles, etc. I use the following for my projects.
Tiles Object
Used for creating tile layouts.
css
.tiles
{
display: block;
}
.tiles__item
{
display: block;
height: auto;
float:left;
}
.tiles--2
{
margin-left: -4%;
}
.tiles--3
{
margin-left: -2%;
}
.tiles--4
{
margin-left: -2%;
}
.tiles--2 .tiles__item
{
margin-left: 4%;
width: 46%;
}
.tiles--3 .tiles__item
{
margin-left: 2%;
width: 31.3%;
}
.tiles--4 .tiles__item
{
margin-left: 2%;
width: 23%;
}
html
<div class="tiles tiles--2">
<div class="tiles__item">
</div>
<div class="tiles__item">
</div>
</div>
Dock Object
Docks content to the edge of the screen.
css
.dock
{
position: absolute;
height: auto;
width: auto;
}
.dock--t
{
width: 100%;
top: 0;
}
.dock--b
{
width: 100%;
bottom: 0;
}
.dock--l
{
height: 100%;
left: 0;
}
.dock--r
{
height: 100%;
right: 0;
}
html
<div class="dock dock--t">
The content will be docked to the top of the screen.
</div?
Other Ones
I would look at the Tiles object in the Metro UI Framework. They allow for heights
http://metroui.org.ua/
If you are looking for a good layout system that uses proportions:
https://github.com/stubbornella/oocss/wiki/Grids