Implementing CSS GRID for Layout
The concept is straightforward, as shown in the code snippet below. However, I will provide a detailed explanation to ensure clarity for all readers.
Exploring a New Solution
I incorporated the calc();
function in my CSS code, which facilitates dynamic calculations within the styling.
Additionally, I utilized a CSS variable to specify the height of the navigation bar:
--nav-height: 46px;
To apply this height to your navigation bar selector, simply use height: var(--nav-height);
If you require two images displayed vertically, with one above the other...
You can achieve this by setting display: grid;
on the parent element. Essentially, the grid
property organizes the elements in rows automatically.
Avoid Using %
; Instead, Consider:
vh
for height: This unit represents 1% of the browser's viewport height, irrespective of the parent element's dimensions.
vw
for width: Similar to vh
, this unit corresponds to 1% of the viewport width.
By utilizing vh
, the layout becomes responsive by adapting to various screen sizes consistently.
Responsive design ensures compatibility across all devices.
To set the half-height for both images, employ 50vh
:
50vh equates to half of the screen's height.
Addressing an Issue
Your query also involves integrating a navigation bar into the layout...
Here’s a solution: Use the following formula to synchronize image heights:
--img-height: calc(50vh - ((var(--nav-height)) / 2))
This approach involves:
- Setting the image height to half of the viewport
- Determining the navigation bar's height (
NavHeight
)
- Subtracting half of
NavHeight
from each image’s height
In Conclusion:
Set the image widths to auto
for proper display:
width: auto;
Utilizing auto
prevents image distortion.
Implementation Code Snippet
* {
--nav-height: 46px;
--img-height: calc(50vh - ((var(--nav-height)) / 2))
}
body {
margin: 0;
height: 100vh;
}
nav {
height: var(--nav-height);
background: blue;
color: white;
}
.container {
display: grid;
}
.container .image {
height: var(--img-height);
width: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav>my Awesome NavBar</nav>
<div class="container">
<img class="image" src="https://laaouatni.github.io/w11-clone/images/1dark.jpg" alt="">
<img class="image" src="https://laaouatni.github.io/w11-clone/images/0light.jpg" alt="">
</div>
</body>
</html>
Prior Approach
If you aim to position two elements, one above the other...
Simply utilize display: grid;
on the parent container to align them effectively in a single row.
A Different Strategy without %
:
- Height Representation using
vh
: Refers to a percentage of the total viewport height, ensuring consistent sizing regardless of parent constraints.
- Width Utilization through
vw
: Corresponding to a percentage of the overall viewport width for uniform dimensioning.
Applying 50vh
Height for Images
Half of the screen’s height can be depicted by 50vh.
Employing vh
facilitates optimal responsiveness across diverse devices.
Responsive functionality guarantees seamless operation on various platforms.
Maintain Image Width Integrity with auto
width: auto;
By embracing auto
, image proportions remain intact without any distortions.
Past Style Specifications
body {
margin: 0;
}
.container {
height: 100vh;
display: grid;
}
.container .image {
height: 50vh;
width: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<img class="image" src="https://laaouatni.github.io/w11-clone/images/1dark.jpg" alt="">
<img class="image" src="https://laaouatni.github.io/w11-clone/images/0light.jpg" alt="">
</div>
</body>
</html>