I have a fluid container and I want its height to gradually change based on the width of the window (since the with of the div is 100%).
Note that the desired behavior is similar to the one when the aspect-ratio css rule is applied, which is a linear gradual increase/decrease.
Such variation should stop at a certain min and max height.
I understand that some js might be required, so I am open to it if css alone fails.
You should look into different available units of width in css. Mainly the vh and vw. In the example below you can see a div, with a height of 10% of viewport width, and width of 10% of viewport height.
div {
width: 10vh;
height: 10vw;
background-color: red;
}
<div>Custom div</div>
Related
Whatever I try I can not adjust the iframe's height on my website. I have tried height="100%" and in CSS
iframe {
height: 100%;
}
link to it and my code (inspect)
https://jandjcards.netlify.app/
Have you tried using an absolute unit like vh, vw or px? If you use percentage the width and height of the iframe will depend on the parent element. However, using other relative units will allow you to change the width and height of the element properly.
Absolute measurement units:
VH (Relative to 1% of the height of the viewport)
VW (Relative to 1% of the width of the viewport)
PX (Pixels)
REM (Relative to font-size of the root element)
First solution
Try including this:
iframe{
width: 20vw;
/* Use whichever unit you want except percentage */
}
If you want more informaton about absolute and relative css units I recommend you this website:
https://www.w3schools.com/CSSref/css_units.asp
Second solution
If you still want to use percentage, you will need to resize the parent element of the iframe. Basically all the elements whose display is "block" will expand itself horizontally all it cans, but will set its height to whatever it has inside. So if you use percentage to resize an iframe it will adopt the height of the parent element and that's not the idea.
Try resizing the parent element like this:
#container{
height: 10vw;
}
iframe{
height: 80%;
}
I hope this helps!
I would like to change the width of some element according to the height of the page. if height gets smaller, the width of those elements gets larger!
You can use viewport-percentage lengths, specifically the vh (viewport height) unit to scale any property of an element based on the height of the window.
For example, this element will have its width at 75% of the browser's height:
.scale {
width: 75vh;
}
To do the inverse of this as you stated ("if height gets smaller, the width of those elements gets larger"), you could use calc like so:
.scale {
width: calc(100% - 25vh);
}
I can do this in JavaScript easily enough, but would like to know if it's possible with straight CSS: keep a square div in a window that fits within the window size regardless of what that is.
Any solutions I've found do not account for the height becoming less than the width.
Logically speaking what I want is, when the window width is less than the height, give me a square of that width. If the height is less, then give me a square of that size.
The closest solution I've seen uses a width and height measured in vw, but it does not work when the window is very wide and short.
I suggest using the vmin unit.
Source
From the viewport-percentage lengths documentation on MDN:
Viewport-percentage lengths define the value relative to the size of the viewport, i.e., the visible portion of the document. Viewport lengths are invalid in #page declaration blocks.
vmin
Equal to the smaller of vw and vh.
Example
Use the full page link to test it with the example in your browser.
body {
/* So the whole viewport can be used by the square. */
margin: 0;
}
#sqr {
/* Uses the 'outer' (i.e. border-box) size when setting width and height. */
box-sizing: border-box;
width: 100vmin;
height: 100vmin;
background-color: red;
border: yellow 10px solid;
}
<div id="sqr"><div>
I am learning CSS, I am new to this field. Please excuse me if my questions is naive.
I have a simple HTML containing 1 div.
.square {
background-color: blue;
width: 90vw;
height: 90vh;
margin-left: 5vw;
margin-right: 5vw;
}
<!doctype html>
<head>
<link href="viewport-cord.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class='square'></div>
</body>
I am expecting a margin of 5% of viewport width on both sides of the div. However, it seems that we have broader gap in the left side of div compared to right. I am not sure where these extra margin is coming from.
You can do this:
Give max with and then subtract 10% (5% each side), then center square with margin auto.
CSS
.square {
background-color: blue;
width: calc(100vw - 10%);
height: 100vh;
margin: 0 auto;
}
DEMO HERE
Since you said you are new to HTML & CSS, let me give you a breif description on how to adjest widht of an element based on the viewport.
Viewport
The viewport is the area of your browser where the actual content is displayed - in other words your web browser without its toolbars and buttons. The units are vw, vh, vmin and vmax. They all represent a percentage of the browser (viewport) dimensions and scale accordingly on window resize.
Lets say we have a viewport of 1000px (width) by 800px (height):
vw - Represents 1% of the viewport's width. In our case 50vw = 500px.
vh - A percentage of the window's height. 50vh = 400px.
vmin - A percentage of the minimum of the two.
In our example 50vmin = 400px
since we are in landscape mode. vmax - A percentage of the bigger
dimension. 50vmax = 500px.
You can use these units anywhere that you can specify a value in pixels, like in width, height, margin, font-size and more. They will be recalculated by the browser on window resize or device rotation.
case 1
Now considering your issue, I don't see any change in width on either side, because if you see your inspector->layout->margin I can see both sides showing 64 on the screenshot you provided.
case 2
If you really do see the change in your system again. Try removing the external CSS you have mentioned to see if that's causing any issue and then put margin:0px for html and body.
Hope this help's you out...
recently I found an responsive website which changes the image contents in different size of screen. When the screen size is big like desktop computer, the content of the div is like(there is no other text content, just a div filled with an image using background-image):
#div {
background-image: url('images/pc-content01.jpg');
background: no-repeat center center;
height: 1129px;
}
When the screen size gets smaller, the css style changes like:
#div {
background-image: url('images/pc-content01.jpg');
background-size: cover;
height: 0;
padding-bottom: 95.5%;
}
And the background image will be swap to another image when the screen size is as small as moblie devices.
And my question is, how the percentage of padding-bottom is calculated, why percentage in height is not working but percentage on padding-bottom works?
(I understand why percentage on height is not working).
In padding percentages refer to the width of the containing block. In this case is used to maintain the aspect ratio (the image one) when the width changes. It is a trick often used in responsive design. A box with an intrinsic ratio. Percentage in height works differently
The percentage is calculated with respect to the height of the
generated box's containing block...
MDN, so is not suitable for that purpose.
When using a percentage value for paddings, it always refers to the width of the element. See MDN. So in this case the padding-bottom of #div would be 95.5% of its width. When setting percentage value for height it calculates it by using the height of the containing block. See MDN
Height in percentage
Height using percentage only works if we give height using percentage to the body and html of the page, it will not work otherwise.
Like this-
html, body{
height:100%;
background:black;
}
body>div{
height:50%;
background:gray;
}
<body>
<div>HI</div>
</body>
Padding-bottom in percentage
But in the case of percentage on padding-bottom, it works irrespective to the body or HTML. It only checks the width of the containing element.
Like this -
html, body{
background:black;
}
div{
background:gray;
padding-bottom:20%;
}
<body>
<div>HI</div>
</body>