I know the question of overlapping has been answered multiple times, however, I'm trying to place the text on top of the image centred once hovered.
I tried z-index, I tried relative and absolute, decided I'd ask for help.
.flex-container {
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: wrap;
margin: 0;
padding-left: 7%;
padding-right: 7%;
padding-top: 25px;
padding-bottom: 50px;
justify-content: center;
}
.flex-container div {
margin: 0;
position: relative;
margin-bottom: -5px;
font-size: medium;
text-align: center;
z-index: 0;
}
.flex-container div:hover {
filter: brightness(50%);
}
.flex-container div p {
display: none;
}
.flex-container div:hover p {
position: absolute;
color: black;
z-index: 1;
text-align: center;
}
<div>
<a href="Printing/menus.html">
<img src="../Images/menus-01.png" alt="Printing" style="width:100%; height:auto;"/>
</a>
<p>Menus</p>
</div>
You should have set the text CSS first and just then get the hover to show the styles, also, you forgot to set the container styles on your code.
See if that's what you want :
HTML :
<div class="container">
<img src="../Images/menus-01.png" alt="Image" style="width:100%;">
<div class="topTxt">Text on Center/div>
</div>
CSS :
.container {
position: relative;
text-align: center;
}
.topTxt {
position: absolute;
display: none;
top: 0;
left: 50%;
transform: translate(-50%, 0%);
}
.container:hover .topTxt {
display: block;
}
OR use JavaScript :
document.getElementsByClass("container").addEventListener("mouseover", function() {
document.getElementsByClass("topTxt").style.display = "block";
});
Get the following snippet to better view of what you requested :
#container {
max-width: 900px;
margin: 0 auto;
padding: 5px;
font-size: 0;
list-style: none;
background-color: #444;
}
#container li {
display: inline-block;
width: 25%;
vertical-align: middle;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container-cells {
margin: 5px;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.3);
display: block;
position: relative;
overflow: hidden;
}
.imgs {
display: block;
width: 100%;
height: auto;
border: none;
filter: brightness(100%);
transition: all 0.3s;
}
#container li:hover .imgs {
filter: brightness(50%);
}
.overlay {
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 0px;
margin: auto;
background: #000;
background-size: 50px 50px;
transition: all 0.3s;
}
#container li:hover .overlay {
height: 30px;
}
.title {
display: block;
padding: 5px 30px;
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 40px;
text-align: center;
font-size: 18px;
color: white;
opacity: 0;
transform: translateY(-20px);
transition: all .3s;
}
#container li:hover .title {
transform: translateY(0px);
opacity: 0.9;
}
#media (max-width: 9000px) {
#container li {
width: 25%;
}
}
#media (max-width: 700px) {
#container li {
width: 33.33%;
}
}
#media (max-width: 550px) {
#container li {
width: 50%;
}
}
<ul id="container">
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
</ul>
You just need to put the text before the image, as the image will be the background either way, just add the the text before, then the image and the text will be siblings, not parent and child, so the effect of hover won't affect the child, therefore, the text color will remain without having the filter applied to it.
Maybe the below is what you're looking for?
How to put text over images in html?
The answer suggests using a "div" tag rather than an "img" tag. Here is an example:
.image {
width:400px;
height:400px;
background-image: url(http://lorempixel.com/output/cats-q-c-640-480-4.jpg);
background-size:cover;
}
<div class="image">Text on top of image</div>
Related
This question already has an answer here:
CSS transparent curved shape with two rounded sides
(1 answer)
Closed 6 months ago.
How to apply rounded border radius in opposite direction on hover on menu item in menubar
I want this effect on menu item hover
Currently my menubar looks like this
This is my css code
sidebar-menu .dropdown-toggle:hover, .sidebar-menu .show>.dropdown-toggle {
background: #f8f9fa;
color: #2daab8;
border-top-left-radius: 50px;
border-bottom-left-radius: 50px;
border-bottom-right-radius: 70px;
border-top-right-radius: 70px;
width: 250px;
}
HTML Code:
<div class="left-side-bar lftsideBar">
<div class="brand-logo">
<a href="{% url 'home' %}">
<img src="{% static 'website/vendors/images/Rectangle_33.png' %}" alt="" />
</a>
</div>
<div class="close-sidebar" data-toggle="left-sidebar-close">
<i class="ion-close-round"></i>
</div>
<div class="menu-block customscroll">
<div class="sidebar-menu">
<ul id="accordion-menu menucls" style="margin-left: 25px !important;margin-bottom:250px;">
<li>
<a href="{% url 'home' %}" class="dropdown-toggle no-arrow">
<span class="micon dw dw-house-1"></span><span class="mtext">Home</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Client</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Medical</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Social</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Transportation</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Activity</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Food</span>
</a>
</li>
<li>
<a href="#" class="dropdown-toggle no-arrow">
<span class="micon dw dw-add-user"></span><span class="mtext">Reports</span>
</a>
</li>
</ul>
</div>
</div>
Can you please suggest me to what css make changes so I can make exactly same design as per screenshot-1 on each and every item of menubar?
You'll need imagination to be able to create the inverted radius. We could use a circle and use it to mask a square, but CSS mask is not very well supported still. But we can extend on this strategy and use borders to fill the part of a square not occupied by the circle.
In my example below, I've used a holder with overflow:hidden to hide any overflowing border. The size of the holder also determines the size of the final curve since the div inside spans to 100%. Instead of using a full circle, I've used just the 1/4 of it that was needed by rounding just the interesting corner to 100%. The color of the border determines the color of the curve.
.holder{
width:100px;
height:100px;
overflow:hidden;
}
.holder div{
width:100%;
height:100%;
border-bottom-right-radius:100%;
border:99999px solid black;
border-left:none;
border-top:none;
}
<div class="holder">
<div></div>
</div>
Now, armed with that knowledge, you can easily do the same for the bottom part by rounding a different corner. You'll need to add the markup to each menu element tough, and play with the display property. You could try to reduce as much as possible by using the ::before and/or ::after elements but I doupt you'd be able to save much (just maybe get to replace the inner div maybe?)
source code from there
const initNavBar = () => {
const menusEl = document.querySelectorAll('.side-bar ul li')
menusEl.forEach(menu => menu.addEventListener('click', ()=> {
const menuActiveEl = document.querySelector('.side-bar ul li.active')
menuActiveEl.classList.remove('active')
menu.classList.add('active')
}))
}
initNavBar()
:root {
--color-primary: #1380b8;
--color-secondary: #33ace3;
}
aside.side-bar-wrap {
--radius-size: 25px;
height: 98vh;
position: fixed;
top: 1vh;
left: 1vh;
overflow-y: scroll;
overflow-x: hidden;
border-radius: var(--radius-size);
padding-right: 2px;
}
aside.side-bar-wrap::-webkit-scrollbar {
width: 10px;
}
aside.side-bar-wrap::-webkit-scrollbar-track {
background-color: transparent;
}
aside.side-bar-wrap::-webkit-scrollbar-thumb {
border-radius: var(--radius-size);
background-color: var(--color-primary);
}
nav.side-bar {
min-height: 100%;
background-color: var(--color-primary);
display: inline-block;
border-left: var(--radius-size) solid var(--color-secondary);
border-right: var(--radius-size) solid var(--color-primary);
border-radius: var(--radius-size);
}
nav.side-bar .logo-area {
--curve-size: calc(2 * var(--radius-size));
position: relative;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
min-height: var(--curve-size);
background-color: var(--color-secondary);
border-radius: 0 var(--radius-size) var(--radius-size) 0;
box-shadow: var(--radius-size) 0 var(--color-secondary);
}
nav.side-bar .logo-area::after {
content: '';
width: var(--curve-size);
height: var(--curve-size);
background-color: var(--color-primary);
border-radius: 50%;
position: absolute;
bottom: calc(-1 * var(--curve-size));
left: 0;
box-shadow: calc(-1 * var(--curve-size) * 0.5) calc(-1 * var(--curve-size) * 0.5) var(--color-secondary);
}
nav.side-bar .logo-area img {
position: absolute;
max-height: 75%;
transition-duration: 1s;
}
aside.side-bar-wrap:hover nav.side-bar .logo-area img.min {
max-height: 0;
opacity: 0;
}
nav.side-bar .logo-area img.max {
max-width: 0;
opacity: 0;
transition-duration: 1s;
}
aside.side-bar-wrap:hover nav.side-bar .logo-area img.max {
max-width: 90%;
opacity: 1;
}
nav.side-bar ul {
padding: 0;
margin: calc(2 * var(--radius-size)) 0;
display: flex;
flex-direction: column;
}
nav.side-bar ul li {
height: 50px;
padding: 10px var(--radius-size);
list-style: none;
border-radius: var(--radius-size);
margin-bottom: var(--radius-size);
z-index: 1;
}
nav.side-bar ul li:not(.active) {
z-index: 2;
}
nav.side-bar ul li:not(.active):hover {
backdrop-filter: brightness(0.85);
}
nav.side-bar ul li.active {
position: relative;
background-color: var(--color-secondary);
border-radius: 0 var(--radius-size) var(--radius-size) 0;
}
nav.side-bar ul li.active::before,
nav.side-bar ul li.active::after {
--curve-size: calc(2 * var(--radius-size));
content: '';
width: var(--curve-size);
height: var(--curve-size);
background-color: var(--color-primary);
border-radius: 50%;
position: absolute;
}
nav.side-bar ul li.active::before {
top: calc(-1 * var(--curve-size));
left: 0;
box-shadow: calc(-1 * var(--curve-size) * 0.5) calc(var(--curve-size) * 0.5) var(--color-secondary);
}
nav.side-bar ul li.active::after {
bottom: calc(-1 * var(--curve-size));
left: 0;
box-shadow: calc(-1 * var(--curve-size) * 0.5) calc(-1 * var(--curve-size) * 0.5) var(--color-secondary);
}
nav.side-bar ul li a{
color: white;
font-size: 16pt;
width: 100%;
height: 100%;
display: flex;
gap: 0;
align-items: center;
text-decoration: none;
transition: 1s;
}
aside:hover nav.side-bar ul li a{
gap: 10px;
}
nav.side-bar ul li a span {
display: flex;
transition: 0.75s cubic-bezier(0.39, 0.58, 0.57, 1);
}
nav.side-bar ul li a span.icon {
font-size: 24pt;
}
nav.side-bar ul li a span.title {
max-width: 0;
opacity: 0;
}
aside:hover nav.side-bar ul li a span.title {
width: auto;
max-width: 10rem;
opacity: 1;
}
<aside class="side-bar-wrap">
<nav class="side-bar">
<div class="logo-area">
<img class="min" src="images/min-logo.png" alt="logo">
<img class="max" src="images/max-logo.png" alt="logo">
</div>
<ul>
<li class="active">
<a href="#">
<span class="title">Home</span>
</a>
</li>
<li>
<a href="#">
<span class="title">News</span>
</a>
</li>
<li>
<a href="#">
<span class="title">Jobs</span>
</a>
</li>
<li>
<a href="#">
<span class="title">Contact</span>
</a>
</li>
<li>
<a href="#">
<span class="title">About</span>
</a>
</li>
</ul>
</nav>
</aside>
I cannot understand how, in my case, possible to properly positioned the menu by creating only one class menu, instead of menu_left and menu_right classes.
How can I optimize css here?
Here's code example:
html:
<div class="menu_left" >
<div class="menu__item">
<span class="menu__item__link__text">SHOP</span>
</div>
<div class="menu__item">
<span class="menu__item__link__text">ABOUT</span>
</div>
</div>
<a href="" class="logo">
<img src="assets/sds.jpgf" class="logo__image">
</a>
<div class ="menu_right">
<div class="menu__item">
<span class="menu__item__link__text">CART</span>
</div>
<div class="menu__item">
<span class="menu__item__link__text">EUR</span>
</div>
</div>
Menu Image
css
.menu_left {
background: rgba(0, 0, 0, 0.85);
position: relative;
top: auto;
right: auto;
bottom: auto;
left: auto;
background: transparent;
float: left;
width: auto;
opacity: 1;
visibility: visible;
-webkit-transition: none;
-moz-transition: none;
transition: none; }
.menu_right{
position: relative;
top: auto;
right: auto;
bottom: auto;
left: auto;
background: transparent;
float: right;
width: auto;
opacity: 1;
visibility: visible;
-webkit-transition: none;
-moz-transition: none;
transition: none; }
}
Try it with Flexbox
See flexbox menu here
HTML
<div class="menu">
<ul>
<li>
Lorem
</li>
<li>
Lorem
</li>
</ul>
<div class="img"> <!-- replace div with the <img src="" alt="" /> tag -->
img here
</div>
<ul>
<li>
Lorem
</li>
<li>
Lorem
</li>
</ul>
</div>
CSS
.menu {
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px 10px;
background: #ddd;
}
ul {
display: flex;
padding: 0;
list-style: none;
}
a {
padding: 5px 10px;
color: #000;
text-decoration: none;
text-transform: uppercase;
}
.img {
width: 200px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
background-color: #000;
color: #fff;
}
To optimize CSS when using very similar settings, use three classes instead of two: One common class for both elements which contains all the common settings, and two separate classes for the two elements which contain the different settings:
CSS:
.menu_all {
position: relative;
top: auto;
right: auto;
bottom: auto;
left: auto;
background: transparent;
width: auto;
opacity: 1;
visibility: visible;
-webkit-transition: none;
-moz-transition: none;
transition: none;
top: auto;
right: auto;
bottom: auto;
left: auto;
background: transparent;
}
.menu_left {
background: rgba(0, 0, 0, 0.85);
float: left;
}
.menu_right{
float: right;
}
and HTML:
<div class="menu_all menu_left" >
<div class="menu__item">
<span class="menu__item__link__text">SHOP</span>
</div>
<div class="menu__item">
<span class="menu__item__link__text">ABOUT</span>
</div>
</div>
<a href="" class="logo">
<img src="assets/sds.jpgf" class="logo__image">
</a>
<div class ="menu_all menu_right">
<div class="menu__item">
<span class="menu__item__link__text">CART</span>
</div>
<div class="menu__item">
<span class="menu__item__link__text">EUR</span>
</div>
</div>
I'm looking for a way to keep my fixed position images, which are placed in the background of the page via z-index, always in the center.
Right now they are in the center, but when I start scrolling, they don't move down the page and are therefore no longer centered.
I've tried setting the image container to fixed positioning with top and left 50%. I've also tried using 50vh and 50vw, and have tested other vh and vw values.
* { padding: 0; margin: 0; box-sizing: border-box; }
body {
padding: 30px;
}
li.track {
display: block;
font-size: 5rem;
margin-bottom: 15px;
}
li.track:hover {
color: transparent;
-webkit-text-stroke-color: #18181b;
-webkit-text-stroke-width: 3px;
}
li.track:hover .album-art {
display: block;
}
.album-art {
z-index: -10;
position: fixed;
left: 47vw;
top: 12vh;
width: 90%;
height: 50%;
max-height: 450px;
display: none;
opacity: 0.5;
overflow: hidden;
transform: translateX(-50%);
}
.album-art img {
width: 100%;
}
a {
text-decoration: none;
color: inherit;
}
<div class="tracks">
<ol>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/michael-nau.jpg"></div>
<p class="track-name">King Princess - 'Cheap Queen'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="hhttps://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/louie-short.jpg"></div>
<p class="track-name">Charly Bliss - 'Capacity'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="/https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/P4K-Tom-Waits-Jim-Jarmusch_PitchHeader.jpg"></div>
<p class="track-name">Julia Jacklin - 'Good Guy'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="/img/kevin-abstract.png"></div>
<p class="track-name">Kevin Abstract - 'Georgia'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="/img/okey-dokey.jpg"></div>
<p class="track-name">Okey Dokey - 'Wag Your Tail'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/fruit-bats.jpg"></div>
<p class="track-name">Devendra Banhart - 'Kantori Ongaku'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="/img/michael-nau.jpg"></div>
<p class="track-name">Michael Nau - 'Rides Through The Morning'</p>
</a>
</li>
<li class="track">
<a href="#" class="nav">
<div class="album-art"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/fruit-bats.jpg"></div>
<p class="track-name">Hop Along - 'Prior Things'</p>
</a>
</li>
<ol>
</div>
I want the images that appear when hovered to always be in the center of the page (scrolled or not).
try this css:
body {
padding: 30px;
}
li.track {
display: block;
font-size: 5rem;
margin-bottom: 15px;
}
li.track:hover {
color: transparent;
-webkit-text-stroke-color: #18181b;
-webkit-text-stroke-width: 3px;
}
li.track:hover .album-art {
display: block;
}
.album-art {
z-index: -10;
position: fixed;
left: 50%;
top: 50%;
width: 90%;
height: 50%;
max-height: 450px;
display: none;
opacity: 0.5;
overflow: hidden;
transform: translate(-50%,-50%);
}
.album-art img {
width: 100%;
}
a {
text-decoration: none;
color: inherit;
}
thank you
I would like to increase the height of my images to 400px. However the images don't fill the div while maintaining the aspect ratio.
I previously added height:100% to my images while adding a fixed height of 400px to my parent div, then adding object-fit: cover to the images. However, on page resize, the images do not maintain their ratio and instead squash / collapse.
Any help would be great. Thank you.
#test {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#test h2 {
font-family: 'Poppins';
text-transform: uppercase;
font-weight: 600;
font-size: 1.3em;
color: #333;
margin-bottom: 20px;
}
#picwrapper {
width: 85%;
}
#media only screen and (max-width: 986px) {
#picwrapper {
flex-direction: column;
}
}
#picwrapper {
display: flex;
flex-wrap: wrap;
}
.third {
width: 33.3333333333%;
position: relative;
}
#media only screen and (max-width: 986px) {
.third {
width: 100%;
}
}
.third img {
max-width: 100%;
height: auto;
display: block;
padding-top: 10%;
/* 4:3 Aspect Ratio */
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background: linear-gradient(rgba(25, 25, 25, 0.9), rgba(25, 25, 25, 0.9));
}
.overlay-text {
font-family: 'Poppins';
font-weight: 500;
}
.third:hover .overlay {
opacity: 1;
}
.overlay-text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<section id="test">
<div id="picwrapper">
<div class="third">
<img src="https://cdn.zmescience.com/wp-
content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-_August_31.jpg">
<a href="AUDI/audi.html">
<div class="overlay">
<h1 class="overlay-text">Parkash Sandhu</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://solarsystem.nasa.gov/system/news_items/main_images/853_ph3_waxing_gibbous_2k.jpg">
<a href="#">
<div class="overlay">
<h1 class="overlay-text">Flo Music</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://cdn.zmescience.com/wp-content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-_August_31.jpg">
<a href="#">
<div class="overlay">
<h1 class="overlay-text">British Athletics</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://solarsystem.nasa.gov/system/news_items/main_images/853_ph3_waxing_gibbous_2k.jpg">
<a href="AUDI/audi.html">
<div class="overlay">
<h1 class="overlay-text">Audi</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://cdn.zmescience.com/wp-
content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-
_August_31.jpg">
<a href="Virgin Atlantic">
<div class="overlay">
<h1 class="overlay-text">Virgin Atlantic</h1>
</div>
</a>
</div>
</div>
</section>
In what follows, I'm making some assumptions about what you're after.
I'm assuming that you want the images to maintain their aspect ratio (4:3) at all times, but still scale larger and smaller proportionally as the screen grows/shrinks.
Below, you'll find a different implementation of your code, but one that I think captures what you're going for. At least, maybe it'll get you going in the right direction.
(BTW, Credit to Andy Bell for this aspect ratio technique. See here: https://andy-bell.design/wrote/creating-an-aspect-ratio-css-utility/)
[class*="aspect-ratio-"] {
display: block;
position: relative;
width: 100%;
}
[class*="aspect-ratio-"] > * {
display: block;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.aspect-ratio-tv {
padding-top: 75%; /* 4:3 */
}
.gallery {
display: flex;
flex-wrap: wrap;
list-style: none;
padding: 0;
margin: 0;
}
.gallery {
width: 100%;
}
.gallery li {
flex-basis: 100%;
}
#media screen and (min-width: 580px) {
.gallery li {
flex-basis: 50%;
}
}
#media screen and (min-width: 960px) {
.gallery li {
flex-basis: 33.33333%;
}
}
.gallery img {
object-fit: cover;
}
.overlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: 0.5s ease;
background: linear-gradient(rgba(25, 25, 25, 0.9), rgba(25, 25, 25, 0.9));
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.overlay-text {
color: white;
font-size: 20px;
text-decoration: none;
}
.overlay:hover {
opacity: 1;
text-decoration: none;
}
<ul class="gallery">
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/phvbkGThElM/800x600" alt="A neon ferris wheel" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/H_mTtLykvKs/800x682" alt="A dimly lit drum kit" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/Hc42xXu_WOg/800x567" alt="Blueberries, close up" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/MfynxC5_tiU/800x999" alt="High angle waterfall" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/7ZTx1iA7a7Q/800x397" alt="Sunset coastal scence" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
<li>
<div class="aspect-ratio-tv">
<img src="https://source.unsplash.com/pRvy1j4aINE/800x785" alt="High angle shot of a recording studio" loading="lazy" />
<a href="#0" class="overlay">
<h1 class="overlay-text">
TEST HEADING
</h1>
</a>
</div>
</li>
</ul>
See here for a pen: https://codepen.io/anon/pen/oOeBOj
Dont specify a width, just height.
.moon
{
height: 100%;
}
Specifying a width and height will squash the images; just specify the height.
img {
height: 50%; /*Change this to what you want.*/
}
#test {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#test h2 {
font-family: 'Poppins';
text-transform: uppercase;
font-weight: 600;
font-size: 1.3em;
color: #333;
margin-bottom: 20px;
}
#media only screen and (max-width: 986px) {
#picwrapper {
flex-direction: column;
}
}
#picwrapper {
display: flex;
flex-wrap: wrap;
}
.third {
width: 33.3333333333%;
position: relative;
}
#media only screen and (max-width: 986px) {
.third {
width: 100%;
}
}
.third img {
height: auto;
display: block;
padding-top: 10%;
/* 4:3 Aspect Ratio */
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background: linear-gradient(rgba(25, 25, 25, 0.9), rgba(25, 25, 25, 0.9));
}
.overlay-text {
font-family: 'Poppins';
font-weight: 500;
}
.third:hover .overlay {
opacity: 1;
}
.overlay-text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<section id="test">
<div id="picwrapper">
<div class="third">
<img src="https://cdn.zmescience.com/wp-
content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-_August_31.jpg">
<a href="AUDI/audi.html">
<div class="overlay">
<h1 class="overlay-text">Parkash Sandhu</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://solarsystem.nasa.gov/system/news_items/main_images/853_ph3_waxing_gibbous_2k.jpg">
<a href="#">
<div class="overlay">
<h1 class="overlay-text">Flo Music</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://cdn.zmescience.com/wp-content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-_August_31.jpg">
<a href="#">
<div class="overlay">
<h1 class="overlay-text">British Athletics</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://solarsystem.nasa.gov/system/news_items/main_images/853_ph3_waxing_gibbous_2k.jpg">
<a href="AUDI/audi.html">
<div class="overlay">
<h1 class="overlay-text">Audi</h1>
</div>
</a>
</div>
<div class="third">
<img src="https://cdn.zmescience.com/wp-
content/uploads/2018/11/Magnificent_CME_Erupts_on_the_Sun_-
_August_31.jpg">
<a href="Virgin Atlantic">
<div class="overlay">
<h1 class="overlay-text">Virgin Atlantic</h1>
</div>
</a>
</div>
</div>
</section>
I created a small website using two flex containers and the result is good. Is it possible to achieve the same result by using only one flexbox container on the container class?
I have been trying to do it but it does not work and I don't want to have two flexbox containers at the same time.
/* Framework.css */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul {
padding: 0;
margin: 0;
list-style: none;
}
body {
font-family: 'Electrolize', sans-serif;
}
.container {
max-width: 940px;
margin: 0 auto;
padding: 0 5%;
}
.gallery {
display: flex;
}
.gallery li {
width: 200px;
background-color: #1c1c1c;
color: #bdc3c7;
margin: 0% 0.5% 0% 0.5%;
}
.gallery img {
width: 100%;
height: auto;
}
.gallery p {
margin: 0;
padding: 6%;
font-size: 1.25em;
background-color: #483636;
color: #bdc3c7;
text-align: center;
}
.galleryproducts {
display: flex;
}
.galleryproducts li {
width: 200px;
margin: 2%;
}
.galleryproducts img {
width: 100%;
height: auto;
border: 3px solid white;
}
.latest {
margin-top: 1%;
background-color: #1c1c1c;
}
.latest h1 {
color: white;
font-size: 1.5em;
font-weight: 300;
border-bottom: 3px solid white;
margin-bottom: 5%;
padding: 2%;
}
a {
text-decoration: none;
}
<!-- index.html -->
<div class="container">
<section class="boxes">
<ul class="gallery">
<li>
<a href="img/electrical.png">
<img src="img/electrical.png" alt="">
<p>Electrical Installations</p>
</a>
</li>
<li>
<a href="img/lighting.png">
<img src="img/lighting.png" alt="">
<p>Lighting Decorations</p>
</a>
</li>
<li>
<a href="img/homeappliances1.png">
<img src="img/homeappliances1.png" alt="">
<p>Electrical Appliances</p>
</a>
</li>
<li>
<a href="img/homeappliances2.png">
<img src="img/homeappliances2.png" alt="">
<p>Kitchen Appliances</p>
</a>
</li>
</ul>
</section>
<section class="latest">
<h1>Our latest products</h1>
<ul class="galleryproducts">
<li>
<a href="img/1.jpg">
<img src="img/1.jpg" alt="">
</a>
</li>
<li>
<a href="img/2.jpg">
<img src="img/2.jpg" alt="">
</a>
</li>
<li>
<a href="img/3.jpg">
<img src="img/3.jpg" alt="">
</a>
</li>
<li>
<a href="img/4.jpg">
<img src="img/4.jpg" alt="">
</a>
</li>
</ul>
</section>
</div>
No, you can't set display: flex on the containter an have the gallery's/galleryproducts's children behave as they do now, as they aren't direct children of the container.
If you would change it like that, the boxes/latest will become flex children and the gallery's/galleryproducts's children will just become normal li items, stacked on top of each other, not side by side.
So what you have is what you need, to get the result you say is good (if to assume you want to use flex of course).