How to make different sized images responsive? - css

I have two pictures that are equal size on desktop, but on mobile the heights are not the same. How can I keep the heights the same on mobile too without setting a specific height?
See here for screenshots: https://imgur.com/a/agz5T2z.
Here's my code (ReactJS):
HTML:
<div className="container beigeBackground">
<h1>Gallery</h1>
<div className="homeGalleryContainer">
<img src="https://images.pexels.com/photos/1449773/pexels-photo-1449773.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="Homepage Gallery 1"></img>
<img src="https://images.pexels.com/photos/6341164/pexels-photo-6341164.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="Homepage Gallery 2"></img>
</div>
<button>GALLERY</button>
</div>
CSS:
.container {
padding: 4em;
background-color: rgb(240, 240, 240);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.beigeBackground {
background-color: var(--Beige);
padding: 4em 2em;
}
.beigeBackground * {
color: white;
}
.homeGalleryContainer {
display: flex;
flex-direction: column;
padding-top: 4em;
padding-bottom: 3em;
}
.homeGalleryContainer img {
width: 100%;
margin-bottom: 2em;
object-fit: cover;
}
.beigeBackground button {
border-color: white;
}
.beigeBackground button:hover {
background-color: white;
color: var(--Beige);
}
#media screen and (min-width: 768px) {
.beigeBackground {
padding: 4em 0;
}
.homeGalleryContainer {
flex-direction: row;
padding: 4em 0;
padding-left: 2em;
/* to balance out margin-right of each img */
justify-content: center;
align-items: initial;
}
.homeGalleryContainer img {
width: 40%;
margin-bottom: 0;
margin-right: 2em;
}
}
Thank you.

this will make img responsive
.homeGalleryContainer img {
max-width: 100%;
height: auto;
}

Related

CSS Background rectangle width won't extend to full device width

I am trying to make a homepage for my portfolio website with a responsive css background rectangle and a side-by-side hero with text on the left and an image on the right, such as the website template here. I can't figure out what I did wrong in my HTML or CSS that's making it so that the background css rectangle isn't extending the full width of the screen. I'm new at this, so I would appreciate the help.
#charset "utf-8";
/* CSS Document */
#import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght#500&display=swap');
/*Variable*/
:root {
--primary-color: #0d081a;
--secondary-blue: #eaf0f9;
--secondary-gray: #48463b;
--body-bg: ffffff;
}
/*Global Styles*/
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
scroll-behavior: smooth;
}
body {
background-color: var(--body-bg);
font-family: Arial, Helvetica, sans-serif;
line-height: 1.6;
margin: 0;
min-height: 100vh;
}
img {
width: 100%;
height: 100%;
}
a {
color: black;
text-decoration: none;
}
section {
display: flex;
align-items: center;
justify-content: center;
}
.background {
border: 0;
padding: 0;
width: max-content;
background-color: var(--secondary-blue);
}
.container {
width: 90%;
height: 100%;
margin: 0 auto;
}
.primary-btn {
display: inline-block;
background-color: var(--primary-color);
color: white;
font-size: 1.8rem;
padding: 0.6em 1.6em;
border-radius: 50px;
}
.buttons {
margin-top: 3rem;
display: flex;
align-items: center;
justify-content: flex-start;
gap: 2rem;
}
/*nav*/
#navigation {
position: fixed;
right: 50px;
height: auto;
top: 20px;
z-index: 100;
}
/*hero section*/
#hero {
height: 100vh;
width: 100%;
padding-top: 10rem;
}
#hero .container {
display: flex;
align-items: center;
justify-content: center;
}
#hero .container .left {
flex: 6;
}
#hero .container .right {
flex: 8;
}
#hero .left .subheading {
font-size: 1.8rem;
font-weight: 500;
text-transform: uppercase;
color: var(--primary-color);
}
#hero .left .heading {
font-size: 6rem;
font-family: 'Times New Roman', Times, serif;
font-weight: 600;
}
#hero .left .desc {
margin-top: 2.5rem;
max-width: 400px;
font-size: 1.5 rem;
color: var(--secondary-blue);
}
#hero .right {
text-align: right;
}
#hero .right img {
width: 100%;
max-width: 450px;
height: 600px;
object-fit: cover;
object-position: 50% 30%;
border-radius: 12px;
}
#media only screen and (max-width: 768px) {
html {
font-size: 9px;
}
#hero .container {
flex-direction: column-reverse;
}
#hero .container .right {
text-align: center;
flex: 1;
margin-bottom: 2rem;
}
#hero .container .left {
text-align: center;
padding-right: 0;
flex: 1;
height: fit-content;
}
#hero .left .buttons {
justify-content: center;
}
#hero .left .heading {
font-size: 4rem;
margin: 0 auto;
}
#hero .container .left .desc {
margin: 0 auto;
margin-top: 2rem;
}
#hero .right img {
width: 100%;
height: 400px;
}
}
#media only screen and (max-width: 1200px) {
#hero .container .right {
flex: 6;
}
}
/* End Hero Section*/
/*Navbar*/
ul {
margin: 0;
padding: 0;
list-style: none;
}
a {
text-decoration: none;
}
.main-nav {
margin-top: 5px;
}
.logo a,
.main-nav a {
padding: 10px 15px;
text-transform: uppercase;
text-align: center;
display: block;
}
.main-nav a {
color: #34495e;
font-size: 1.5em;
}
.main-nav a:hover {
color: #718daa;
}
.header {
padding-top: 1em;
padding-bottom: 1em;
}
/* Nav Media Queries*/
#media (min-width: 769px) {
.header,
.main-nav {
display: flex;
}
.header {
flex-direction: column;
align-items: flex-end;
.header {
width: 80%;
margin: 0 auto;
max-width: 1150px;
}
}
}
#media (min-width: 1025px) {
.header {
flex-direction: row;
justify-content: flex-end;
}
}
<body>
<!--nav section-->
<header class="nav">
<ul class="main-nav">
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</header>
<!--end nav section-->
<!--hero section-->
<section id="hero">
<div class="background">
<div class="container">
<div class="left">
<p class="subheading"></p>
<h2 class="heading">
<div class="wrapper"><span>Hello, nice </span></div>
<div class="wrapper"><span>to"meet" you!</span></div>
</h2>
<p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<div class="buttons">
Contact Me
</div>
</div>
<div class="right">
<img src="/linkedinheadshot.png" alt="picture">
</div>
</div>
</div>
</section>
<!--End hero section-->
</body>
I've tried adjusting the padding and margins and the widths in the #mediaquery. Researching solutions on google and stack overflow.
i review your problem and if you use
#hero {
min-width: 100vw;
display: block;
background-color: var(--secondary-blue);
}
html #hero .container {
display: flex;
justify-content: space-between;
}
it will solve you problem
i am also a new web developer and learning now javascript
if it help you like this if you had still any problem post your question

How can I stop overlapping(and somehow image 'dissapearing) in the .container element - desktop media query when i put align-items: ... => responsive

I have tried putting flex: 1 on both child elements of the .container(responsive - media query), and it's not working. Somehow my image (.container-img (div element)) is 'hiding'/overlapping/not showing.
I do not know where the issue might be in this case.
I would appreciate if someone more experienced took a look at this issue. Thank you!
<body>
<main class="main-container">
<div class="container">
<div class="container-img">
</div>
<div class="container-desc">
<span class="secondary-title">PERFUME</span>
<h1 class="main-title">Gabrielle Essence Eau De Parfum</h1>
<p class="text">
A floral, solar and voluptuous interpretation composed by Olivier Polge,
Perfumer-Creator for the House of CHANEL.
</p>
<div class="price">
<span class="price-current">$149.99</span>
<span class="price-discount">$169.99</span>
</div>
<button class="cart">
<img src="images/icon-cart.svg" alt="Shop">
<span class="cart-shop">Add to Cart</span>
</button>
</div>
</div>
</main>
</body>
body {
background: var(--clr-bg-main);
font-family: 'Montserrat', sans-serif;
margin: 0;
}
img {
display: block;
max-width: 100%;
object-fit: cover;
}
h1,
p {
margin: 0;
}
.main-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
margin: 2rem 1rem;
width: min(100% - 2rem, 800px);
min-height: 80vh;
display: flex;
flex-direction: column;
}
.container-img {
background: url('images/image-product-mobile.jpg');
background-repeat: no-repeat;
background-size: cover;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
flex: 1;
}
.container-desc {
background: var(--clr-neutral);
padding: 2rem;
border-bottom-left-radius: 15px;
border-bottom-right-radius: 15px;
display: flex;
flex-direction: column;
}
.main-title {
font-family: 'Fraunces', serif;
font-size: var(--fs-title-primary);
line-height: 1;
margin-top: 1rem;
}
.secondary-title,
.text,
.price-discount {
color: var(--clr-dark-grayish-blue);
}
.secondary-title {
font-size: var(--fs-title-secondary);
letter-spacing: 5px;
}
.text {
font-size: var(--fs-text-primary);
margin-top: 1rem;
line-height: 1.6;
}
.price {
margin-top: 2rem;
display: flex;
gap: 2rem;
align-items: center;
}
.price-current {
font-family: 'Fraunces', serif;
font-size: var(--fs-text-current-price);
color: var(--clr-bg-button-price);
}
.price-discount {
text-decoration: line-through;
}
.cart {
cursor: pointer;
border: 0;
outline: none;
background: var(--clr-bg-button-price);
font-size: var(--fs-text-button);
font-weight: var(--fw-bold);
margin-top: 1.5rem;
padding: 1.5rem 6rem;
border-radius: 10px;
}
.cart:hover,
.cart:active {
background: var(--clr-bg-button-hover);
}
.cart img {
display: inline-block;
margin-right: 1rem;
}
.cart-shop {
color: var(--clr-neutral);
letter-spacing: 0.5px;
}
#media (min-width: 40em) {
.container {
min-height: 60vh;
flex-direction: row;
align-items: center;
}
.container-img {
background: url('images/image-product-desktop.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
z-index: 100;
}
.container-desc {
flex: 1;
}
}
Actually, i found the solution to this problem, which of course is pretty simple one. I have been focusing on the parent instead of a child element .container-desc specifically to put 'justify-items: center'. Lack of concentration.

How to apply object-fit: contain also to div?

There is object-fit: contain as a way to resize the image so that the entire image fits within the frame, aligning the long side of the image with the frame.
h2 {
font-family: Courier New, monospace;
font-size: 1em;
margin: 1em 0 0.3em;
}
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: flex-start;
height: 940px;
}
img {
width: 150px;
height: 100px;
border: 1px solid #000;
}
.narrow {
width: 100px;
height: 150px;
margin-top: 10px;
}
.contain {
object-fit: contain;
}
<div>
<h2>object-fit: contain</h2>
<img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="contain" />
<img src="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" alt="MDN Logo" class="contain narrow" />
</div>
However, this does not work for div tags and so on, it works only for replacement elements.
h2 {
font-family: Courier New, monospace;
font-size: 1em;
margin: 1em 0 0.3em;
}
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: flex-start;
height: 940px;
}
.contain-parent {
width: 150px;
height: 100px;
background: red;
border: 1px solid #000;
}
.narrow {
width: 100px;
height: 150px;
background: green;
margin-top: 10px;
}
.contain1 {
width: 50px;
height: 80px;
background: blue;
object-fit: contain;
}
.contain2 {
width: 80px;
height: 50px;
background: blue;
object-fit: contain;
}
<div>
<h2>object-fit: contain(not work)</h2>
<div class="contain-parent">
<div class="contain1"></div>
</div>
<div class="contain-parent">
<div class="contain2 narrow"></div>
</div>
</div>
How can I reproduce the same behavior as object-fit: contain on replacement elements with div tags,
span tags and so on? I want to display letter box when the aspect ratio does not match.

how to have a perfect responsive css circles? [duplicate]

This question already has answers here:
A grid layout with responsive squares
(5 answers)
Closed 5 years ago.
In process of learning flexbox, and confused about having perfect CSS circles that are responsive. How do I do that? As it stands, my current code has circle1, circle2, and circle3 at 100 width, and height. I don't want to hard-code their height but rather make it responsive. Is there a way to have a perfect circle in %? So it scales each time the browser is resized?
Or are media queries the only option to fix this?
Thank you for your help.
* {
box-sizing: border-box;
}
html, body {
height: 100%;
font-family: sans-serif;
font-weight: 100;
}
body {
display: flex;
margin: 0;
flex-direction: column;
}
main {
display: flex;
flex: 1 0 100%;
/*for content and sidebar */
flex-direction: row;
}
/* main */
#content {
flex: 1 0 80%;
/* for header/logo and description */
display: flex;
flex-direction: column;
}
#description img {
display: block;
}
#header {
flex: 1 0 5%;
padding: 10px;
/* for test */
display: flex;
justify-content: center;
}
#test {
display: flex;
flex-direction: row;
}
#header h1 {
text-align: center;
font-size: 5em;
padding: 0;
margin: 0;
font-family: 'Satisfy', cursive;
}
h1 {
font-family: 'Satisfy', cursive;
}
#description {
flex: 1 0 10%;
padding: 30px;
display: flex;
}
#description p {
padding-left: 20px;
font-size: 20px;
}
#description img {
width: 250px;
height: 250px;
border-radius: 50%;
border: 6px solid #db6525;
border: 6px solid #00B2AC;
}
#name {
font-size: 35px;
color: #db6525;
font-family: 'Satisfy', cursive;
}
#test img {
display: inline;
vertical-align: text-top;
width: 100px;
height: 100px;
/* for the following image and description */
display: flex;
flex-direction: row;
align-content: center;
align-items: center;
}
#sidebar {
flex: 1 0 20%;
/* background-color: green; */
text-align: center;
line-height: 90%;
/* for sidebar contents */
display: flex;
flex-direction: column;
}
#js {
flex: 1 0 33.33333%;
/* background-color: red; */
background-color: #db6525;
border: 20px solid #00B2AC;
padding: 10px;
}
#js h1 {
font-size: 50px;
}
#forms {
flex: 1 0 33.33333%;
/* background-color: gray; */
background-color: #db6525;
border: 20px solid #00B2AC;
padding: 10px;
}
#forms h1 {
font-size: 50px;
}
#sites {
flex: 1 0 33.33333%;
/* background-color: Chartreuse; */
background-color: #db6525;
border: 20px solid #00B2AC;
padding: 10px;
}
#sites h1 {
font-size: 50px;
}
.circles {
flex: 0 0 5%;
/* for circles within */
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.circle1 {
flex: 0 1 33.33333%;
display: flex;
justify-content: center;
}
.circle1 h1{
font-size: 12px;
color: #fff !important;
background-color: #db6525;
border: 4px solid #00B2AC;
border-radius:50%;
height: 100px;
width: 100px;
text-align: center;
vertical-align: middle;
}
.circle2 {
flex: 0 1 33.33333%;
display: flex;
justify-content: center;
}
.circle2 h1 {
font-size: 12px;
color: #fff !important;
background-color: #db6525;
border: 4px solid #00B2AC;
border-radius:50%;
height: 100px;
width: 100px;
text-align: center;
vertical-align: middle;
}
.circle3 {
flex: 0 1 33.33333%;
display: flex;
justify-content: center;
}
.circle3 h1 {
font-size: 12px;
color: #fff !important;
background-color: #db6525;
border: 4px solid #00B2AC;
border-radius:50%;
height: 100px;
width: 100px;
text-align: center;
vertical-align: middle;
}
<main>
<section id="content">
<article id="header">
<section id="test">
<h1>My Website</h1>
</section>
</article>
<article id="description">
<img src='images/profilePic.png' />
<p></p>
</article>
<article class="circles">
<div class="circle1">
<h1>Twitter</h1>
</div>
<div class="circle2">
<h1>Blog</h1>
</div>
<div class="circle3">
<h1>Contact</h1>
</div>
</article>
</section>
<section id="sidebar">
<article id="js">
<h1>Javascript</h1>
<p>Mini JS Projects</p>
<p class="subtitle">Work in progress
</article>
<article id="forms">
<h1>Free Forms</h1>
<p>Feel free to download the forms</p>
</article>
<article id="sites">
<h1>Portfolio</h1>
<p>Combination of previous work and additional sites</p>
</article>
</section>
</main>
The question now is How to have a perfect responsive css square? Because when you have a square, you will easily have a circle with border-radius: 50%. Now you can found so many solution for it in SO. Here is a nice solution with flexbox item.
.flex-container {
padding: 0;
margin: 0;
display: flex;
}
.flex-item {
background: tomato;
margin: 5px;
color: white;
flex: 1 0 auto;
border-radius: 50%;
}
.flex-item:before {
content:'';
float:left;
padding-top:100%;
}
<div class="flex-container">
<div class="flex-item ">
</div>
<div class="flex-item ">
</div>
<div class="flex-item ">
</div>
</div>
Updated answer
I reworked the flex containers to a minimal working example. The flex-items should all be set to
flex: 1 1 auto /* flex-grow flex-shrink flex-basis */
This allows the circle h1 flex-items to grow and shrink as necessary. It might be necessary to use js to obtain the height of a circle from its expanded width when you apply the example to your code.
Hope this helps.
html,
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
.circles {
/* for circles within */
display: flex;
justify-content: center;
height: 100%;
width: 100%;
}
.circle1,
.circle2,
.circle3 {
display: flex;
flex: 1 1 auto;
width: 33vw;
height: 33vw;
}
.circle1 h1,
.circle2 h1,
.circle3 h1 {
flex: 1 1 auto;
width: 100%;
height: 100%;
font-size: 12px;
color: #fff !important;
background-color: #db6525;
border: 4px solid #00B2AC;
border-radius: 50%;
text-align: center;
vertical-align: middle;
}
<article class="circles">
<div class="circle1">
<h1>Twitter</h1>
</div>
<div class="circle2">
<h1>Blog</h1>
</div>
<div class="circle3">
<h1>Content</h1>
</div>
</article>

How to expand background from flexbox grid text & images with equal height

I have done a flexbox grid, it combines text cells with image cells. All the cells have the same height in each resolution, also text cells. Text cells have the text centered vertically, and this is a must. Now I need to assign each text cell a different background-color, but here I have the problem.
With align-items: center the text is centered vertically, but the background color is only applied to the text. Without align-items: center the background expands to all the cell, but the content is not centered vertically.
Here is the codepen
Any suggestions in how to achieve the two features at same time?
vertically centered text
different background-color for each text cell
Thanks!
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
padding: 30px;
font-family: Helvetica;
}
a {
color: white;
text-decoration: none;
}
h2 {
font-size: 1.2em;
}
.wrap-items {
background-color: #ccc;
padding: 0;
margin: 0 auto;
display: -ms-flexbox;
-ms-flex-wrap: wrap;
-ms-flex-direction: row;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
}
.wrap-items .item {
-webkit-box-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 1 0 33.333%;
width: 33.33333%;
margin: 0;
padding: 0;
color: white;
font-size: 0;
border: none;
background-color: steelblue;
}
.wrap-items .item.span-2 {
-webkit-box-flex: 2 0 auto;
-ms-flex: 2 0 auto;
flex: 2 0 auto;
width: 66.6666%;
height: auto;
}
.wrap-items .item img {
width: 100%;
height: auto;
}
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
height: 100%;
}
.item.un .text{
background-color: orange;
}
.item.dos {
background-color: brown;
}
.item.tres {
background-color: violet;
}
#media screen and (max-width: 760px) {
.wrap-items .item {
margin: 0;
flex: 50%
}
}
#media screen and (max-width: 400px) {
.wrap-items .item {
margin: 0;
flex: 100%;
}
}
Use transforms instead of flexbox to vertically center the text.
1) Remove align-items: center from the .wrap-items class
2) Adjust the .wrap-items .item > .text class like this:
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Updated Codepen
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
padding: 30px;
font-family: Helvetica;
}
a {
color: white;
text-decoration: none;
}
h2 {
font-size: 1.2em;
}
.wrap-items {
background-color: #ccc;
padding: 0;
margin: 0 auto;
display: -ms-flexbox;
-ms-flex-wrap: wrap;
-ms-flex-direction: row;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
justify-content: center;
//align-items: center;
//align-content: center;
}
.wrap-items .item {
-webkit-box-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 1 0 33.333%;
width: 33.33333%;
margin: 0;
padding: 0;
color: white;
font-size: 0;
border: none;
background-color: steelblue;
position: relative;
}
.wrap-items .item.span-2 {
-webkit-box-flex: 2 0 auto;
-ms-flex: 2 0 auto;
flex: 2 0 auto;
width: 66.6666%;
height: auto;
}
.wrap-items .item img {
width: 100%;
height: auto;
}
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
.item.un {
background-color: orange;
}
.item.dos {
background-color: brown;
}
.item.tres {
background-color: purple;
}
#media screen and (max-width: 760px) {
.wrap-items .item {
margin: 0;
flex: 50%
}
}
#media screen and (max-width: 400px) {
.wrap-items .item {
margin: 0;
flex: 100%;
}
}
<div class="wrap-items">
<!-- row 1 -->
<div class="item span-2">
<div class="text">
<h2>THE TITLE</h2>
<p>Just be water my friend</p>
<p>Small people can do very big things.</p>
</div>
</div>
<div class="item">
<img src="https://placekitten.com/g/800/600" alt>
</div>
<!-- row 2 -->
<div class="item un">
<div class="text ">
one or more lines of text inside the box
</div>
</div>
<div class="item">
<img src="https://placekitten.com/g/800/600" alt>
</div>
<div class="item dos">
<div class="text">
text in the middle
</div>
</div>
<!-- row 3 -->
<div class="item">
<img src="https://placekitten.com/g/800/600" alt="">
</div>
<div class="item tres">
<div class="text">
three lines of text centered vertically in the middle of the box
</div>
</div>
<div class="item">
<img src="https://placekitten.com/g/800/600" alt>
</div>
</div>

Resources