Does anyone know how to make a DIV behave in such a way that it will always try to consume it's maximum space (Regardless of content)?
For example, if I have a DIV with a max-height of 600px and a min-height of 200px, how can I make it so that:
If the window height is greater than 600px enough it will occupy 600px
If the window height is between 600px and 200px it will occupy all available height
If the window is 200px or less it will always occupy 200px?
An alternative to the above that I could live with would be to ignore the minimum height and just let it scale all the way down.
Eventually, I would like to vertically align it within the window but first things first.
I'd prefer to use pure CSS for this if possible. If it comes to it, I won't have too much trouble writing a script to achieve it.
Any suggestions welcome.
Cheers.
Using only the height property you may obtain this behaviour using a set of mediaqueries
div {
height: 200px;
}
/* needed to stretch the height of div */
#media all and (min-height: 201px) and (max-height: 600px) {
html, body {
height: 100%;
}
}
#media all and (min-height: 201px) {
div {
height: 100%;
}
}
#media all and (min-height: 601px) {
div {
height: 600px;
/* if you need to vertical-align the div, use following rules */
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}
}
Live Example (basic): http://codepen.io/anon/pen/NPGbNq
Live Example (with vertical alignment > 600px) http://codepen.io/anon/pen/yyYVag
You can do this in a very simple way without media queries by combining height, max-height and min-height. The browser mediates between them.
div {
height: 100%;
max-height: 600px;
min-height: 200px;
}
See example.
EDIT - VERTICAL CENTRING
To include vertical centring, there's also a smart way without media queries or CSS transforms. This solution should therefore work in IE8.
div {
height: 100%;
max-height: 600px;
min-height: 200px;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
See example.
Vertical alignment using this smart technique.
see this .it will take 100% width and 100% height at any width and height of windows.
div{
display: block;
background: red;
height: 100%;
width: 100%;
position: fixed;
}
click here
Related
I have been attempting to make some SVG data charts responsive but seem to be unable to do so with the current CSS 'position:fixed' applied to the elements.
I'm looking for, if possible, a solution that doesn't rely on media queries as I have multiple elements that I would need to apply this to. If this isn't possible, then any suggestions on what to do in order to keep all the data matched up with the SVG as the browser is resized, would be great!
Ideally I would like the SVG to scale up and down in size, whilst remaining central, no matter what size the browser is.
This is one of the SVGs that i'm looking to make responsive (right hand side)
http://datahealthcheck.databarracks.com/2016/#backup-section-3
I've created a codepen and added just one percentage on the SVG as an example of the problem http://codepen.io/anon/pen/YGvXkq
<div id="Backup-3"></div>
<p id="percentage" class="backup3-percentage">3%</p>
#Backup-3 {
position: fixed;
width: 550px;
margin-left: 73px;
margin-top: 31px;
}
.backup3-percentage {
position: fixed;
color: #000;
margin-left: 478px;
margin-top: 96px;
font-size: 1em;
transform: rotate(6deg);
}
I'd go with viewport units
.responsive-div {
position: fixed;
width: 70vw; // vw being viewport-width, so 70% of the width of the viewport
height: 50vh; // vh being viewport-height, so 50% of the height of the viewport
}
This article is going more in-depth about it
body{ margin:0; padding:0;
}
.mydiv {
max-width:1800px;
width:100%;
position:fixed; background:red; height:100px; border:5px solid green; box-sizing: border-box;}
<div class="mydiv">
</div>
I think media queries would be the best approach for make the div responsive.
If not you can use with:100% and max-width to your position fixed div
max-width dont work with position: fixed. This my alternative:
`.nav{
width: 1280px;
position: fixed;
}
#media all and (max-width: 1280px) {
.nav{
width: 100%;
}
}`
if you used width without #media 100% your block
I am building an app in Ionic where I am trying to locate four row elements and a button inside a div. The div has an height: 100% that covers the whole screen, and the content has to stretch to its full-height.
I have tried the following solution, but apparently it is not working:
ion-content {
position: relative;
height: auto;
width: 100%;
}
.boxes-container {
position: absolute;
height: 100%;
width: 100%;
}
This is the complete code. Do you have an idea of what is a possible way to solve it?
Thanks in advance for your replies!
You can use a combination of the calc function and viewport units to achieve this layout.
height: 100vh will give an element a height equal to that of the viewport.
You have a header element that is 44px in height. Each row has a vertical margin. You can deduct these from 100vh using the calc function:
.boxes-container {
height: calc(100vh - 44px - 50px);
}
This will give your element a height equal to that of the viewport minus the height of the header element.
You then need to give your four rows and the button a height of 20% so that they occupy all of the available vertical space in the container.
.row {
...
height: 20%;
}
.button {
height: 20%;
}
Updated live demo
calc() is available in all major browsers and IE>8 (caniuse.com)
An alternative would be to give both .scroll and .boxes-container a height of 100% and .row a height of 20%:
.scroll {
height: 100%;
}
.boxes-container {
height: 100%;
}
.row {
...
height: 20%;
}
Updated live demo
I'm trying to solve the problem showed on the picture.
I have a site of a width of 980px (margin:auto) centered in the middle of the page and I need surrounding background of a width of 400px each side. But when user narrows the width of the browser the background shouldn't affect horizontal bar (only the width of the page itself 980px)
Plus there's an image which is placed 80% in the main page and the rest outside of the page. I also want this piece of the image (20%) not to affect the horizontal bar when user narrows the width of the window.
THANKS!!!
image:
http://tinypic.com/r/ri58io/8
code: link to fiddlehttps://jsfiddle.net/c0ro66s4/
The thing with this design is that the 'background' boxes have a fix width. When the fill the rest of remaining width (next to the content) no scrollbar appears (at least, not in Firefox).
So what I've did is made a media query. When the screen size is bigger than (980+400+400=) 1780px the boxes will have their normal width. As soon as the screensize becomes under the 1780px, the width will be set to auto and we use the 'left' position, which makes them fill the screen and won't show the scrollbars.
I made the testcase in JSFiddle with half the sizes (otherwise it wouldn't fit on my screen).
<div id="content">Content</div>
<div id="bgLeft"> </div>
<div id="bgRight"> </div>
body, html { margin: 0; padding: 0; min-height: 100%; }
#content {
background: lightblue;
position: absolute;
width: 490px;
height: 100%;
left: 50%;
margin-left: -245px;
}
#bgLeft {
background: lightgreen;
position: absolute;
width: 200px;
right: 50%;
margin-right: 245px;
height: 100%;
}
#bgRight {
background: lightgreen;
position: absolute;
width: 200px;
left: 50%;
margin-left: 245px;
height: 100%;
}
#media screen and ( max-width: 890px ) {
#bgLeft {
width: auto;
left: 0;
}
#bgRight {
width: auto;
right: 0;
}
}
Set the image as background in both boxes and a align the one in the left box on the right and the one in the right box on the left.
DEMO
EDIT
Added your background-images: DEMO 2
I am developing a responsive website which has a large banner image which spans edge to edge. The problem is that its supposed to be a static height regardless of the screen size. So when I set the image to width:100% it looks great in full size, but as soon as I start to shrink the screen the image gets shorter then the width that it needs to be. How can I set this up so that the image height appears to never change and when the screen size is smaller then the image size it zooms in and crops the edges?
http://bit.ly/1qxmZir
HTML
<div id="hd-img">
<img src="http://72.52.242.20/~camacoll/wp-content/uploads/2014/05/inner-hd.jpg">
</div>
CSS
#hd-img {
max-height: 352px;
}
img {
width: 100%;
height: auto;
left: 100%;
margin-left: -200%;
position: relative;
}
One way you could do it is like this.
/* CSS */
#hd-img {
height: auto;
overflow: hidden;
}
#hd-img img {
/* Optional max width: 600px; */
display: block;
margin: 0 auto;
}
Try this:
background-image:url(../images/header-bannerbg.png);background-position:fixed;background-repeat:no-repeat;height:580px;-webkit-background-size: 100% 100%;-moz-background-size: 100% 100%;-o-background-size: 100% 100%;background-size: 100% 100%;
I have found this vertical centring method which seems pretty common..
#container {
width: 960px;
height: 740px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -480px;
margin-top: -370px;
}
What I'm trying to center here is the entire site, and this code goes perfectly as expected when the screen preview is larger than the div height (larger than 740px). However, Once the browser window is minimized less than div's vertical size (740px) parts of the header disappear above the top of the page.
I can sort of understand why this is happening seeing that 50% becomes less than half the div's size which will be equalized with margin-top.
What I'm looking for is a fix for this issue? Or even a completely different method, I just need to center the site both vertically and horizontally.
try this:
#container {
height: 740px;
width: 960px;
position: absolute;
margin: auto;
top: 0; right: 0; bottom: 0; left: 0;
}
By the way, Smashing Magazine recently published a nice article about this.
You need to add a media query:
#media screen and (min-height:740px) {
#container {
top:0;
margin-top:0;
}
}
This will only apply the formatting where the screen is at least 740px tall. If you want to learn more about media queries, check http://www.w3.org/TR/css3-mediaqueries/
Absolute Centering like Lino Rosa mentioned is the best approach here for easy horizontal and vertical centering while allowing you to add some responsive touches, like fixing your height issue.
Ideally, you should be using percentages for the width and height declarations so that your content will vary with the viewport. Of course, sometimes you just need pixels :-)
Here's what I've done:
.Absolute-Center {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
margin: auto;
}
#container {
width: 960px;
max-width: 90%;
height: 740px;
max-height: 90%;
overflow: auto;
}
By setting a max-height and max-width, the box will never be more than 90% of the container (in this case, the browser viewport) even if it's less than 960px wide or 740px tall, so even small screens see a nice centered box. overflow: auto ensures that if the content is longer than the box, the user can scroll in the box to see the rest.
View the demo
If you must have the box exactly 960px by 740px no matter the screen size (forcing the user to scroll around to see all of the content on a small window), then only apply the Absolute Centering styles to #container using a media query, like so:
#container {
width: 960px;
height: 740px;
overflow: auto;
margin: auto;
}
#media screen and (min-height:740px) and (min-width: 960px) {
#container {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
}
}
View the demo
I encountered the same issue. As the height of my element is dynamically changed, I can't give it a fixed height.
Here is a demo below, hope it helps.
.wrapper {
display: table;
height: 100vh;
width: 100%;
}
#container {
display: table-cell;
vertical-align: middle;
background-color: lightblue;
}
.content {
width: 30%;
height: 30%;
background-color: red;
}
<html>
</html>
<body>
<div class="wrapper">
<div id="container">
<div class="content">
</div>
</div>
</div>
</body>