Div aspect ratio and height:100% [duplicate] - html

This question already has answers here:
Setting Element Width Based on Height Via CSS
(10 answers)
Set width according to height [duplicate]
(2 answers)
Closed 2 years ago.
I'm struggling to apply an aspect ratio to a div that should grow to 100% height of it's parent.
The map div should grow to the height of the card (with is defined by the amount of text) and also maintain a squared 1:1 ratio.
I saw working solutions if the div is using 100% width (like here: https://tailwindcss.com/course/locking-images-to-a-fixed-aspect-ratio)
But it's not working when I want to have height:100% and therefore using padding-left/right:100%
This is currently my not working attempt:
<div class="row">
<div
style="position: relative; height: 100%; padding-left:100%"
>
<div
style="position: absolute; height: 100%; width: 100%;">
#map
></div>
</div>
<div class="col pt-2">
<h5>Title</h5>
<p>Subtitle</p>
<p>Subtitle</p>
<p>Subtitle</p>
<p>Subtitle</p>
</div>
</div>

If I understood well, you want to dynamically change the map box width and height based on the text size. Is so, follow this code:
<div class="wrapper">
<div class="map">
<p>Map</p>
</div>
<div class="content">
<h1>Title</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>
</div>
</div>
CSS:
.wrapper {
display: flex;
border: 1px solid gray;
}
.map {
background: gray;
}
.content {
background: white;
flex: 1;
}
jQuery script:
$( window ).on("load resize", function() {
var width = $('.map').height();
$('.map').width(width);
});

Related

How do I prevent an `<img>` from making a parent with `width: fit-content` bigger [duplicate]

This question already has answers here:
How to match width of text to width of dynamically sized image/title?
(2 answers)
Closed 1 year ago.
Please take a look at the code snippet below:
.parent {
background-color: #a7dbff;
width: fit-content;
padding: 5px;
margin: 5px;
display: inline-block;
}
.image {
width: 100%;
aspect-ratio: 1 / 1;
background-image: url(https://i.stack.imgur.com/qV078.jpg);
background-size: contain;
}
<div class="parent">
<h3>Some title</h3>
<div class="image"></div>
<div>Some more content here</div>
</div>
<div class="parent">
<h3>Some title</h3>
<img class="image" src="https://i.stack.imgur.com/qV078.jpg">
<div>Some more content here</div>
</div>
I'm trying to make the image the size of the largest element in the parent.
In the first example the image is set using background-image, this works fine. Using width: 100%, the element gets resized to the width of the parent.
But in the second example the image is an <img> element. In this case the image grows bigger than the parent, causing the parent to grow with it.
Some context: I'd like to use a <picture> element so that the ua automatically downloads the image in the correct format. The <picture> element seems to suffer from this same behaviour unfortunately. It seems like adding an <img> to the parent causes the fit-content value of the parent to grow.
What is causing this behaviour, and is there some way to fix this with css?
Note that this is similar to How to match width of text to width of dynamically sized image/title? but the solutions there don't apply here because I'm working with an <img> rather than a <div>
I added two properties to .parent. I'm not sure how the white-space will work out on all kinds of sizes but it's ok for your example. There's a subtle difference in the snippet-result; I didn't look into that.
.parent {
background-color: #a7dbff;
width: fit-content;
padding: 5px;
margin: 5px;
display: inline-block;
max-inline-size: min-content;
white-space: nowrap;
}
.image {
width: 100%;
aspect-ratio: 1 / 1;
background-image: url(https://i.stack.imgur.com/qV078.jpg);
background-size: contain;
}
<div class="parent">
<h3>Some title</h3>
<div class="image"></div>
<div>Some more content here</div>
</div>
<div class="parent">
<h3>Some title</h3>
<img class="image" src="https://i.stack.imgur.com/qV078.jpg">
<div>Some more content here</div>
</div>
Does this solve your problem?
.parent {
background-color: #a7dbff;
width:100px;
padding: 5px;
margin: 5px;
display: inline-block;
}
.image {
height:100%;
width:100%;
object-fit: cover;
}
<div class="parent">
<h3>Some title</h3>
<img class="image" src="https://i.stack.imgur.com/qV078.jpg">
<div>Some more content here</div>
</div>
You can also specify a height for the image, but then you need to create another parent div for the img and give the div a height property

Fit an image inside an auto grid [duplicate]

This question already has answers here:
How to match width of text to width of dynamically sized image/title?
(2 answers)
Closed 2 years ago.
Fiddle
<div id="grid">
<div id="toolbar">
<div>
Some writing.
</div>
<img src="some-link" alt="">
</div>
<div id="main">Main view</div>
</div>
#grid {
display:grid;
grid-template-columns: auto 1fr;
}
#toolbar {
display:flex;
flex-direction:column;
}
Is it possible to force the image to take only as much as width as the text does?
I've got a toolbar with icons that I'd like to have an image on the bottom. The icons decide the width of the toolbar (which resides inside an auto cell of a grid), and I'd like the image to fit exactly that width.
By adding width to toolbar you can achieve this.
#toolbar {
display:flex;
flex-direction:column;
background-color: rgb(225, 225, 220);
width: 99px;
}
img{
max-width: 100%;
max-height: 100%;
}
https://jsfiddle.net/3fydzgxp/2/
Other option you can try using jQuery or javascript by capturing width for text div and applied to img style.
<div id="grid">
<div id="toolbar">
<div id="text">
Some writing.
</div>
<img src="https://smallimg.pngkey.com/png/small/197-1978960_free-new-icon-download-google-chrome-icon-redesign.png" alt="" id="imagetag">
</div>
<div id="main">Main view</div>
</div>
console.log(document.getElementById("text").offsetWidth);
document.getElementById("imagetag").style.width=document.getElementById("text").offsetWidth;

How to inherit height of a sibling [duplicate]

This question already has answers here:
CSS - Equal Height Columns?
(11 answers)
How can you set the height of an outer div to always be equal to a particular inner div?
(2 answers)
Equal height rows in CSS Grid Layout
(2 answers)
Closed 2 years ago.
Let's say I have a container div with 2 elements
<div class="container" >
<div class="section" ></div>
<div class="text" >Some text</div>
</div>
My question is, how can I write the CSS so that the height of the .text div, containing all the text is as big as the .section div, so for example if
.section{
height: 450px;
}
Then height for .text is also 450px. Or do I need javascript to do this
You can give them both the same percentage height and then set the height based on the parent
.section {
height: 50%;
background: red;
}
.text {
background: green;
height:50%;
}
.container{
height:450px}
<div class="container">
<div class="section"></div>
<div class="text">Some text</div>
</div>
You should put text div inside section div so it affect changes, like this:
<div class="section" >
<div class="text" >Some text</div>
</div>
.section {
height: 450px;
background: red;
}

How to override parent containers width property in CSS [duplicate]

This question already has answers here:
Is there a way to make a child DIV's width wider than the parent DIV using CSS?
(15 answers)
Closed 7 years ago.
I am having trouble overriding the parent's width within my CSS.
Essentially, I have a parent and a child div like:
.parent{ width: 768px; background-color: red; }
.child{ background-color:blue; }
<div class="parent">
<div class="child">
//content
</div>
</div>
A lot of elements still use the parents parameter of 768px width, however I wish this one specific child element to extend the entire width of the screen - I have tried doing left: 0, right: 0, clearing the floats and setting the width to auto.
I also wanted to avoid using !important if I can.
Any suggestions ?
An accurate representation of what I want would look like this:
_____
|par. |
_|_____|_
| child |
| |
|_________|
| |
|_____|
Do this, use padding and margin (margin-left and margin-right and padding-left and padding-right) to achieve this.
<div class="parent">
<p>This is parent</p>
<div class="child">
<p>This is child</p>
</div>
<p>This is still parent</p>
</div>
.parent{ width: 468px; background-color: red; margin: 0 auto; }
.child{
background: blue;
margin-left: -300vw;
padding-left: 300vw;
margin-right: -300vw;
padding-right: 300vw;
}
http://cssdeck.com/labs/full/6xljy6pz
Try this https://jsfiddle.net/7txe5eev/. This will calculate and set margin for you. I assumed you are using bootstrap but if you get the logic you can modify this to fit your code.
HTML
<div class="container">
<div class="row">
<div class="col-xs-offset-3 col-xs-6 parent">
<div class="row">
<div class="col-xs-12 divs red"></div>
</div>
<div class="row">
<div class="col-xs-12 divs green special"></div>
</div>
<div class="row">
<div class="col-xs-12 divs blue"></div>
</div>
</div>
</div>
CSS
.divs {
height: 200px;
margin: 5px auto;
}
.red {
background-color: red;
}
.green {
background-color: green
}
.blue {
background-color: blue;
}
.special {
width: 100vw;
}
JS
$(document).ready(calcMargin);
$(window).resize(calcMargin);
function calcMargin() {
var width = $('.parent').width() - $('.special').width();
var leftMargin = width/2;
$('.special').css('margin-left', leftMargin);
}
Kind of hacky, but it works (in browsers that support calc and vw): http://jsfiddle.net/tvg2ocvs/
margin-left: calc(-50vw + (768px/2));
margin-right: calc(-50vw + (768px/2));
Doesn't look nice when viewport is smaller than 768px though, but nothing a media query won't fix :)

Centre Content in DIV vertically in bootstrap column [duplicate]

This question already has answers here:
How can I vertically center text in a dynamically height div?
(10 answers)
Closed 7 years ago.
I want to centre some text within a DIV vertically. However, I'm using Bootstrap and none of the conventional methods seem to work because it's within a column. Here's what I've got so far:
HTML:
<div class="col-sm-6">
<div class="innercontent">
<h2 class="text-center">Last Hope: The Halo Machinima</h2>
</div>
</div>
CSS:
.innercontent {
display:block
margin:auto 0;
}
The col-sm-6 doesn't have a set height and nor does the inner because they will vary on multiple uses. The CSS is what I assumed would work but doesn't.
The effect I kinda want you can see on the live dev site here: http://dev.infiniteideamedia.com/machinima/lasthope.php but it's not perfectly centred using a less than adequate method.
This is how you center anything inside div which has dynamic height
.col-sm-6 {
border: 1px solid;
min-height: 300px;
position: relative;
max-width: 600px;
}
h2 {
margin: 0;
position: absolute;
top: 50%;
transform: translate(0%,-50%);
width: 100%;
text-align: center;
}
<div class="col-sm-6">
<div class="innercontent">
<h2 class="text-center">Last Hope: The Halo Machinima</h2>
</div>
</div>
David Walsh has written a good article on centering. Have a look at it.
Run below snippet
.innercontent {
height:400px;
background:#777;
padding-top:20%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-sm-6">
<div class="innercontent">
<h2 class="text-center">Last Hope: The Halo Machinima</h2>
</div>
</div>