How to have fixed div height irrespective of image height within it - css

I have a row with three coloumns. I want to have a image on each coloumn's divs with same height irrespective of image size in each div.
I want the images to get resized with in a div. But i get different size of divs as the image size of myImage1.png, myImage2.png and myImage3.png is different.
This is happening because i have fixed padding in my 'bg' class. Can somebody help me to fix this? Below is my HTML and CSS code.
<div class="row IDE_container">
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="bg">
<img src="myImage1.png" class="IDE_div" />
</div>
</div>
<div class="col-lg-2 col-md-2 col-sm-2">
<div class="circle_bg circle_bg_sm">
<img src="myImage2.png" class="IDE_div" />
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="circle_bg">
<img src="myImage3.png" class="IDE_div" />
</div>
</div>
</div>
.bg{
background-color: #ededed;
border-radius: 100%;
text-align: center;
padding: 35% 5%;
width: max-content;
max-width: 100%;
box-shadow: 0 0 3px 1px #DDD;
}
.IDE_div {
display: inline-block;
position: relative;
width: 100%;
height: 100%;
}
.IDE_container {
display: flex;
align-items: baseline;
}

For various kind of images, so can use object-fit property.
Add the CSS like this:
.IDE_div{
width: 100%;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}
<div class="col-3">
<div class="circle_bg">
<img src="myImage3.png" class="IDE_div" />
</div>
</div>
You will find the details about object-fit and object-position here : https://css-tricks.com/on-object-fit-and-object-position/

You need to set a height or max-height on the divs containing the images, or they will adapt to the height of their content (the images). As you've set the images to take up 100% width and height, and haven't constrained their parents, they will get as large as they can based on the original image size.
So, for example :
.IDE_container {
display: flex;
align-items: baseline;
div.col-3 {
div {
height: 200px;
.IDE_div {
display: inline-block;
position: relative;
width: 100%;
height: 100%;
&::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: var(--val);
padding-bottom: var(--val);
transform: translate(-50%, -50%);
z-index: -1;
border-radius: 50%;
}
}
}
}
}
You could give all containers the same class (e.g. fixed-height-bg) to style them more easily.
Also note: You don't need to give your columns individual classes for different screen sizes, as they always take up the same proportion. So, you can just use
<div class="col-3">
<div class="circle_bg">
<img src="myImage3.png" class="IDE_div" />
</div>
</div>
EDITED to add: if you want the images to maintain their proportions, you need to change your css to look like this:
img {
height: 100%; /* height of the parent container if specified, or of the image itself */
width: auto; /* will maintain the proportions of the image */
}
You can also switch those values, to height: auto; width: 100%; if you want the images to take the full width.

Related

Div with explicitly defined dimensions is scaling size with viewport width?

I have two divs in a container: one containing an image and the other containing text. The image container has the set dimensions width: 250px; and height: 240px;, but when the screen (and the containing div) is made narrow enough, the image container begins to decrease in size in addition to the other div, who is set to width: auto;.
I suspect that this is caused by the text in the second inner div requiring a certain amount of space and consequently 'pushing' the image container inward, but using word-break: break-word; didn't solve the issue.
Style:
/* "imagecontainer" and "credentials" are contained within "info-container" */
.subsection .info-container {
display: flex;
}
.subsection .imagecontainer {
width: 250px;
height: 240px;
margin: 0;
padding: 0;
}
.subsection .imagecontainer img {
width: 90%;
height: 90%;
margin-left: auto;
margin-right: auto;
border-radius: 50%;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
margin-top: 5px;
}
.subsection .credentials {
width: 600px;
height: 230px;
padding: 10px;
padding-right: 20px;
position: relative;
border-right: 1px solid #333;
}
/* This problem occurs at pageWidth<920px, when this media query would be in effect: */
#media only screen and (max-width: 920px) {
.subsection .credentials {
width: auto;
border-right: none;
}
}
Markup:
<div class="subsection">
<div class="info-container">
<div class="imagecontainer">
<img src="data/users/images/5d7d03b1d0013.png">
</div>
<div class="credentials" style="width: auto;">
<div class="name">Test User</div>
<div class="location">Albany, New York</div>
</div>
</div>
<div class="actions">
<div class="actions-container">
<div class="action">
<a class="btn btn-primary" href="viewprofile?id=5d7d03b1d0013">View profile</a>
</div>
<div class="action">
<a class="btn btn-primary" href="connect?id=5d7d03b1d0013">Make Connection</a>
</div>
</div>
</div>
</div>
Expected: only the div containing text will adjust its width in response to a change in viewport width.
Actual: both the image-containing div and the text-containing div decrease in size.
EDIT: Markup added
The parent container of that image container has display: flex;. If you don't define anything for the child elements, they will be allowed to shrink.
To avoid this, you can add flex-shrink: 0 (meaning it must not become any smaller) to .subsection .imagecontainer.

how to put text over image without absolute positioning

I am using bootstrap for layout, one of the difficulties I have run into is about how to put the text over image. I have tried all things I can do such as using background-image which does not work. One of the approaches is to place image as relative and put the text in the absolute position.
However for my issue, I can't use absolute position because I have so many images and texts' lengths are varied. So I have to solve issue this by using align-center..
If someone is familiar with this issue, could you give some advice for this problem.. I have to put the text over the image on the very center vertically and horizontally.
<div class="row justify-content-center">
<div class="col-sm-auto">
<img src="img/1.jpg">
</div>
<div class="col-sm-auto">
<img src="img/2.jpg">
</div>
<div class="col-sm-auto">
<img src="img/3.jpg">
</div>
</div>
If the image source isnt changing, then you can try setting background: url(img/1.jpg) from CSS and adjust its position using background-position
Check out the fiddle here.
http://jsfiddle.net/yktvod54/2/
html
<div class="row justify-content-center">
<div class="col-sm-auto image-1">
text1
</div>
<div class="col-sm-auto image-2">
text2
</div>
<div class="col-sm-auto image-3">
text3 with longer text
</div>
css
.col-sm-auto {
color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.col-sm-auto.image-1 {
width: 200px;
height: 150px;
background:
url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
background-size: cover;
}
.col-sm-auto.image-2 {
width: 200px;
height: 200px;
background: url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
background-size: cover;
}
.col-sm-auto.image-3 {
width: 250px;
height: 250px;
background:
url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
background-size: cover;
}
Set the image as a background to a div, make that div display:flex; justify-content: center; align-items: center; and give it a width/height. The flex alignments will push everything to the center of the div, but it needs a height/width or the div will only be the size of the text. Add flex-wrap: wrap; and the text will wrap around, but there isnt a nice breakpoint like with word-break
I can't use absolute position because I have so many images and texts' lengths are varied
You can use relative lengths for positioning you texts so they will be centered whatever their size.
You will need the translate CSS function as well.
.image-wrapper {
width: 400px;
position: relative;
}
.image-wrapper img {
width: 100%;
height: auto;
}
.image-wrapper figcaption {
width: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
font-size: 2em;
color: white;
}
<figure class="image-wrapper">
<img src="https://image.ibb.co/n5wsKU/bg3.jpg" alt="A nice pic!">
<figcaption>A text that fits on one line</figcaption>
</figure>
In case you still don't want to use absolute positioning, here is another solution using a background-image instead of an img tag:
.image-wrapper {
width: 400px;
height: 300px;
display: flex;
}
.image-wrapper.image-01 {
background: url('https://image.ibb.co/n5wsKU/bg3.jpg') center center / cover;
}
.image-wrapper p {
margin: auto;
font-size: 2em;
color: white;
}
<div class="image-wrapper image-01">
<p>Some text</p>
</div>

In a gallery, how do I make my portrait image larger so as to fill the full div while also keeping it centered?

I have read other answers and can't find an answer that works for me. I am trying to create an image gallery that is both responsive and takes different sized and orientated images i.e. some will be landscape, others will be portrait. I want it so that no matter its size or orientation it fills the size of the div and is centered within it.
Currently, the div is 250px height and width is 23% of the container. This was the best I could get it. Using this method the landscape images show fine but the portrait images have whitespace on either side of as it is not taking up the full div.
How can I get it so that no matter the size of the image it will fill the div without stretching it? I don't mind if the image is larger and gets cut off via overflow: hidden just as long as it fully covers the div.
EDIT: corrected the size of my stated image container width as I said it wrong.
Here is my code:
CSS
.gallery-container {
width: 90%;
margin: 0 auto;
}
.image-container {
float: left;
position: relative;
overflow: hidden;
height: 250px;
margin: 5px;
border: 1px solid #ccc;
width: 23%;
}
div.image-container:hover {
border: 1px solid #777;
}
div.image-container img {
position: absolute;
left: 50%;
top: 50%;
transform: translate( -50%, -50%);
height: 100%;
}
HTML
<div class="gallery-container">
<div class="image-container">
<a target="_blank" href="img/60.jpg">
<img src="img/60.jpg" alt="flowers">
</a>
<!-- <div class="desc">Add a description here
</div> -->
</div>
<div class="image-container">
<a target="_blank" href="img/portrait.jpg">
<img src="img/portrait.jpg" alt="portrait">
</a>
<!-- <div class="desc">Add a description here</div> -->
</div>
<div class="image-container">
<a target="_blank" href="img/flowers2.jpg">
<img src="img/flowers2.jpg" alt="flowers2">
</a>
<!-- <div class="desc">Add a description here</div> -->
</div>
</div>
div.image-container img {
position: absolute;
left: 50%;
top: 50%;
transform: translate( -50%, -50%);
width:100%;
}
object-fit and object-position are CSS properties specialized in the behavior of replaced elements like <img> and <video>. Applied like so:
div.image-container img {
/* Added */
width: 100%;
height: 100%;
/* Added */
object-position: 50% 50%;
/* Added */
object-fit: cover;
}
This particular ruleset will render the <img> so that it will fill its parent container edge to edge without distortion but cropping will happen should the dimensions of the parent not coincide with the image's AR (Aspect Ratio).
To center a series of elements, display:flex and justify-content:center on the parent should suffice:
.gallery-container {
width: 90%;
margin: 0 auto;
/* Added */
display: flex;
/* Added */
justify-content: center;
}
Demo
.gallery-container {
width: 90%;
margin: 0 auto;
/* Added */
display: flex;
/* Added */
justify-content: center;
}
.image-container {
overflow: hidden;
height: 250px;
margin: 5px;
border: 1px solid #ccc;
width: 23%;
}
div.image-container:hover {
border: 1px solid #777;
}
div.image-container img {
/* Added */
width: 100%;
height: 100%;
/* Added */
object-position: 50% 50%;
/* Added */
object-fit: cover;
}
<div class="gallery-container">
<div class="image-container">
<a target="_blank" href="img/60.jpg">
<img src="https://interactive-examples.mdn.mozilla.net/media/examples/plumeria.jpg" alt="flowers">
</a>
<!-- <div class="desc">Add a description here
</div> -->
</div>
<div class="image-container">
<a target="_blank" href="img/portrait.jpg">
<img src="https://interactive-examples.mdn.mozilla.net/media/examples/plumeria.jpg" alt="portrait">
</a>
<!-- <div class="desc">Add a description here</div> -->
</div>
<div class="image-container">
<a target="_blank" href="img/flowers2.jpg">
<img src="https://interactive-examples.mdn.mozilla.net/media/examples/plumeria.jpg" alt="flowers2">
</a>
<!-- <div class="desc">Add a description here</div> -->
</div>
</div>

Responsive Alignment CSS

So I'm either missing something or misunderstanding how this should work, and I'm near my wit's end.
I would like to have four columns. Each column will have an image and a div containing text. The image should fill the width of the column with any overflow hidden so that the entire height does not exceed 600px. The wrapper should be centered horizontally and everything should be responsive, as I will delete the right column depending on screen size.
My problems... when all four columns are displayed, my text divs are not as wide as the images. When I kill #column4, the text divs are the same width as the images, but then I can't center the wrapper on the page. And the image in #column1, at some screen widths, won't hide the overflow of the image, so part of it "sticks out" so that he column is taller than the others. sigh I'm obviously self-taught... poorly.
I've tried killing the float and changing the display, but each display option gives me odd vertical spacing.
Edited to include an image of all four columns. First, and most irritating, is that I cannot get the wrapper div centered on the page. Also, note the images and text boxes are different widths. (However, when the page is narrower and column4 is hidden, then the widths are the same.) Finally, I would like the image to fill the available space vertically (65% and 75% of the column) with the text divs to fill the remainder so that all columns are the same height.
CSS Fail
#dcwrapper {
width: 90%;
max-width: 1200px;
height: 600px;
margin: 10px 5%;
position: relative;
}
.column {
width: 24%;
margin-right: 1%;
float: left;
}
#text1 {
height: 35%;
width: 100%;
}
#text2 {
height: 25%;
width: 100%;
}
#text3 {
height: 25%;
width: 100%;
}
#text4 {
height: 35%;
width: 100%;
}
#image1 img {
max-height: 385px;
max-width: 295px;
overflow: hidden;
}
#image2 img {
max-height: 445px;
max-width: 295px;
overflow: hidden;
}
#image3 img {
max-height: 445px;
max-width: 295px;
overflow: hidden;
}
#image4 img {
max-height: 385px;
max-width: 295px;
overflow: hidden;
}
.txt {
border: solid 2px #799048;
border-radius: 4px;
padding: 5px;
}
.image65{
height:65%;
overflow:hidden;
}
.image75{
height:75%;
overflow:hidden;
}
/* HIDE COLUMNS AT WIDTHS */
#media only screen and (max-width: 1250px) {
#column4 {
display: none;
}
#image1 img {
max-width: 100%;
}
#image2 img {
max-width: 100%;
}
#image3 img {
max-width: 100%;
}
#image4 img {
max-width: 100%;
}
}
<div id="wrapper">
<div id="column1" class="column">
<div id="text1" class="txt">
These are our hours.
</div>
<!--end text1-->
<div id="image1" class="image65">
<img src="1a.jpg" />
</div>
</div>
<!--end column1-->
<div id="column2" class="column">
<div id="image2" class="image75">
<img src="2a.jpg" />
</div>
<div id="text2" class="txt">
We have a lot of different products now.
</div>
</div>
<!--end column2-->
<div id="column3" class="column">
<div id="text3" class="txt">
Yo, make an appointment!
</div>
<div id="image3" class="image75">
<img src="3a.jpg" />
</div>
</div>
<!--end column3-->
<div id="column4" class="column">
<div id="image4" class="image65">
<img src="4a.jpg" />
</div>
<div id="text4" class="txt">
Lookey what's new
</div>
</div>
<!--end column4-->
</div>
<!--end wrapper-->

How to get a background picture going outside max-width div

I have a responsive website with max-width set to 1000px, but I need to fit background picture that will overlap one of the divs and also place full page-width bottom borders to other divs.
The code i have is like this:
.container {
width: 100%;
max-width: 1000px;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 750px;
background: url(background.jpg) no-repeat top center;
margin: auto;
}
.line {
border-bottom: 1px solid black;
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main line" id="second">
</div><div class="container">
<div id="third">
</div>
</div>
</body>
I get the first div with correct width and bottom border going across the full page width, second div has got the background picture showing, but the max-width of 1000px does no longer apply. The bottom border is shown correctly (dividing second and third div) and the third div has got the correct max-width applied again.
What am I doing wrong/not doing to get the max-width for the second div?
YOUR SOLUTION
If the browser support of background-size property is good enough for you, you can use background-size: cover;. Check here or here to see browser support.
Here is the code snippet to show how it works. Be sure to position your background-image to center center if you want it to always be centered.
.container {
width: 100%;
max-width: 300px;
margin: 0 auto;
}
.line {
border-bottom: 1px solid black;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 250px;
background: url(http://lorempixel.com/250/250) no-repeat center center;
background-size: cover; /* This does the magic */
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main" id="second">
<div class="container">Put your content in here.</div>
</div>
<div class="line"></div>
<div class="container">
<div id="third">
</div>
</div>
<div class="line"></div>
</body>
LAST (BUT NOT LEAST)
You might want to check this great article about the state of responsive images in web design, that will help you if you are going into responsive web design: Responsive images done right.

Resources