I'm trying to create 3 div's with a width of 100% and height is 100% so that every div occupies the entire screen. Every div has a text with an image placed at the bottom middle of the entire div.
<div class="first">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="second">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="third">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
Hence I gave the images absolute positioning and my main div's relative positioning and gave some percentage values to the absolutely positioned images, so that they are aligned properly at the bottom center even when the screen is resized.
.first{
width : 100%;
height : 100%;
position : relative;
}
.second{
width : 100%;
height : 100%;
position : relative;
}
.third{
width : 100%;
height : 100%;
position : relative;
}
img{
position : absolute;
top : 60%;
}
Here comes my problem when I resize the window the image is also getting resized as it is responsive and as it is absolutely positioned when the image size is getting bigger, It is getting overlapped on the text. How should I get rid of this overlapping in responsive screens? thanks in advance :)
If you are creating a responsive layout, CSS Flexbox module is a very good place to start. If I have understood the description of the layout you are trying to achieve correctly, here is an example of how you might approach creating that layout in Flexbox:
body {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
div {
flex: 1 0 100%;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
min-height: 100vh;
}
.first{
background-color:red;
}
.second{
background-color:yellow;
}
.third {
background-color:green;
}
img {
width: 40vw;
height: 10vw;
margin-bottom:12px;
background-color: rgba(0,0,0,0.3);
border: 4px solid rgba(0,0,0,0.4);
}
<div class="first">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="second">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
<div class="third">
<p>Some text is inserted here</p>
<img src="some-image" class="img img-responsive"/>
</div>
Related
I am trying to have an image on the left side and the heading and text on the right side. Currently, the image fits perfectly on the viewing screen in the about section when I scroll to it (vertical height of 91 and 45% of the width).
However, I wanted to write a heading and text on the right side of it, and whenever I try to add flex to the parent container my image shrinks and looks very odd. I am not sure how to rectify it so that the image takes 40% of the space in the viewing screen and the text and heading comes properly on the right.
return (
<div id="about" className="style-about">
<div className="style-picture">
<img src={aboutImage} alt="about image"></img>
</div>
<h1> About us </h1>
<p> Some text here... </p>
</div>
)
}
export default About
.style-about {
background-color:#b2c5b2;
min-height:91vh;
/* trying to use display:flex here */
z-index: -1;
}
.style-picture img {
max-width: 45%;
height:91vh;
object-fit: cover;
}
.style-about {
background-color: #b2c5b2;
min-height: 91vh;
/* trying to use display:flex here */
z-index: -1;
}
.style-picture img {
max-width: 45%;
height: 91vh;
object-fit: cover;
}
<div id="about" class="style-about">
<div class="style-picture">
<img src="https://via.placeholder.com/300x200" alt="about image" />
</div>
<h1> About us </h1>
<p> Some text here... </p>
</div>
This is a fairly basic flex layout, but you probably only want two columns. This means the heading and paragraph need to be contained.
I'm using flex-basis to set the first column's width. This makes it that wide on smaller screens. On larger screens (see the full page demo) the column shrinks to fit the image. Adjust to suit.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
.style-about {
background-color: #b2c5b2;
min-height: 91vh;
display: flex;
}
.style-picture {
flex-basis: 45%;
}
.style-picture img {
object-fit: cover;
max-width: 100%;
}
<div id="about" class="style-about">
<div class="style-picture">
<img src="https://via.placeholder.com/300x800" alt="about image" />
</div>
<div>
<h1> About us </h1>
<p> Some text here... </p>
</div>
</div>
the container element with display: flex only affects direct children elements (in your case the div.style-picture, the h1and the p)
.style-about {
background-color: #b2c5b2;
min-height: 91vh;
z-index: -1;
display: flex;
}
.style-picture {
flex-basis: 45%;
}
.nav-header-text {
flex-grow: 1;
}
.nav-right {
flex-grow: 0;
}
you can use flex-basis to have a fixed width of a flex child, then flex-grow: 1 on the middle element to take as much space as possible on the flex container and flex-grow: 0 on the right side element to make it not expandable and it will naturaly get placed on the right end of the flex container
<div id="about" className="style-about">
<div className="style-picture">
<img src={ico} alt="about" />
</div>
<h1 className="nav-header-text"> About us </h1>
<p className="nav-right"> Some text here... </p>
</div>
Hope it helps!
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
I have multiple images which I want to show using flexbox. But the images leave a gap if the next image cannot fit in the same row.
I want these images to resize accordingly so that there is no gap left.
Example:
HTML:
<div class="ImageContainer">
<div class="ImageBlock">
<img src="https://c4.wallpaperflare.com/wallpaper/246/739/689/digital-digital-art-artwork-illustration-abstract-hd-wallpaper-thumb.jpg"
alt="">
</div>
<div class="ImageBlock">
<img src="https://c4.wallpaperflare.com/wallpaper/410/867/750/vector-forest-sunset-forest-sunset-forest-wallpaper-thumb.jpg"
alt="">
</div>
<div class="ImageBlock">
<img src="https://c4.wallpaperflare.com/wallpaper/500/442/354/outrun-vaporwave-hd-wallpaper-thumb.jpg"
alt="">
</div>
<div class="ImageBlock">
<img src="https://c4.wallpaperflare.com/wallpaper/39/346/426/digital-art-men-city-futuristic-night-hd-wallpaper-thumb.jpg"
alt="">
</div>
</div>
CSS:
.ImageContainer{
margin:40px;
display: flex;
flex-wrap: wrap;
}
.ImageBlock{
margin:10px;
}
.ImageBlock img{
max-height: 250px;
}
You will need to add flex-grow: 1; to the .ImageBlock
This will make the block expand.
.ImageBlock {
margin: 10px;
flex-grow: 1;
}
Then you will have to make the img fill the block with width: 100%;
If you don't want the image to stretch out of proportions, you can use object-fit: cover;.
.ImageBlock img {
max-height: 250px;
width: 100%;
object-fit: cover;
}
Example codepen: https://codepen.io/bj-rn-nyborg/pen/rNMVrVL
I am trying to achieve a structure like below on a webpage
https://www.figma.com/file/NfikH1inSqCgwXOSzb3VeDCW/Untitled?node-id=0%3A1
So the full width is max width 1600px. Problem is, making it responsive has become very complex.
Reason is, all the boxes are given width in percentages.
So first whole section is divided in 2 parts 50% left – 50% right.
Inside 50% left – I have added 4 images by giving 50% width
Inside 50% right – I have added 1 image by giving 100% width
If we use just images in this structure, it stays very responsive if I reduce the screen size.
But as there are texts added BELOW each images, the box that contains the text has fixed width (66px). When we reduce the screen size, this disturbs the layout responsiveness.
Any solution to make it proper responsive?
I tried making the text box as an overlay to the image, so position absolute bottom of the image which fixes the issue but then the bottom part of the image goes behind the text box.
I want to make sure the image stays full visible and the text box also stay below it.
Any thoughts? I am happy to use JS too if there is a good solution.
*{
box-sizing: border-box;
}
.fifty {
width: 50%;
padding-bottom: 66px;
position: relative;
}
.parent {
display: flex;
flex-wrap: wrap;
}
img {
width: 100%;
height: 100%;
}
.left {
display: flex;
flex-wrap: wrap;
}
p {
position: absolute;
left: 50%;
bottom: 10px;
transform: translateX(-50%);
}
.pb-0 {
padding-bottom: 0;
}
.child {
border: 1px solid #333;
}
<div class="parent">
<div class="left fifty pb-0">
<div class="fifty child">
<img src="https://dummyimage.com/350x250/aae0ff/aae0ff" alt="image"/>
<p>Text</p>
</div>
<div class="fifty child">
<img src="https://dummyimage.com/350x250/aae0ff/aae0ff" alt="image"/>
<p>Text</p>
</div>
<div class="fifty child">
<img src="https://dummyimage.com/350x250/aae0ff/aae0ff" alt="image"/>
<p>Text</p>
</div>
<div class="fifty child">
<img src="https://dummyimage.com/350x250/aae0ff/aae0ff" alt="image"/>
<p>Text</p>
</div>
</div>
<div class="right fifty child">
<img src="https://dummyimage.com/350x250/ffa0a0/ffa0a0" alt="image"/>
<p>Text Right</p>
</div>
</div>
I think you want something like this, if needed anything else, Please let me know
I'm working on a layout for a thumbnails component which contains an image and text element. The images have a height: 100px; and auto width, while the text should span up to the width of the image width and the overflow should be hidden.
<div>
<img src="...">
<span>a bunch of text that should be cut off</span>
</div>
I've tried accomplishing this using flexbox but I can't get the text width to be equal to the image width. Instead, the text just grows 100% and stretches out the image.
See my jsbin
You need to specify container's width -> so that your text box knows it's width.
You need tell text to not wrap.
You need to hide the overflowing text.
You need ellipsis for overflowing text.
Following code works:
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
width: 50%; // px would also work
}
.main img {
height: 100px;
flex: 0 0 100%;
}
.main div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="main">
<img src="http://i.imgur.com/uiGur1h.jpg" alt="">
<div>Text go here </div>
</div>
<div class="main">
<img src="http://i.imgur.com/uiGur1h.jpg" alt="">
<div>Text would go here but would be cut off if it's very long </div>
</div>
Refer example in https://developer.mozilla.org/en/docs/Web/CSS/text-overflow