Image and text on right using react and css - html

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!

Related

Trying to achieve responsive boxed structure using CSS

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

Why are my images overflowing the window with flexbox?

I have created a showcase section where in I have used flexbox to align images to the right. However when I shrink the size of the window, the images go out of the window. I am looking for something like this: http://jsfiddle.net/xLc2Le0k/15/
Here is the snippet for HTML:
<div class="showcase shadow">
<div id="places">
<p class="category">Places</p>
<div>
<img src="img\Taj Hotel.png" alt="">
</div>
<div>
<img src="img\Gateway of India.png" alt="">
</div>
<div>
<img src="img\Shack at Goa.png" alt="">
</div>
</div>
<p class="more-text">View more...</p>
</div>
Here is the snippet for SCSS:
.showcase {
width: 100%;
margin-top: 5%;
display: inline-block;
#places {
width: 100%;
display: flex;
div:nth-of-type(1) {
align-self: flex-end;
}
div {
margin-left: 0.5%;
}
}
}
Here is the link for the live web page: https://swizzx.github.io/Photography-Portfolio/
Just a heads up, I have tried setting the width of the parent and the element to 100%, when I do so, it shrinks but does not work as how I want it to like in the JSFiddle provided above. On the contrary setting the width to 100% makes the first image equal to the size of the others which I don't want.
You should add below css property to the flex container. It wraps elements automatically to the next line when you shrink the window.
flex-wrap: wrap;
One thing you are missing in your code is applying 100% width to your img tag. Give img 100% width and it will work the same way you want.
#places div img {
width: 100%;
}
Actually it was the paragraph in the #places div that was causing the images to shrink much more than required. Figured out that had to place it out of the #places div and inside the .showcase div like :
<div class="showcase shadow">
<p class="category">Places</p> // like this
<div id="places">
<div>
<img src="img\Taj Hotel.png" alt="">
</div>
<div>
<img src="img\Gateway of India.png" alt="">
</div>
<div>
<img src="img\Shack at Goa.png" alt="">
</div>
</div>
<p class="more-text">View more...</p>
</div>
and than setting the width of the images to 100% like gagan mentioned :
.showcase {
width: 100%;
margin-top: 5%;
display: inline-block;
#places {
width: 100%;
display: flex;
justify-content: flex-end;
div:nth-of-type(1) {
align-self: flex-end;
}
div {
margin-left: 0.5%;
img {
width: 100%; //width to 100%
}
}
}
}

css: how to "center" a big image inside limited width and height without changing dimensions

before anything: No I don't mean centering an image inside a div
WHAT I WANT TO DO IS SIMILAR TO THE DESKTOP 'CENTER' OPTION HENCE THE TITLE PLEASE DON'T BE CONFUSED I JUST DON'T KNOW THE SUITABLE TERM OF WHAT I WANT
I am making a certian css template for my use, but I have this question that I couldn't figure out ..
basically, there is an image before each post, so the width is known. I set it to 100% of a container called "post". post has a width of 75% of the browser
<div class="post">
<h1>post title</h1>
<img class="post-img" src="myImg.png" />
<!--other elements-->
</div>
css:
.post {
width: 75%;
.post-img {
max-width: 100%;
}
Now to the problem ..
although the width now is fixed regardless of the image, the height is pretty much automatic
I want to set the height to a certain value, like max-height: 500px; for example .. so when a picture is big:
make width = 100% of the post div
is the height > 500px? no then make it auto. yes then crop the extra part
an image
as you can see, the black stroke is the limited width and height
the width was checked first so no extra parts to the left and right
the height is more than 500px, so it will be cropped and the viewed image would be the one inside the black frame
Like i said overflow:hidden; is the one you need
.container{
width: 700px;
}
.post {
width: 75%;
overflow:hidden;
border:2px solid #f63;
}
.post img{
max-width: 100%;
}
<div class="container">
<div class="post">
<h1>post title</h1>
<img class="post-img" src="http://www.petsworld.in/blog/wp-content/uploads/2014/09/adorable-cat.jpg" />
<!--other elements-->
</div>
</div>
What you are trying to achieve may be something like that, using background instead of <img>, so you have a much more simple control on the image :
.post {
width: 75%;
}
.container {
width: 100%;
height: 500px;
overflow: hidden;
background: url('https://s3-eu-west-1.amazonaws.com/hasselblad-masters/web-hi-res-content/X1D-Sample-Images/X1D5_B0001993.jpg') center no-repeat;
background-size: cover;
}
<div class="post">
<h1>post title</h1>
<div class="container">
</div>
<article>
<p>Other content</p>
</article>
</div>
I think what you need is to wrap your image in a container and set the maximum sizes and overflow:hidden; on that container
Something like:
<div class="post">
<h1>post title</h1>
<div class="img-container">
<img class="post-img" src="img.jpg" />
</div>
</div>
<style>
.post {
width: 75%;
}
.img-container {
max-width: 100%;
max-height: 100px;
overflow: hidden;
}
</style>

How to make a div centered, text to the left and photo to the right

I don't know how to make a paragraph and a photo inline like this one
Flexbox Module can achieve that easily.
CSS:
#profile {
display: flex;
}
#bio {
flex: 2;
margin-right: 2px;
/*
other rules for bio here
*/
}
#photo {
flex: 1;
/*
other rules for photo here
*/
}
HTML:
<section id="profile">
<section id="bio">
<p>Bio Text</p>
</section>
<section id="photo">
<img src="user_profile_photo.jpg" alt="User Photo" />
</section>
</section>
alternatively, you can use 'display: table' for '#profile' and 'display: table-cell' for '#bio' & '#photo'.
To get the content centered, you should specify the max-width of the container holding your content and set the margin to auto.
You could try putting both in the same div and then in CSS using float: right; for the image and float: left; for the text?
If you don't want to keep fiddling with values in CSS, you could use the Bootstrap framework and do:
<div class="summary">
<p class="col-md-9">[text here]</p>
<img class="col-md-3" src="[image source]"/>
</div>
and then play around with the 9 and 3 to get the width of the columns how you want them

Absolute positioned image getting overlapped on the other elements in responsive

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>