Sizing div within flexbox - html

I'm trying to size a div according to the page and column size. The div must always be a square, regardless of its width. What I mean by that is:
When it's set in a row, it takes up the amount of space it's allocated. In this case, the text took 50% of the space. The remaining 50% must be taken up by the square div.
When it's set to a column (stacked on smaller screens), it takes up the amount of space it's allocated. In this case, the square div is the full width of the screen.
div.layout-1-img{
flex: 1;
background-color: gray;
width: 5em;
height:5em;
}
Jsfiddle

From what I understand:
you have a div that will contain the text you want, but this div width and height change.
You want the outerdiv to always be a SQUARE regardless of the page width, but still have the text div inside.
Possible Solution:
Create an outer div:
.outerDiv{
display: block;
position: absolute;
background-color: gray;
padding: 10px; // Specify the padding value if you want
}
Then using jquery:
$(document).ready(function(){
//Get the text div height and width
var height = $('#textdiv').height();
var width = $('#textdiv').width();
// increase the size of height or width by
// How much taller you want the back to be compared to text div.
//In this example I'm increasing the background by a factor of 2
height = height *2;
width = width *2;
//Compare to see which one is largest.
// Then change css of background to correspond to the correct size
if(height>width){
$('.outerDiv').height(height);
$('.outerDiv').width(height);
}else{
$('.outerDiv').height(width);
$('.outerDiv').width(width);
}
});

Related

Img element to fill container width unless maximum height is reached using pure CSS

I am trying to use CSS to set the size of an image. I want the image to fill the width of the container, so width: 100% would do it (even if the original img width is smaller).
However, the source can contain both portrait or landscape images. In case of long portraits, instead of filling the width I would like to center the image within the container given a maximum height.
I've tried the obvious:
width: 100%;
max-height: 250px;
But obviously it forces the width and the image is distorted. If I set max-width instead of width, smaller images than the container won't fill the space even in the maximum height is not reached.
Is there any way in pure css to tell the image to fill the container, unless some height is reached?

Why is there extra space in the div element when I use a percentage value as the width?

In this CodePen, why is there no space when "#spatula"'s width is a pixel value:
#spatula { width: 50px; }
but as soon as it's a percentage value:
#spatula { width: 10%; }
there's extra space on the right?
All depends on the size of the container if you operate with percentages, they will be calculated in relation to the size of its parent, the container. If in revenche you display in pixel, they will be displayed relative to the pixels of the screen. The pixels do not change according to the size of the screen or their parent containers. Pixels are a fixed and non-variant measure.

Text and images overlapping on decreasing size of window

I have a container div (width:100%) containing 3 more divs arranged along the container (of widths 33.3% each).
On decreasing the width of the window, all the images start to overlap. The text as well. How can I stop this?
I see that stack overflow has a mechanism which prevents the user to decrease width beyond a certain point.
You should use the css class min-width.
Eg:
.myclass {
min-width: 100px;
}
need to set 100% width to the image
img.class-name {
max-width: 100%;
width:100%;
}

Keep DIV fixed height, allow image inside div to change

What I am trying to do is have a container with a fixed height of 95% and a width of 100%. Then inside that div is an image with a variable height/width.
How would I go about keeping the container have a constant height/width but allow the image (changes with jQuery) to be any size smaller than the container and centered.
This is my current CSS that I have been playing around with but I havnt had any luck yet:
.gallery-image {
text-align:center;
line-height:0px;
min-height: 95%;
max-height: 95%;
width: 100%;
}
.gallery-image img {
max-height:65%;
max-width:95%;
}
Any ideas? Thanks.
Smaller than the container is relatively easy, you would just need to set the max-width and max-height styles --
max-width,
max-height
Centering is a little trickier; it will definitely involve some JavaScript. You will need to know the dimensions of the images (so if they haven't loaded you'll need to wait for them before you can read the dimensions), but assuming their dimensions are known here's an example method:
function centerImage(image,elem){ //elem is the parent container
var ratioDiff = (image.width() / image.height()) - (elem.width() / elem.height());
if(!isNaN(ratioDiff)){
if(ratioDiff > 0){
//center vertically - full width
image.css({width:elem.width()+'px',position:'relative'});
image.css({top:((elem.height() - image.height())/2)+'px'});
}else{
//center horizontally - full height
image.css({height:elem.height()+'px',position:'relative'});
image.css({left:((elem.width() - image.width())/2)+'px'});
}
}
}
This works by finding the ratio of height to width and scaling the image accordingly (setting left/top positioning where needed). It will basically always fill the image to the parent size and centered within the parent.
EDIT: I should mention this uses jQuery. You can accomplish it without (would just need to re-write the style setting and getting functions)... but why would you?

Difference between simple(width and height) and max/min(width and height)?

1-What's the difference between simple(width and height) and max/min(width and height)? Explain in terms of what will happen if the content, width and height of the element, for which (width and height) or max/min(width and height) is already specified in an internal style, grows more than the specified ones?
2-Secondly, how do we know which one to use when?(simple or max/min)
3-In the following example:
<html>
<head>
<style type="text/css">
p
{
max-height:50px;
background-color:yellow;
}
</style>
</head>
<body>
<p>The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
The maximum height of this paragraph is set to 50px.
</p>
</body>
</html>
Here, the max-height seems to have no effect on the content of the element as it's height grows and shrinks with the content in it?. Iam currrently using IE8.
Answers:
1: Please see below for the difference between simple and max:
#element {
width: 100px;
height: 100px;
background-color: red;
}
<div id="element">
I'm a 100px wide, 100px high block!
</div>
The div above will be a 100px high and 100px wide red block on the page with the text 'I'm a 100px wide, 100px height block' inside it. If the text were to long for this block it would either leak out or if you put overflow: hidden in your css for the element, the excess content would be hidden.
If instead you did this:
#element {
max-width: 100px;
max-height: 100px;
background-color: red;
}
<div id="element">
I'm a flexible block!
</div>
The element would be as large as your content but if your content ever reached 100px high or over the element would stop and it would do the same thing as the above example (either cut the content off if you have overflow: hidden in your css or the content will leak into the page from the element).
2: If you want a big red block on the page or. use width/height, if you want a small red block on the page that needs to grow but only grow to a certain size use max.
3: There are two types of elements inline and block, setting height and width (max or simple) will do nothing on an inline element (which a p, in your example, is not). You can set it to block in your css by adding display: block to the p css or use a div instead (which is block by default).