Trying to achieve responsive boxed structure using CSS - html

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

Related

Image and text on right using react and css

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!

Flexbox - Make item 16:9

I have a basic flexbox container with 2 items..
html, body {
margin:0;
}
.container {
display: flex;
flex-direction:row;
}
.item div {
width: 100%;
padding-bottom: 56.25%;
background: gold;
}
img {
max-width:100%;
object-fit:cover;
}
<div class="container">
<div class="item">
<div>
<img src="https://placeimg.com/1000/1000/any/grayscale">
</div>
</div>
<div class="item">
<div>
<img src="https://placeimg.com/1000/1000/any/grayscale">
</div>
</div>
</div>
Following the code at Maintain the aspect ratio of a div with CSS - I am trying to make the 2 items 16:9. But it is not working as expected.
Where am I going wrong?
Your problem is that the img elements inside the child divs are altering the height. Then the padding is being applied on top of that. You'll see what I mean if you remove the img.
To get around this, you can add the image as: background: url('imageurl'). Then add: flex-basis: 50% to your children and voila!
Did someone say codepen?

CSS for image next to block of text ON MOBILE

What's the best way to do this?
I have a full-width row (320px). 160px is an image on one side, the other 160px is reserved for text. Is it a simple float, or is there a less destructive way?
<div class="two-up_img u-pull-right">
<figure>
<img src="../img/img.jpg">
</figure>
</div>
<div class="two-up_info">
<div class="inner">
<h4 class="highlight">Rob</h4>
<div class="small">
<p>Landing Page</p>
<p>Brand Identity</p>
</div>
</div>
</div>
CSS
.two-up_info {
height: 160px;
background: #fff;
}
.two-up_info .inner {
text-align: center;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.two-up_img {
max-width: 50%;
}
I'm using a framework called Skeleton (http://getskeleton.com/), which is where the u-pull-right comes from. It floats an element to the right.
You want to wrap your html in a parent div and apply the styling clear: both to that parent div.
See this fiddle: https://jsfiddle.net/5gkzh2pa/
Or, if you are not concerned about supporting old browsers, take a look at flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

making image fill html/css

I'm aiming for a page with a collage of 4 photos;
a large one on the left taking up 50% width of the page and 100% of the height.
3 smaller photos, each taking up 50% of the width of the page but 33% of the height.
I'm running into problems with the larger image though,
my current CSS is;
.container {
width: 50%;
height: 100%;
overflow: hidden;
-size: cover;
}
and my html;
<div class="container">
<img height="100%" width="100%" src="jpg"></img>
</div>
the image's 50% width is fine but the height is only 50%.
It's a large image(4k) and my aim was to have overflow:hidden so that it fills the container but it's not working.
How could I do this?
Edit:
I'm aiming for it to be similar to this website:
http://www.masitupungato.com/
Suggested Solution
In fact, it is the easiest solution
Use two different divs, one for the left side and the other for the right side.
The left side div takes the half of the container width, and contains a image
The right side div takes the half of the container width, and contains 3 different divs, each one takes 33% of this right div height, and contains an image.
Use the CSS below:
.container {
width: 250px;
height: 250px;
margin: auto;
}
#left {
width: 50%;
float: left;
height: 100%;
}
#right {
width: 50%;
float: left;
height: 100%;
}
.right-inner{
width: 100%;
height: 33%;
}
.left-inner {
width: 100%;
height: 100%;
}
Expected output
Check it out.
You might change the height of the biggest image to fit the window of the device. Try to to set its height to "100vh", maybe it is what you were looking for.
you can use display:flex; property for this purpose. it will resolve your issue dynamically.
<div class="wrapper">
<div class="big-image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
<div class="small-image-wrapper">
<div class="image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
<div class="image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
<div class="image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
here is the fiddle https://jsfiddle.net/d95hw8fq/

Html: image going to new line when resizing page

I am making a header on a HTML page in which I have an image aligned to the left of the page and an image aligned to the right. I want there to be a center background color that is white when I enlarge the page horizontally, and that center white section to minimize to nothing when I shrink the page horizontally. Then the image on the right should be cut of from the right as the page shrinks.
The main problem I'm having is the image on the right goes down below the left image when I shrink the page. How can I fix this? The center section isn't white as well.
HTML:
<div class="navlogo">
<div class="left">
<img src="Weir-Logo.jpg">
</div>
<div class="right">
<img src="compass.jpg" />
</div>
</div>
CSS:
.navlogo {
width:100%;
background-color: white;
}
.navlogo .left {
float:left;
}
.navlogo .right {
float:right;
}
I would go with Turnip's answer, but here's another option for you for variety, if you like tables:
<table style="width:100%;background-color:white">
<tr>
<td align="left">
<img src="Weir-Logo.jpg">
</td>
<td></td>
<td align="right">
<img src="compass.jpg" />
</td>
</tr>
</table>
position: relative for the left image and position: absolute for the right image along with a z-index value on both should get you there:
.navlogo {
width: 100%;
background-color: white;
}
.navlogo .left {
float: left;
}
.navlogo .left img {
position: relative;
z-index: 1;
}
.navlogo .right {
position: relative;
}
.navlogo .right img {
position: absolute;
right: 0;
z-index: 0;
}
<div class="navlogo">
<div class="left">
<img src="http://placehold.it/400x200/f2f2f2/000000">
</div>
<div class="right">
<img src="http://placehold.it/400x200/000000/ffffff" />
</div>
</div>
With the "max-width" style the image will be resize.
<div class="left">
<img style="max-width:30%;" src="Weir-Logo.jpg">
</div>
<div class="right">
<img style="max-width:30%;" src="compass.jpg" />
</div>
So far you've got the old-school strategies covered: tables and position:absolute. Neither meets your requirement for "the image on the right should be cut off from the right as the page shrinks" and both can be problematic for a variety of reasons -- tables are, well, tables; and absolute-positioning, while sometimes necessary, tends to lead to fragile layouts; I try to avoid reaching for that part of the toolbox unless absolutely necessary.
In this situation I would depend on CSS background images, with an #media breakpoint to cover the two different layouts.
With this HTML:
<div class="navlogo"><div></div></div>
This covers the "the screen is wider than both images; put whitespace in between them" case:
.navlogo {
background: url('//placehold.it/250x100') top left no-repeat;
}
.navlogo div {
background: url('//placehold.it/250x100') top right no-repeat;
min-height: 100px;
}
Then, for the "the screen is smaller than the two images, so cut the right-hand one off from the right":
#media screen and (max-width: 500px) {
.navlogo div {
background-position: 250px 0;
}
}
(The #media breakpoint here should be the width of both images added together. The right-hand image's background-position should be the width of the left-hand image. Adjust for body margins/padding as needed.)
If I understood you correctly, you were looking for something like this?
Fiddle: https://jsfiddle.net/7twgsx23/1/
You need to give the navlogo a height value in order to get the white background.
CSS:
.navlogo{
width: 100%;
height: 100px;
background-color: white;
overflow: hidden;
min-width:600px;
}
I also suggest using a middle div to get the desired layout.
HTML:
<div class="middle">
</div>
You can use non-breaking space to fill the middle div if you don't want any content there.