Dynamic margins using css - javascript

I'm quite new to web-design, but know most of the basic stuff, but please try to keep it relatively simple) So, I'm designing a web-site, where I want to have grey background on the sides of the main page, which I managed to do. However, I want the page to "eat up" the grey space if the window is resized, just like Marriott hotel uses I have tried looking their code up, but it's a lot and I don't think they use CSS but JavaScript for resizing dynamics. Am I right? If so, is there a way of doing it with CSS? Or java, but relatively simple, I'm very new to it!
Also, I have currently given the wrapper a width of 88% and used the 12% as the grey background. Is using % the best solution for different screen resolutions? (Keeping in mind that it would be for PCs and tablets at the most, no smartphones)

Using percentages can be useful, but if you truly want things to look how you want them to on a smaller screen resolution, you can use media queries.
Media queries work well because they can even change the styles depending on device orientation:
#media (min-width: 700px) and (orientation: landscape) { ... }
Using a media query. (Make the window bigger & smaller to see it in action.)

I think what you want is the following:
Add a background to the body.
Add a div with X px and margin auto, that will center it.
So then, when you resize the window, this will "eat" the margins from the body hidding the grey background.
If what you want is different sizes for the web depending on the width screen, then you are entering to media queries.

You are looking for:
#wrapper { width: 900px; margin: 0 auto; }
This will place your wrapper (with a 900px width) with even margins on the side and then recalculate the margins as the window changes.
I would give your wrapper a set width since setting a width of 88% means that the center content will ALWAYS be 88% of the window which means you will always have margins.

You may do it this way
here is the fiddle: http://jsfiddle.net/fcvbkv5w/
body {
text-align: center;
}
#navbar {
width: 100%;
margin-left:0%;
background-color: black;
text-align: center;
}
#fwcenter {
width: 70%;
position: relative;
text-align: center;
background-color: red;
}
#media (max-width: 700px){
#fwcenter{
width:100%;
}
}
<div id="navbar">
<center>
<div id="fwcenter">
<div id="container">
Website content
</div>
</div>
</center>
</div>

Related

Why are my HTML elements moving around depending on screen size or zoom?

My HTML and CSS elements are moving around depending on the screen size or zoom of a page. I have screenshots of this example.
This is what it ends up looking like
What it SHOULD look like
If your HTML and CSS elements change with the screen size you can either write media queries or try putting your CSS in terms of percentages rather than pixels. For instance given this CSS code:
.some-class {
width: 300px;
padding: 25px;
margin-top: 10px;
}
All of those measurements in px are going to look very different on a phone screen vs a laptop screen because 300px is about 1/2 the width of a phone screen but only about 30% of a laptop screen. Rather than specify width in px, we can say we want some-class to take up a certain percentage of the screen like this:
.some-class{
width: 30%;
}
Or like this:
.some-class{
width: 30vw;
}
For the padding and margin we can write a media query, which changes the CSS according to screen size. Assuming our original code is for a laptop and we want to make it fit on a phone screen we can add the following to our CSS stylesheet:
#media screen and (max-width: 480px;){
.some-class {
padding: 15px;
margin-top: 5px;
}
}
This overwrites the original CSS if it detects a screen smaller than 480px across. It's worth knowing how to write media queries anyway in case something that is horizontally aligned on a laptop should be vertically aligned on a phone so here's a helpful link.
https://www.w3schools.com/Css/css3_mediaqueries_ex.asp

Reduces image by width first, then after max-width, proportionally

I have a image that have 1200 x 200px. On center of this image I have a space with 500 x 200px that is the main content of full image. On each side of this image, I have an additional content. Note: it is on a single image.
If the window width is reduced, then first it should consumes the additional content of the image, but cutting it, keeping image height intact. But if I reduces the window width below of the main content width (in this time, all additional content was cutted off), then now the image should be resized proportionally, affecting the height.
My doubts:
Is possible do it only with CSS?
If not, there are some JS library to do that?
If not, how I should structure the HTML, CSS and JS to do it works?
It's an example banner with full width: Note that is have a main content and two sides with additional content.
This image below should help understand: I tried to simulate a window width resize, on 1200 px, 1000 px and 500 px (that not affect height yet) then by 350 px (that affect and resize image proportionally).
#banner {
background-image: url("http://i.stack.imgur.com/csRha.png");
width: 100%;
height: 200px;
background-repeat: no-repeat;
margin: 0 auto;
display: block;
border: 1px solid red;
}
#media all and (min-width: 1200px) {
#banner {
width: 1200px;
}
}
#media all and (max-width: 1200px) {
#banner {
background-position: 50% 0;
}
}
#media all and (max-width: 500px) {
#banner {
background-size: 240%;
}
}
<div id="banner"></div>
Is possible do it only with CSS?
Yes, and you only need 1 <div>. Treat the image as a background image, positioned dead center with background-position. Then resize the <div> using media queries, setting widths to the designated breakpoints.
You might use a simple media query like this:
#media max-width: 500px {
#your-image {
width: 100vw;
}
}
Use media queries for adding images/elements and changing css.
As for your problem use percentages for re-sizing images.
I suggest to look up progressive enhancement and Responsive design.
Also look up view-port in case you are not using it.
Don't use VW there is not enough support yet in IE,EDGE(no support for VMAX) and Opera mini, ie8 (no support at all).

How to get the browser border to lock to a div when resizing?

I'm looking at this 3 fixed-column CSS layout on http://www.vanseodesign.com. I want to use this layout however I'd like to change the way it behaves when I resize the browser.
When the browser is open nice and wide the columns are centered nicely on the page:
Then we reduce the width of the browser and it locks to the left side of the left-most column like this:
What I'd like to do is change the CSS (or javascript if necessary) so that the browser locks to the left side of the middle column instead when the browser becomes too narrow:
I'm not sure how to achieve this though?? Can anyone suggest how to change the code and most importantly why your solution works?
EDIT:
For those reading this question: I marked Salem Ouerdani's answer as the correct one because he was the first to answer with a solution that worked the particular way I wanted. However, it became clear that people were interpreting the question in slightly different ways. So it is worth reading through because there are some really great answers which might suit your situation better. Please upvote them as such.
Try to modify your container div from this :
#container { width: 960px; margin: 20px auto;}
to this :
#container {
width: 960px;
margin: 20px auto;
position: absolute;
left: 50%;
margin-left: -480px; /* half the fixed width */
}
EDIT :
Also you need to add this in order to lock to the left side of the center div whenever your browser size is beyond the fixed 960px :
#media (max-width: 960px) {
#container {
left: 0;
margin-left: -240px; /* Primary Sidebar width */
}
}
UPDATE : As #media (max-width: 480px){} did better solve the issue rather than 960px then I'm adding the related code pen example with the final solution : >> Codepen sample code
Revised Solution (based on OP comment)
Solution by #spenibus is the best and I recommend that. The solution does not work with browsers <= IE9 (IE9 only partially supports it). If you wish to make it cross browser compatible, then I suggest using javascript to manually set the scroll.
Fiddle: http://jsfiddle.net/dp7gwcn5/4/
$(function () {
$(window).resize(function () {
var width = $(window).width();
if (width <= 960) {
if(width <= 480) $(document).scrollLeft($('#content').offset().left);
else $(document).scrollLeft((960-width) / 2);
}
});
});
Old Solution
Solution #1 (Scroll: Do not hide anything, just cling)
If you wish to keep the sidebar still accessible by scrolling but only make the browser's left edge stick to the left edge of your main content #content. Then manipulate the scroll
Fiddle: http://jsfiddle.net/dp7gwcn5/. The fiddle will let you change the size of your preview window, as soon as you shrink it enough to hide the secondary sidebar the window clings to the left edge of #content
$(function(){
$(window).resize(function(){
if ($(window).width() < 720) {
$(document).scrollLeft($('#content').offset().left);
}
});
});
Note: The width 720 was given based on the widths you have set on your sidebars and conents, you can modify it to be calculated on the fly if you wish.
Solution #2 (Static: Hide primary sidebar)
If you wish to hide the primary sidebar then simply use a media query to hide the primary sidebar when your window size is small
Fiddle:http://jsfiddle.net/dp7gwcn5/1/
#media all and (max-width:720px) {
#primary {
display:none;
}
#container {
width:720px;
}
}
Solution #3 (Dynamic: Hide primary sidebar)
Solution #2 is static and there is not much you can do about it, however, if you wish to compute the widths on the fly and hide the side bar, use a script
Fiddle: http://jsfiddle.net/dp7gwcn5/2/
Script
$(function(){
$(window).resize(function(){
if ($(window).width() < 720) $('#container').addClass('small');
else $('#container').removeClass('small');
});
});
CSS
#container.small {
width:720px;
}
#container.small #primary {
display:none;
}
My Suggestion
My suggestion is to use Solution #1 if you do not wish to make it mobile compatible. If you wish to make it mobile compatible I suggest using Solution #2 with a menu button to show your primary sidebar.
Note: Solution #1 and Solution #3 will work on most browsers. Solution #2 will not work on browsers < IE9
Update 2015-08-03 01:59 +0000
Reading your comment on another answer about staying centered until the left edge of the centered column meets the browser edge made things much clearer.
This will keep the middle column centered until the browser is as large as the column, then it will stick to the left edge of the column.
html, body {
margin:0;
padding:0;
}
#media (max-width: 480px) {
#container {
margin-left:-240px;
}
}
#media (max-width: 960px) and (min-width:481px) {
#container {
margin-left:calc( (960px - 100%) / -2 );
}
}
Original anwser
Pure css. The values of "960" and "720" should be adjusted to account for body margin. Works under Firefox, no guarantees elsewhere. Using calc() for adaptive negative margin based on browser width.
/* fixed negative margin below 720px width */
#media (max-width: 720px) {
#container {
margin-left:-240px;
}
}
/* adaptive negative margin vetween 720px and 960px width */
#media (max-width: 960px) and (min-width:721px) {
#container {
margin-left:calc( (960px - 100%) * -1 );
}
}
Preview
From what I can see, this template uses the margin 0 auto in the container. The way the margin is set up, it centres the "container" which contains all three columns. This means that the content is currently centred by treating each of the three columns as one big one.
This layout is also not responsive. This means that it does not adapt to larger or smaller browser sizes. It has a fixed width and height.
A simple solution is to change the pixel units to %. This will create a fluid layout that will adapt more easily to the browser size as the measurements are based on a portion of the window size instead of having a fixed size.
For example:
body {
width: 50%; }
No matter what size the browser window is, the body will only account for half the size.
If you are looking to have all three columns visible in the exact same layout or just visible in the browser without scrolling to the side no matter what device or screen size, then you need to use media queries. Media queries are css properties that let you set specific css styles for specific resolutions or screen sizes.
For example:
#media screen and (max-width: 300px) {
body {
background-color: lightblue;
}
}
#media screen and (max-width: 600px) {
body {
background-color: purple;
}
}
In my example (although very rough), the background of the body will be purple up to a small size of 600px. It will then change to blue until a size of 300px.
You can read more about media queries here: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
1 - remove the margin from your BODY CSS.
2 - wrap all of your html in a wrapper ... all your body content
3 - Define the CSS for the wrapper:
This will hold everything together, centered on the page.
#conatiner{
margin-left:auto;
margin-right:auto;
width:960px;
}
.container {
max-width: 960px !important;
margin: 0px auto;
position: relative;
}
it will work definitely .
html, body
{
overflow: scroll !important;
ms-overflow-y: auto;
float: none !important;
overflow-y: auto;
overflow-x: visible;
}
try this in css
or in script use
function enableScrolling()
{
document.body.scroll = "yes";
document.body.overflow="hidden";
}
#media(max-width : 960px)
{
#primary{width:0;}
#content{width:66.7%}
#secondary{width:33.3%}
#container{width:100%}
}
Hope this will work for you.
You can do this easily with media queries
At the bottom of your CSS, just add something like:
#media screen and (max-width : 960px) {
#container {
margin-left: -260px;
}
}
When the window size becomes less than 960px, it will shift the container to the left by the width of the left column, leaving it fixed on the left side at the middle column
Here is a fiddle: https://jsfiddle.net/Locwv54q/
EDIT:
With the addition of CSS transitions to make things smoother you can use something like this
At the bottom of your CSS add a media query:
#media (max-width : 960px) {
#container {
left: 460px;
margin-left: -700px;
}
}
Then during the definition of container earlier in the CSS, use something like this:
#container {
position: absolute;
left: 50%;
width:960px;
margin-left: -480px;
margin-top: 20px;
margin-bottom: 20px;
-webkit-transition: margin-left 1s ease-in-out;
-moz-transition: margin-left 1s ease-in-out;
-o-transition: margin-left 1s ease-in-out;
transition: margin-left 1s ease-in-out;
}
Of course can fiddle with the values/timings to get things exactly as you want, but this general approach works on my end
updated jsfiddle: https://jsfiddle.net/8L5n58jy/
EDIT2:
This should now work as intended
#media (max-width : 480px) {
#container {
margin-left: -240px;
left: 0px;
}
}
#container {
position: absolute;
width: 960px;
left: 50%;
margin-left: -480px;
margin-top: 20px;
margin-bottom: 20px;
}
I would suggest to always center the #container with
position: absolute;
left: 50%;
transform:translateX(-50%);
Then when it will only fit the center column and 1 column (max-width: 720px) you fix the position:
#media (max-width: 720px) {
#container {
left:240px;
transition: left 2s;//make it smooth
}
}
A plus for the crazy tab resizers would be to add transition to animate the change transition: left 2s; (but thinking about 'normal' users it may not be necessary)
fiddle
You should definitely read about Twitter-Bootstrap framework, bro:
<div class="container">
<div class="row>
<div class="col-sm-3">
<h2>Left</h2>
</div>
<div class="col-sm-6">
<h2>Center</h2>
</div>
<div class="col-sm-3">
<h2>Right</h3>
</div>
</div>
</div>
would do your work, as it is RESPONSIVE. Just saying!
Why not use Bootstrap to create the columns and use the col-sm- or the col-xs- to pull the middle container into place?
Here is the example at the Bootstrap Website

How to make a snapping header that snaps to each side of the browser?

So while working on my FAQ page, I figured I would try and create a stylish header. The problem is now, that I have it, I have no idea how to snap it to the sides of the browser (if that's even plausible).
What I was wondering if either CSS or JavaScript would help with taking this image: (http://files.enjin.com/503205/FAQWelcome.png) and setting it to a 50-75% height (depending on how it looks) and snap it to the web browser so that the smaller the web browser gets, the smaller the image gets so it maintains the same look in any browser.
I was also wondering if it is possible so that a boatload of pure HTML text could follow the same pattern of expanding and collapsing perhaps?
I'm way too new to CSS but I have an intermediate understanding of how to make it work. I want to be able to create a clean, fluid website that's enjoyable to visit.
Edit: I'm using module placement for this one as Enjin won't support the edit of full-website CSS but rather HTML modules.
Here's a Fiddle in this example image is set as body background
body {
background: #f8f8f8 url(http://files.enjin.com/503205/FAQWelcome.png) top center no-repeat;
}
#media all and (max-width:960px) {
body {
background: #f8f8f8 url(http://files.enjin.com/503205/FAQWelcome.png) top center no-repeat;
background-size: auto 300px;
}
}
#media all and (max-width:600px) {
body {
background: #f8f8f8 url(http://files.enjin.com/503205/FAQWelcome.png) top center no-repeat;
background-size: auto 200px;
}
}
To get responsiveness you must use CSS media queries, with media queries you target screen size and by screen size you're setting appropriate CSS properties.

Is there a Vertically Responsive Gallery Plugin? Jquery or Javascript?

Can anyone point me in the direction of a horizontal and vertically responsive gallery slider plugin or Jscript?
I have tried Flexslider1/2, Galleria, various other plugins, but they are all a set width and don't seem to respond to resizing the browser vertically? I have tried changing all the CSS but no luck.
Any help at would be greatly appreciated.
Example (With Flexslider): If you resize the browser horizontally, the images will automatically resize to fit within the browser window. If you resize vertically this will cut off the image vertically.
Aim: When you resize the browser window vertically the image will change width and height to keep ration and fit within the browser window. Same with Horizontal.
Hopefully I have explained this clearly. If you need clarification please ask rather than down voting.
Having a poke around the code in Flexslider I couldn't find anything specifically excluding the possibility of an image resize on vertical window change. The image resize appears to be purely CSS based which then feeds the width/height inforomation into the javascript. It appears you are stuck with a DOM/CSS issue of the browser not resizing an image on a vertical window change and then a little bit of this not being a tested FlexSlider setup.
It'a always been a bit finicky to get all browsers to understand 100% vertical layouts with no vertical scrolling as it's not the normal layout paradigm. Subsequent CSS versions have helped a bit but there's still a big difference between how browsers react and interpret what you mean by 100%.
I took the slider demo and borrowed most of a stack answer to manage the 100% vertical layout and ended up with this with the detail below.
First, change your image CSS properties to scale the height of the layout and set the width auto to keep the correct aspect:
width: auto;
height: 90%;
This works on an image by itself but the FlexSlider javascript adds some extra elements into the page and also defaults some CSS width values in the demo for the horizontal layout.
.flexslider .slides img {width: 100%; display: block;}
becomes
.flexslider .slides { width: 100%; display: block;}
.img {display: block; }
Now you have a slideshow working in Chrome.
Firefox won't apply the images 100% height to the elements containing the like Chrome so I had to step back through all the elements and apply the 100% height rule in CSS
.flexslider .slides {height: 100%; width: 100%; display: block;}
.img {display: block; }
.flex-viewport img{ height: 100%;}
.flex-viewport li { height: 100%;}
.flex-viewport ul { height: 100%;}
.flex-viewport { height: 100%;}
You'll see I did the img there as well. And you end up with the page.
One draw back to this solution is you now have an image that will resize past the horizontal size of the screen. You probably need to build something in to cater for this as you will run into issues if you use the basic carousel which is dependendant on the width to work. Also something funky happens when you mouseOut of the screen adding a horizontal scroll bar but I'll leave that one for you. Also try IE at your own risk.
...and this is why I shy away from front end development =)
Sorry that post ended up being a bit of a running commentary of me poking about.
I also wanted an image slider that was vertically responsive (and horizontally). After not finding anything out-of-the-box that worked they way I wanted I started fiddlin'.
Here's the result.
Here's the key elements (go to the jsFiddle for the full demo). It's not quite perfect but should be enough to get you started.
HTML
<div id="fader">
<img src="http://placehold.it/600x600&text=Slide1" >
<img src="http://placehold.it/600x600&text=Slide2" >
<img src="http://placehold.it/600x600&text=Slide3" >
<img src="http://placehold.it/600x600&text=Slide4" >
</div>
CSS
#fader {
position: relative;
width: auto;
height: 100%;
}
#fader a {
display:block;
width:auto;
height:100%;
}
#media screen and (orientation: portrait) {
img {
width: 100%;
height:auto !important;
}
}
#media screen and (orientation: landscape) {
img {
height: 100%;
min-width:10%;
max-width:100%;
}
}
Special thanks to this jsFiddle for the lightweight jQuery rotator.

Categories