As simple as it must be, there doesn't appear to be anything at this site that addresses this exactly as shown.
I've tried everything I can image, without satisfaction. Likewise, I'm disappointed with all the extra code that finds its way into my page when I adapt methods designed for slightly different scenarios.
This is essentially what I want to achieve:
full-screen and resized screen responsive layouts http://q-beans.ca/header/headerSKETCH2.png
Please note in (2), the resized version, that the header background increases in height to accommodate the floated blue div. The logo is unchanged in size. Only the rightmost, blue div decreases in width after the screen gets below 660px.
In my attempts, the centered content below the header remains centered as it should. The header, however, has not been willing to cooperate. Surely there is a concise, valid way to code the header without javascript that covers what I've been missing.
Demo goes here: Fiddle Demo
HTML
<div class="page-content">
<header>
<div class="header-content">
<div class="logo">LOGO HERE</div>
<div class="content">HEADER CONTENT HERE</div>
</div>
</header>
</div>
CSS
header {width:100%;background-color:gray;padding:20px 0;}
header .header-content {width:880px;margin:0px auto;}
header .logo {float:left;width:220px;height:50px;background-color:lightgreen;}
header .content {margin-left:220px;height:50px;background-color:cyan;}
#media screen and (max-width:900px) {
header .header-content {width:auto;margin:10px auto;min-width:340px;}
header .logo {float:none;margin:10px auto;}
header .content {margin:10px;width:auto;}
}
Say What???
By floating the fixed-width logo to the left, and setting that width as the main content's fixed margin-left value, we get the header content element to be responsive, and basically take over the [headerWidth - logoWidth] leftover :-)
First off the header should have no set height. that way when the elements stack the header will expand. second, in your media query, set the blue and green element to width:100%. that way they will stack.
the width you should define in PX is your container element, the one that has margin 0 auto with an 800px width. In your media query just make that width 320px. that way green and blue elements will expand 100 percent to width 320px.
get it? got it? good
DEMO: http://jsfiddle.net/kougiland/dx38K/1/
<section>
<div>left</div>
<div>right</div>
</section>
The css
#media screen and (max-width:600px) {
body { background:red; }
}
#media screen and (max-width:340px) {
body { background:black; }
section div {display: block;
clear: both;
margin: 0 auto;
float: none!important;}
}
*{
-webkit-box-sizing:border-box;
}
section{
text-align:center;
color:white;
width:100%;
max-width:880px;
margin:0 auto;
}
section div {
float: left;
padding:20px;
}
section div:nth-child(1){
width:30%;
max-width:220px;
height:60px;
background:red;
}
section div:nth-child(2){
width:70%;
max-width:660px;
height:60px;
background:green;
}
DEMO: http://jsfiddle.net/kougiland/dx38K/1/
UPDATE: PLEASE NOTE THAT YOU HAVE TO RESIZE THE JSFIDDLE RUNVIEW TO SEE IT IN ACTION
I assume that you have done all the coding related to the page, making all the divisions and other stuff.
Now, for the header to resize itself when the browser size goes below a certain threshold(by resizing the browser or simply using ipad or iphone,etc):
In your page,
In the head section, add:(generally, a good practice for a responsive site)
<meta name="viewport" content="width=device-width, maximum-scale=1.0, minimum-scale=1.0, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="_css/screen_styles.css" /><!-- for general styling of divisions-->
<link rel="stylesheet" type="text/css" href="_css/screen_layout_large.css" /><!-- for large normal screen sizes-->
<link rel="stylesheet" type="text/css" media="only screen and (min-width:501px) and (max-width:800px)" href="_css/screen_layout_medium.css" /><!-- for devices like ipad and other tabs -->
<link rel="stylesheet" type="text/css" media="only screen and (min-width:50px) and (max-width:500px)" href="_css/screen_layout_small.css" /><!-- for devices like phones -->
First of all, lets say for your large screen, i.e. screen_layout_large.css
.header{
height: 80px;/*let's say that the height of the header and container, both are themselves 80px*/
......
}
.logo{
display:block;
float:left;
position:absolute;
width:220px;
height:80px;/* <= header height :whatever you want it to be*/
}
.content{/*the right part of the header*/
max-width:660px;
float:right;
height: 80px;
}
Then, let's say your screen size goes down to 350px,
it will automatically use the screen_layout_small.css
in which you have:
.header{
height: 135px;/*( >= (.logo height) + (.content height)) : see below*/
....
}
.logo{
display:block;
float:left;
position:absolute;
width:220px;
height:60px;/*because you might want to reduce the size of your logo for smaller screens*/
}
.content{
min-width:320px;
float:right;
height: 75px;/* same reason again*/
}
Any further help or clarification required, please leave a comment.
Ok, let me try one new thing to see if we are on the same page.
In your page, I think you already have divs like these:
<div class="header">
<div class="container">
<div class="logo">
</div>
<div class="content">
</div>
</div>
</div>
Now, whatever you want to extend in height to accomodate the floated blue div, in this case, the header for that matter.
Modify the structure of the code like this:
<div class="header">
<div class="container">
<div class="logo">
</div>
<div class="content">
</div>
<div class="clearfix">
</div>
</div>
<div class="clearfix">
</div>
</div>
Now, in your screen_styles.css or main.css(wherever you do all the styling):
.clearfix{
clear:both;
line-height:1px;
}
What it does is, the outermost page container extends down to encompass that div. Since that div appears after both of the individual divs of logo and content and itself doesn't have a float, the outermost page container is forced to increase its height to encompass that element.
Is that what you were looking for?
Related
I got two numbers of div tag, with float left and the width 50% like the following image :
What i want my div tag position on the screen change like this if the window size changed:
I know that the code must be written into media, for different screen sizes but i don't know what to do in order to change the position of these two DIV tags
<div class="wrapper">
<div class="content"></div>
<div class="sidebar"></div>
</div>
It will be like this, when the width is smaller than 796px the sidebar will be over the content but note that in the html the div.sidebar should be before the div.content
.sidebar{
background:#4F6072;
}
.content{
background:#4F9996;
}
#media only screen and (min-width: 769px) {
.sidebar{
width:50%;
float:right;
}
.content{
width:50%;
float:left;
}
}
<div class="wrapper">
<div class="sidebar">Page sidebar</div>
<div class="content">Page content</div>
</div>
You can also doing this using Bootstrap.
So, You need not to write #media query for that
You can check out link here:
https://jsfiddle.net/md93vpxj/
https://jsfiddle.net/md93vpxj/
I'm trying to create a fixed side bar with a responsive content div which has to be 732px width plus 20px margin left and right. To achieve this i've used position-fixed for both the side bar and nav-top bar. Then applied margin-left so that the content div starts after the side bar.
I'm struggling with the responsive part. I've kept the 248px margin-left in the media query section so that the content div still starts after the side-bar. I'm having difficulty working out px to %. I applied 100% to the content div, that then forces the content to go outside the wrapper by the width of the side-bar (228px + 20px gap). So I took away the width of the side-bar 248px from the 100% which has left me with a large gap of the right. I've added another 20px on the right so that there's an equal 20px left and right of the content div. However the gap still remains.
I'm not sure if its ok to use both % and px together? Where am i going wrong when calculating the space needed? Thanks in advance.
the html:
<body>
<div id="wrapper">
<div id="navbar-top">
</div>
<div id="navbar-side">
<p>side bar (228px width plus 20px gap)</p>
</div>
<div id="page-wrapper">
<p>content div</p>
</div>
</div>
</body>
the css:
#wrapper {
width:100%;
background-color:#099;
}
#navbar-top {
width:100%;
height:50px;
position:fixed;
top:0;
left:0;
background-color:#333;
}
#navbar-side {
width:228px;
min-height:100%;
background-color:#666;
position:fixed;
top:50px; /*pushes content under navbar-top*/
left:0;
}
#page-wrapper {
height:1000px;
width:732px;
background-color:#CCC;
margin-top:50px;
margin-left:248px;
}
/***********************[start of media queries]***********************************************/
#media screen and (min-width:1000px) { /*desktop queries [ >1000px ]*/
#wrapper {
background-color:#C9C;
}
#page-wrapper {
width:73.2%;
}
}
#media screen and (max-width:1000px) { /*mobile queries [ < 1000px ]*/
#page-wrapper {
max-width:732px;
}
}
It is not necessary to give the content element an explicit width.
All you need to do is to give it a top and left margin, to not be covered by your fixed elements. It is the default behaviour of block-level elements to take all horizontal space!
Generally it is a bad idea to work with absolute units like 'px', especially when it comes to responsive layouts. And also setting heights often causes "unwanted results".
But to demonstrate that it is possible, I have set up a DEMO.
width: 100%;
This is not needed for block-level elements like div!
The demo has a real gap of 20px. If you want the elements next to each other (because of the background-color/ -image), then simply set the margin-left of #content to 228px and use padding-left: 20px;.
That's it ...!
I am trying to make it so that the content box (Div) will automatically re-size when the browser width is changed.
The sidebar has a set width.
index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="Wrapper">
<div id="header">
Stuff
</div>
<div id="sidebar">
Text<br/>Sidebar
</div>
<div id="content">
Stuff<br/>text<br/>Just to fill some space
</div>
<div id="footer">
Stuff
</div>
</div>
</body>
</html>
style.css:
#wrapper{
width:90%;
}
#header{
width:100%;
height:50px;
background-color:lightblue;
}
#content{
/* *** I want something that will change width to fill blank space when the user re-sizes the browser and the sidebar moves *** */
margin-top:4px;
background-color:yellow;
}
#sidebar{
width:100px;
float:right;
margin-top:4px;
background-color:pink;
}
#footer{
width:100%;
height:40px;
margin-top:4px;
background-color:red;
}
Note that this is just code I wrote while writing this question. All I done was missed out extra code that is irrelevant to the question.
This should (Not tested) show a header at the top, and a footer at the bottom.
The part that I need help with is the middle section.
When the browser width gets changed (Smaller of bigger), the sidebar will stay to the right.
I want the "#content" box to automatically re-size so that there is a gap in between the "#content" box and the "#footer" box.
I have tried css width % values but that doesn't work.
Can css do this?
If not, can I get any php or javascript that can do that?
Fiddle : http://jsfiddle.net/logintomyk/fQPse/
HTML (that you need to ammend) :
<div id="content">
<p> <!-- add a paragrap -->
Stuff<br/>text<br/>Just to fill some space
</p>
</div>
CSS (that you need to ammend) :
#content >p {margin-right:100px;margin-top:0px}
add overflow: hidden to #content. this makes it use the "rest" of the width, browser-width minus 100px for the sidebar.
if you want to have an extra gab between the sidebar and your actual content, add the following to #content:
box-sizing: border-box;
padding-right: 20px; /* (or whatever)*/
fiddle: http://jsfiddle.net/urEbY/1/
I have a page layout as shown here...
http://jsfiddle.net/k55DE/
What I need is for the left edge of the two pink columns to always line up no matter how wide the screen is. The Sidebar container is always 300px wide,
Thanks for any help
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<div id="toolbar">
<div id="nav">NAVIGATION</div>
<div id="search">SEARCH</div>
</div>
<div id="site">
<div id="content">CONTENT</div>
<div id="sidebar">SIDEBAR</div>
</div>
</body>
</html>
-
#toolbar {
margin-bottom:10px;
overflow:auto;
}
#toolbar #nav,
#toolbar #search {
float:left;
}
#toolbar #nav {
background-color:#ddffdd;
min-height:30px;
text-align:right;
width:66%;
}
#toolbar #search {
background-color:#ffdddd;
min-height:50px;
width:34%;
}
#site {
margin:0 auto;
overflow:auto;
width:800px;
}
#site #content,
#site #sidebar {
float:left;
min-height:300px;
}
#site #content {
background-color:#ddffdd;
text-align:right;
width:70%;
}
#site #sidebar {
background-color:#ffdddd;
width:30%;
}
The easiest way to solve this is to make sure the right-side of the green areas is always lined up. In theory, this can be done by making the green areas always be the same size. Unfortunately, this is much harder than it would otherwise be because the upper green area has a percentage width (66%) and the lower green area has an explicit width (70% of 800px or ~560px). Oh, and the lower green area is centered.
Because the lower "content" area has been centered (margin-left and margin-right are auto), the margin on the left will be some fixed amount based on the size of the browser window. (Approximately (browser width - 800)/2.) Since this value is dependent on the width of your browser at the time of rendering, you cannot determine this amount in pure CSS and will need to use JavaScript to force these to line up.
The Javascript would need to do the following:
Determine the width of the left margin (browser width - 800px)/2, approximately.
Set the width of the upper green part to be margin width (as calculated in 1) + 800px. Set the upper pink part to fill the rest horizontally.
Add a listener to the window resize action and repeat 1 and 2 above every time the window is resized.
This would all be much simpler if either the bottom part was not centered or if the top part also had the same centering/sizing applied. Then you'd be able to apply a pure CSS solution.
I want to have a full header, while a fixed width of the content in the center of the page. Here's my code:
HTML:
<body>
<div class="header">
<div class="wrap">Header</div>
</div>
<div class="content">
content
</div>
</body>
CSS:
.header{
background:yellow;
}
.wrap, .content{
border:1px solid red;
margin:0 auto;
width:500px;
}
I've used .wrap inside the .header so that the content in the header also has same width as the .content.
Problem:
The layout looks fine, however the problem starts when the width of the browser window gets less than the width of the wrap (ie. 500px). In that case when we scroll the page towards the right side, some part of header background goes missing.
JSFiddle:
Here you can see the jsfiddle (http://jsfiddle.net/QS3nS/1/). You can see the problem if you decrease the browser width so that it the width of output window becomes less than 500px.
Set a min width on the header
.header{
background:yellow;
min-width: 500px;
}