I am making a react website and since I don’t have much experience with front-end developing I am still learning how the positioning exactly works, when to use vh/vw/%/px etc.
In the main page of my latest web application, I have made a grid layout followed by 5 rows (header, store-location image, filterbar, products, footer). I have this css for the image:
.shopImage {
height: auto;
width: 75%;
display: block;
position: relative;
top: -30px;
left: 12.5%;
z-index: 1;
}
The issue that I am facing is when the resolution width is different, the image resolution
also change leaving an unwanted gap between the image and the filterbar. Any suggestions how I can fix this with keeping the image’s good quality?
normal view /
smaller resolution
UPDATE:
Here is my HTML code as requested:
<body>
<div class="grid-container">
<div id="header" class="grid-item item1"></div>
<div class="imageContainer">
<div id="image" class="grid-item item2">
<img class="shopImage" src={shopImage} />
</div>
</div>
<div id="filterMenu" class="grid-item item3">
<div class="filterMenu">
<ul className='filters'>
<li><a>...</a></li>
<li><a>ВСИЧКИ</a></li>
<li><a>ГРИВНИ</a></li>
<li><a>ОГЪРЛИЦИ</a></li>
<li><a>ПРЪСТЕНИ</a></li>
<li><a>ОБЕЦИ</a></li>
<li>
<input type="text" className='filterSearchbar' id="filterSearchbar" />
<img class="SearchIcon" src={searchIcon} />
</li>
</ul>
</div>
</div>
<div class="grid-item item4">3</div>
<div class="grid-item item5">4</div>
</div>
</body>
</html>
and css:
/*
GRID BOX AND BODY OF THE PAGE
*/
body {
max-width: 100%;
height: 2500px;
position: relative;
padding-left: 50px;
padding-right: 50px;
}
.grid-container {
display: grid;
grid-template-rows: 0.1fr 1fr 0.1fr 4fr 0.2fr;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item1 {
grid-row: 1 / 2;
min-height: 70px;
position: relative;
}
.item2 {
grid-row: 2 / 3;
min-height: 400px;
max-height: 700px;
}
.item3 {
grid-row: 3 / 4;
min-height: 70px;
position: relative;
}
/* MAIN IMAGE SHOP */
.imageContainer {
width: 75%;
margin: 0 auto; /* Center the container */
}
.shopImage {
min-height: 100%;
width: 100%;
display: block; /* Needed to apply margins to an image */
position: relative;
z-index: 1;
top: -30px;
}
/* FILTER MENU */
.filterMenu {
position: relative;
width: 75%;
height: 5vw;
left: 12.5%;
top: -30px;
background-color: #2E0002;
z-index: 99;
}
.filters {
float: left;
position: relative;
list-style-type: none;
width: 150%;
left: -12.5%;
overflow: hidden;
transform: scale(0.8);
}
.filters li {
display: inline;
float: left;
margin-right: 50px;
text-align: center;
margin: 0 4.9vw 0 0;
}
.filters li a {
display: block;
color: white;
text-decoration: none;
font-weight: 540;
font-size: 1.6vw;
font-family: Verdana, Geneva, Tahoma, sans-serif;
position: relative;
}
.filters li:hover a {
color: #FFC700;
}
.filterSearchbar {
width: 90%;
font-size: 24pt;
position: relative;
left: -1.5vw;
top: 0.1vw;
border-radius: 30px;
}
.SearchIcon {
position: absolute;
left: 75.8%;
top: -11.2%;
}
2
Answers
To make your image responsive and maintain its quality while avoiding unwanted gaps, you can use the
max-width
property along with a percentage value. This will ensure that the image doesn’t exceed a certain width while maintaining its aspect ratio.Here’s an updated CSS code for your
.shopImage
class:In this code,
max-width: 100%
ensures that the image’s width won’t exceed the width of its container. This way, as the resolution changes, the image will scale down proportionally, preventing any unwanted gaps between the image and the filter bar.Additionally, you might want to consider using a container div around the image to better control its positioning and ensure responsiveness. Here’s an example:
And in your HTML:
This container will center your image and provide a more consistent and controlled layout, regardless of the screen resolution. Adjust the percentage values according to your design requirements.
You don’t need a
height
value for yourbody
element. This breaks the natural behavior of HTML. You use overuse theposition: relative;
declaration. You also don’t need themin-height: 100%;
declaration for your image. You use these properties incorrectly and that’s why your code doesn’t behave as expected. If you don’t have explicit values for yourgrid
template, you can useflexbox
to organize your elements. Don’t break the natural responsive behavior of HTML by giving elements too many CSS styles.You can use this ruleset for your images:
For your flex container:
You can check this little demo to see what I’m talking about…