I have a div with a background image. This div has some other dive inside, like my navigation.
I would like to have this div have the same height of the background image.
My tries have been:
.bg {
width: 100 %;
display: inline - block;
}
.bg::after {
background: url('/images/desktop2.png') no - repeat center top;
background - size: 100 % auto;
content: "";
opacity: 0.8;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z - index: -1;
}
<div class="bg">
<div class="navigation"></div>
<div>Other contents</div>
</div>
<div class="container">
<div>First content</div>
<div>
<Second content</div>
...
</div>
In this way I'm getting the bg div to have the image height, but the container div arrives inside the div background image.
I need the bg div to have just and only the navigation and Other contents.
After this div I need the container with its contents
you may use a pseudo and padding to expand height of your container to fit the known ratio of your background-image:
example
.bg-ratio {
background: url(https://baconmockup.com/1024/576) no-repeat red/* only to check ratio height */
;
background-size: 100% auto;
overflow: hidden;
/* to see the floatting pseudo */
font-size: 5vmax;
color: white;
text-shadow:0 0 1px black;
}
.bg-ratio:before {
content: '';
float: left;
padding-top: 56.25%;
/* (576px / 1024px) x 100 */
}
body {
width: 80%;
margin: auto;
}
<div class="bg-ratio">
<header>content in here</header>
<main>
<p>play full page and resize window's width</p>
</main>
</div>
How to grow height of an element to match a ratio ?
vertical margins or paddings in percentage value will be calculated from parent's width. padding-top : 100% will be equal to parent's width. If set on a pseudo, an empty box will expand to be a square (ratio 1:1 ).
Source: https://www.w3.org/TR/CSS2/box.html#padding-properties
<length>
Specifies a fixed width.
<percentage>
The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.
Unlike margin properties, values for padding values cannot be negative. Like margin properties, percentage values for padding properties refer to the width of the generated box's containing block.
If the container span the whole screen, you may use vw units via height or min-height and drop the pseudo.
.bg-ratio {
background:
url( https://baconmockup.com/1200/100)
no-repeat
red /* only to check ratio height */;
background-size:100% auto;
height:8.33vw;/* (100px / 1200px) x 100 */
}
body {
margin:auto;
}
<header class="bg-ratio">
<div>content in here</div>
</header>
<main>play full page and resize window's width</main>
Related
Lets say I have a div with width of 100px and I have an image with UNKNOWN width.
Is there any way that I set the image as the background of the div with the following requirements:
If the image is wider than div HIDE overflow from both sides of the image EQUALLY
If the image is smaller or equal of the div make it 100% of the width of the div
A CSS background-image won't afford you this level of flexibility, but you can approximate it with an <img>:
div {
/* We'll be positioning the <img> relative to the <div>. */
position: relative;
/* Don't let an oversized background image stretch out the <div>. */
overflow: hidden;
}
div>img {
/* Since the image is an <img> element and not a background-image,
we don't have to worry about it shrinking, but we do need to
explicitly make it no smaller than the containing <div>. */
min-width: 100%;
/* Don't get in the way of positioning other elements. */
position: absolute;
/* Move the left edge of the image to the center of the <div>, then
shift it back by half the width of the <img>. Result: centered
image. */
left: 50%;
transform: translateX(-50%);
/* It's supposed to be a background image, so put it behind other
content. */
z-index: -1;
}
/* The rest is just for the sake of this example. */
div {
color: red;
border: 1px solid red;
resize: both;
}
<div>
<img src="https://i.stack.imgur.com/xXLKG.png">
Try resizing this <div>!
</div>
background image is not able scale size with container.
Making the content width : 100% of its parent will work here!
.fixSize{
width:500px;
}
.image{
width:100%
}
<!-- Now if i have a large image width:2500px -->
<div class="fixSize">
<img class= "image" src="https://static.pexels.com/photos/248797/pexels-photo-248797.jpeg" alt="Large Image">
</div>
<!-- Now if i have a small image width:256px -->
<div class="fixSize">
<img class= "image" src="https://digitalpadm.com/wp-content/uploads/2017/07/Logarithmic-Image-Gray-level-Transformation-digitalpadm.bmp" alt="">
</div>
Here's an easy fix with JavaScript
Html code
<div id="dv">
<img id="img" src="yourImageSource"/>
</div>
CSS code
#dv {
width: 100px;
height: auto;
min-height: 200px;
overflow: hidden;
border: 2px solid black;
box-sizing: border-box;
}
#img {
height: 200px;
}
JavaScript code
window.onload = ()=>{
var a = document.getElementById("dv");
var b = document.getElementById("img")
var c = a.getBoundingClientRect().width;
var d = b.getBoundingClientRect().width;
if(c < d){
b.style.marginLeft = (c - d)/2 + "px";
b.style.width = "auto";
} else {
b.style.marginLeft = "0px";
b.style.width = "100%";
}
}
Browser firstly is loading div with height 0,
and only after makes height equals image height.
Here are the screen shots : https://puu.sh/vR0Gp/10233ce94d.png
I want to make height as image height from the beginning to avoid repaints.
Here is the page: http://a4004cc1.ngrok.io/banner1.html
html of the banner:
<div class="home-top-box">
<div class="banner">
<img src="mobile-main.jpg" width="750" height="500">
</div>
</div>
css of the banner:
.home-top-box .banner{
position:relative;
height:auto;
width:100%;
display: inline-block;
}
.home-top-box .banner img{
width:100%;
}
Tried changing height to 100%, using min-height - those still didn't solve the problem.
Try changing this so that the parent has an inner padding that matches the image aspect ratio. http://i.imgur.com/2viiD35.png http://i.imgur.com/7k8uszJ.png
.home-top-box .banner {
position: relative;
height: 0;
/* width: 100%; */
/* display: inline-block; */
padding-bottom: 66.6%; /* (500 / 750) * 100 = 66.6% */
}
If you know the image aspect ratio, then you could recalculate your height using jQuery:
$.ready(function(){
$("div.banner").height($("div.banner").width()/750*500);
});
You shold take in account some padding, margins and borders, or make them zero if possible.
I am really stuck with the css for this layout. Here is my html code :
<body>
<div id="main">
<div id="left">
menu
</div>
<div id="middle">
</div>
<div id="right">
sidebar
</div>
</div>
</body>
I want three columns left, middle and right with the width of 25%, 60%, and 15% percent width of the parent and all expand to the 100 percent height of their parent (main).
in the mean time I want the main div to have a minimum height of the browser window and maximum height of its children.
You can do it easily using CSS tables:
html, body {
height: 100%;
}
body {
margin: 0;
}
.main {
display: table;
height: 100%;
width: 100%;
}
.left, .middle, .right {
display: table-cell;
background-color: lightgray;
}
.left {
width: 25%;
}
.middle {
background-color: beige;
}
.right {
width: 15%;
}
See demo at: http://jsfiddle.net/audetwebdesign/9L8wJ/
To fill the height of the view port, you first need to set height: 100% on the html and body elements.
Apply display: table to the parent container and set the height: 100%, and since this is a table, this value acts as a minimum value, so it will fill the vertical screen unless the content is very long, and in this case, the table height will automatically increase to contain the content.
Add widths to the left and right column, the middle will fill the remainder so no need to do the math.
Finally, apply display: table-cell to the three child elements.
You may want to add a wrapper around the content in each table cell is you want better control of the white space between columns, but it depends on you layout and design.
I'm trying to implement solution similar to provided here:
https://stackoverflow.com/a/12242226
The problem with it (for me) is that it does not allow to restrict height of inner div.
So I've updated solution as follows:
<style type='text/css'>
html, body {
height: 400px;
width: 100%;
margin: 0;
}
.wrapper {
display: table;
height: 400px;
width: 100%;
background: yellow;
}
.component {
display: table-row;
background: gray;
}
.content {
display: table-cell; /* height is dynamic, and will expand... */
height: 100%; /* ...as content is added (won't scroll) */
background: turquoise;
}
.contentRel {
height: 100%;
background: blue;
position: relative;
}
.contentRemaining {
background: red;
position: absolute;
top:30px;
bottom:0;
left: 0;
right: 0;
overflow: auto;
}
</style>
<body>
<div class="wrapper">
<div class="component">
<h1>Component</h1>
<p>of variable height</p>
</div>
<div class="content">
<div class='contentRel'>
<div>100% Component Header</div>
<div class='contentRemaining'>
<div style='height:1000px'>
100% Component Content
</div>
</div>
</div>
</div>
<div class="component">
<h3>Other</h3>
<p>componet of variable height</p>
</div>
</div>
</body>
http://jsfiddle.net/UrcV7/
It works as I need in FF (height of contentRel div is set to 320px - height of wrapper div minus sum of heights of component divs), but doesn't work in IE: height of (contentRel div is set to 400px - same as height of wrapper div).
Does anybody know how to fix it?
Here is my problem description (maybe it is another solution for it):
I have an outer div with height set to some px values (wrapper div in example).
In that div I have several other divs which can be hidden dynamically by some JS code.
All divs except of 1 has some height. though it is unknown to me (component divs in example).
I want that one remaining div (content div in example) to:
a. Use all remaining height of wrapper div
b. Have a header of some predefined height (100% Component Header part in example above)
c. Have a child div with height "100% of content div" - "height of header" (100% Component Content in example above)
d. To not to be taller than "height of wrapper div minus sum of heights of component divs" (scrollbars are ok)
I have div that contains 2 divs in it. One of the child divs has static height 2em, and I want the other one to vertically fill the rest of the space of the parent div. How do I do this?
Edit: I need the parent div to fill the screen.
This depends on exactly what you want to achieve. Getting a fixed top and variable bottom where the container is only as large as it needs to be for the two children.
Assuming:
<div id="parent">
<div id="top"></div>
<div id="bottom"></div>
</div>
use:
#top { height: 2em; }
and the bottom div will be as large as it needs to be. You can make the bottom fixed height and achieve the same thing.
But I suspect what you want to do is have the outer div fixed height (say 100%). That gets much harder. The problem is that there is no way in CSS of saying "height of 100% minus 2em" without using an (ill-advised) CSS expression.
One approach is to overlay the top with the bottom.
#outer { position: relative; }
#top { position: absolute; height: 2em; top: 0; left: 0; width: 100%; }
#bottm { height: 100%; padding-top: 2em; }
The top div actually overlays the bottom. This is fine so long as you don't want a border.
You can use Faux Columns if you're using an image for the background or just move the background color back to #parent to give the appearance of filling the screen with the #bottom div. It would fill the page by giving it a 100% height (as long as html and body also get height: 100%).
Example:
<head>
<title>TITLE</title>
<style type="text/css">
html, body { height: 100%; margin: 0; padding: 0; }
#parent { height: 100%; background: #f08; }
#top { height: 2em; background: #80f; }
</style>
</head>
<body>
<div id="parent">
<div id="top">TOP DIV</div>
<div id="bottom">THE REST</div>
</div>
Since CSS is just about styling, giving the appearance of 100% height is the same as having 100% height. Right?