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;
Related
So I have a flex container with 6 flex items and images inside of them. I need to display them as shown in the picture below:
So I want to have flex items of different sizes in two lines, and to fit images to fill a div.
But my layout looks like this:
HTML:
<div class="pic_container">
<div class="pic_block">
<img src="img/pic_1.jpg" id="block1">
</div>
<div class="pic_block">
<img src="img/pic_2.jpg" id="block2">
</div>
<div class="pic_block" id="block3">
<img src="img/pic_3.jpg">
</div>
<div class="pic_block" id="block4">
<img src="img/pic_4.jpg">
</div>
<div class="pic_block" id="block5">
<img src="img/pic_5.jpg">
</div>
<div class="pic_block" id="block6">
<img src="img/pic_6.jpg">
</div>
</div>
CSS:
.pic_container{
display: flex;
justify-content: center;
flex-wrap: wrap;
width:100%;
height:1100px;
background-color: black;
}
.pic_block{
flex: 1 1 auto;
border: 1px solid white;
}
#block1, #block2, #block5, #block6{
width:30%;
}
#block3, #block4{
width:39%;
}
.pic_block img{
object-fit: contain;
width:500px;
}
So I tried to do as in the example, making div widths as 30% and 39%, so two divs with 30% and one div with 39% would fill the whole screen, but instead, third div just goes to the next line.
First things first, you are giving a width (30% and 39%) to your image containers, just to overwrite it with width: 500px; to your img tags themselves, change that to 100%.
Second, your block1 and block2 tags are put on the img tag instead of div so the whole thing breaks. thats why your 2 first images look so different.
Last, remove flex: 1 1 auto; from your .pic_block class.
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;
}
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);
});
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>
This question already has answers here:
How to vertically align an image inside a div
(37 answers)
Closed 8 years ago.
<div class="col-md-3 image-container">
<img class="image" src="path-to-my-image">
</div>
<div class="col-md-9 dynamic-text-container">
<p><!-- Dynamic text here --></p>
</div>
I am using bootstrap 3 in my project. How do I centre the image vertically inside the div having variable height (WITH CSS TABLE CENTERING)?
Here's a demo:
http://www.bootply.com/7rSVv7uDBu
Jsfiddle Demo
Amended CSS
.card{
display:table;
}
.image-container,
.dynamic-text-container{
display:table-cell;
vertical-align:middle;
}
.image-container {
width:25%; /* or some other value */
}
.image-container img {
display: block;
margin:0 auto;
}
Take a look at this:
HTML:
<div class="col-md-3 image-container">
<img class="image" src="image.png" height="200px;" width="200px" />
</div>
<div class="col-md-9 dynamic-text-container">
<p>Some text....</p>
</div>
CSS:
.image-container {
height: 600px;
text-align: center;
}
.image {
position: relative;
top: 50%;
margin-top: -100px; /* half of image height */
}
DEMO HERE