How to styling picture in div? - css

So i making shop and i want to styling the image.
Right now i don't see the faces in the div:
I know it's little thing,but i can't make this happen:
Menu-item component:
import React from "react";
import "./menu-item.styles.scss";
const MenuItem = ({ title, imageUrl, size }) => (
<div className={`${size} menu-item`}>
<div
className="img"
style={{
backgroundImage: `url(${imageUrl})`
}}
/>
<div className="content">
<h1 className="title">{title}</h1>
</div>
</div>
);
export default MenuItem;
Menu-item styling:
.menu-item {
min-width: 30%;
height: 240px;
flex: 1 1 auto;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid black;
margin: 0 7.5px 10px;
border-radius: 10px;
.img {
width: 100%;
height: 100%;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
position: relative;
right: 0px;
border-radius: 10px;
}
&:hover {
.title {
transition: ease-in 0.6s;
opacity: 1;
cursor: pointer;
}
}
&.small-sneakers {
background-position: center;
}
&.large {
height: 420px;
}
&:first-child {
margin-right: 7.5px;
}
&:last-child {
margin-left: 7.5px;
}
.content {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
}
.title {
font-weight: bold;
margin-bottom: 6px;
font-size: 22px;
color: white;
text-transform: uppercase;
align-items: center;
opacity: 0;
z-index: 3;
position: absolute;
}
}
When i try to do background-position: top -50px center,then the image not responsive :/
Thanks for the help!

When you are using background-position: top -50px center you will break the intention of background-size: cover; because it (cover) will determine whether to "crop" the image horizontally or vertically based on the size of the image, so if the cropping is happening on the sides, 50px of the top of the image will be outside of its container.
You can manipulate the wanted behavour by setting overflow:hidden on the parent, set a negative margin to the top of the child, and setting the height of the child to 100% + [margin-top value] using calc.
example code on code pen here
.menu-item
min-width: 30%;
height: 240px;
flex: 1 1 auto;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid black;
margin: 0 7.5px 10px;
border-radius: 10px;
overflow: hidden;
}
.img {
margin-top: -50px;
width: 100%;
height: calc(100% + 50px);
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
background-image: url(https://image.cnbcfm.com/api/v1/image/106069136-1565284193572gettyimages-1142580869.jpeg?v=1576531407&w=1400&h=950);
}

Related

React Sidebar Goes Behind Background Image, but Over Navbar Title

Two issues-
I built a react sidebar, and for some reason whenever toggling it, the bar is behind the page's background image. I tried putting z-index: -1; on the background image, but then the image disappears (though the side bar finally shows again.) I have messed around with "position", with no success either. How can I get my sidebar to go over my background image?
The other issue is that whenever toggling the sidebar, it cover the navbar's title. Any suggestions on how to get my navbar's title to move to the side when the sidebar is being toggled?
enter image description here
enter image description here
.navbar {
align-items: center;
background-color: #060b26;
color: white;
display: flex;
height: 80px;
justify-content: start;
text-align: right;
}
.title {
margin-left: 20px;
}
.menu-bars {
margin-left: 2rem;
font-size: 2rem;
background: none;
}
.nav-menu {
background-color: #060b26;
width: 250px;
height: 100vh;
display: flex;
justify-content: center;
position: fixed;
top: 0;
left: -100%;
transition: 850ms;
}
.nav-menu.active {
left: 0;
position: absolute;
transition: 350ms;
}
.nav-text {
display: flex;
justify-content: start;
align-items: center;
padding: 8px 0px 8px 16px;
list-style: none;
height: 60px;
}
.nav-text a {
text-decoration: none;
color: #f5f5f5;
font-size: 18px;
width: 95%;
height: 100%;
display: flex;
align-items: center;
padding: 0 16px;
border-radius: 4px;
}
.nav-text a:hover {
background-color: #1a83ff;
}
.nav-menu-items {
width: 100%;
}
.navbar-toggle {
align-items: center;
background-color: #060b26;
display: flex;
height: 80px;
justify-content: start;
position: absolute;
width: 100%;
}
span {
margin-left: 16px;
}
.backgroundImage {
background-image: url('assets/dog.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
filter: blur(2px);
height: 100vh;
width: 100%;
}
.dashboardButton {
background: linear-gradient(
#fd585c,
#ec7278,
#ff5b69
);
border-radius: 8px;
color: #f5f5f5;
display: grid;
font-style: "Poppins", sans-serif;
height: 80px;
left: 43%;
letter-spacing: 3px;
margin: auto;
place-items: center;
position: absolute;
text-decoration: none;
top: 55%;
width: 200px;
}
.welcomeHeader {
color: #fd585c;
display: center;
font-size: 375%;
left: 25%;
position: absolute;
top: 35%
}
.welcomeDescription {
color: #f5f5f5;
font-size: 125%;
left: 20%;
position: absolute;
top: 47%;
}
I ended up putting:
backgroundImage {
z-index: 1;
}
nav-menu.active {
z-index: 2;
}
That solved the issue!

How to implement responsive mobile nav menu with Next.Js?

So, I'm a bit confused on how to implement a responsive mobile navigation in Next.js. My problem stems from handling the menu toggle state both via Javascript and CSS.
My nav menu follows the following tutorial from Log Rocket, but the methods employed in the tutorial were written for React and do not account for how Next.js applies CSS class names. The plain React example depends on dynamicaly updating the CSS, while it seems that most Next.js approaches suggest toggling the element with useState. This works, but I lose the granularity of being able to control the navigation through media queries and the toggle button.
My current implementation checks a boolean useState value to show the menu, which I want to only apply to the menu at 780px or lower. If I start with a state of false, the whole menu isn't rendered including the button to toggle it. If I start with a state of true, then the mobile menu is rendered as expanded automatically, which I want to prevent. This also resuls in the toggle removing the menu altogether when the user isn't on mobile (e.g. if the menu is untoggled, and the user goes to a large screen size.
So, the question becomes how to prevent this unintended behavior? Or how to set the mobile menu default state to be hidden while preserving the click to toggle functionality?
import { useState } from "react";
import Link from "next/Link";
import SiteLogo from "../components/CompLogo";
import navStyles from "../styles/Nav.module.css";
const Nav = () => {
const [isNavExpanded, setIsNavExpanded] = useState(true);
return (
<div>
<nav className={navStyles.nav}>
<SiteLogo />
<button
className={navStyles.hamburger}
onClick={() => {
setIsNavExpanded(!isNavExpanded);
console.log("clicked");
console.log(isNavExpanded);
}}
>
{/* icon from heroicons.com */}
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-5 w-5"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fillRule="evenodd"
d="M3 5a1 1 0 011-1h12a1 1 0 110
2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0
01-1- 1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
/>
</svg>
</button>
<div className={navStyles.navigation}>
{isNavExpanded && (
<ul className={navStyles.menu}>
<li className={navStyles.links}>
<Link href="/">
<a className={navStyles.anchors}>Home</a>
</Link>
</li>
<li className={navStyles.links}>
<Link href="/map">
<a className={navStyles.anchors}>Map Page</a>
</Link>
</li>
<li className={navStyles.links}>
<Link href="/about">
<a className={navStyles.anchors}>About</a>
</Link>
</li>
</ul>
)}
</div>
</nav>
</div>
);
};
export default Nav
Here's my css:
.one {
margin-top: 0;
height: 100vh;
width: 100%;
background-image: url("../../public/images/My\ project.jpg");
background-position: top;
background-repeat: no-repeat;
background-size: cover;
display: flex;
justify-content: left;
align-items: center;
}
.oneAppointment {
width: 55%;
margin-left: 10%;
text-align: left;
/* display: flex; */
/* justify-content: center; */
/* flex-direction: column; */
/* height: 40%; */
/* background-color: #fff; */
/* padding: 20px 40px; */
/* border-radius: 6px; */
}
.oneAppointment h1 {
font-size: 40px;
font-weight: 700;
/* color: #000; */
}
.abc {
font-size: 56px;
font-weight: 700;
/* color: blanchedalmond; */
}
.heading {
font-size: 40px;
font-weight: 700;
/* background-color: blueviolet; */
margin-bottom: 10px;
/* color: #000; */
}
.designer a {
margin-top: 16px;
}
.designer {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 520px;
width: 100%;
background-image: url("../../public/images/fas.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: contain;
/* background-color: blueviolet; */
}
.blogs {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 520px;
width: 100%;
background-image: url("../../public/images/fas.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: contain;
/* background-color: blueviolet; */
}
.collection {
margin-top: 0;
height: 90vh;
width: 100%;
/* background-image: url("./website/images/pexels-yan-krukov-4458418.jpg"); */
background-image: url("../../public/images/collec.jpg");
background-position: top;
background-repeat: no-repeat;
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
}
.collections {
width: 45%;
margin-left: 45%;
text-align: center;
margin-top: -100px;
}
.shop {
margin-top: 0;
height: 90vh;
width: 100%;
/* background-image: url("./website/images/pexels-yan-krukov-4458418.jpg"); */
background-image: url("../../public/images/shopp.jpg");
background-position: top;
background-repeat: no-repeat;
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
}
.shopContent {
width: 45%;
margin-left: 45%;
text-align: center;
margin-top: -100px;
}
#media only screen and (min-width: 1px) and (max-width: 768px) {
.heading {
font-size: 36px;
font-weight: 700;
margin-bottom: 10px;
}
.one {
margin-top: 0;
background-image: url("../../public/images/mmy.jpg");
background-position: left;
}
.oneAppointment {
width: 30%;
margin-left: 5%;
text-align: left;
margin-top: -80px;
}
.oneAppointment h1 {
font-size: 28px;
font-weight: 700;
line-height: 55px;
}
.abc {
font-size: 40px;
font-weight: 700;
/* color: blanchedalmond; */
}
.designer {
text-align: center;
height: 600px;
width: 90%;
margin-left: 5%;
background-position: left;
background-size: cover;
}
.blogs {
margin-top: 100px;
text-align: center;
height: 600px;
width: 90%;
margin-left: 5%;
background-position: left;
background-size: cover;
}
.collection {
/* margin-top: 0;
height: 90vh;
width: 100%; */
/* background-image: url("./website/images/pexels-yan-krukov-4458418.jpg"); */
/* background-image: url("../public/images/collec.jpg"); */
background-image: url("../../public/images/colle.jpg");
background-position: left;
/* background-position: calc(-10px); */
/* background-repeat: no-repeat;
background-size: cover;
display: flex; */
justify-content: left;
/* align-items: center; */
}
.collections {
width: 70%;
margin-left: 25%;
/* background-color: aquamarine; */
text-align: right;
margin-top: -140px;
}
.shop {
margin-top: -100px;
/* margin-top: 0;
height: 90vh;
width: 100%; */
/* background-image: url("./website/images/pexels-yan-krukov-4458418.jpg"); */
/* background-image: url("../public/images/shopp.jpg"); */
background-position: calc(-280px);
/* background-repeat: no-repeat; */
/* background-size: cover; */
/* display: flex; */
justify-content: left;
/* align-items: center; */
}
.shopContent {
width: 70%;
margin-left: 25%;
/* background-color: chartreuse; */
text-align: right;
margin-top: 0px;
}
}
.nav {
background-color: white;
color: black;
width: 100%;
display: flex;
align-items: center;
position: relative;
padding: 0.5rem 0 rem;
}
.navigation {
margin-left: auto;
}
.menu {
display: flex;
padding: 0;
}
.links {
list-style-type: none;
margin: 0 1rem;
}
.links a {
text-decoration: none;
display: block;
width: 100%;
}
.hamburger {
border: 0;
height: 80px;
width: 80px;
padding: 0.5rem;
border-radius: 50%;
background-color: white;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
position: absolute;
top: 50%;
right: 5px;
transform: translateY(-50%);
display: none;
}
#media screen and (max-width: 768px) {
.hamburger {
display: block;
}
.menu {
position: absolute;
top: 146px;
left: 0;
flex-direction: column;
width: 100%;
height: calc(100vh - 147px);
background-color: white;
border-top: 1px solid black;
}
.links {
text-align: center;
margin: 0;
}
.links a {
color: black;
width: 100%;
padding: 1.5rem 0;
}
.links:hover {
background-color: #eee;
}
.menu.expanded {
display: block;
}
}
Not Going By your code but a general guide -
*Important - this is only for your understanding and may not be exactly the same as what your code is.
For you, the width is 780px where you want to check and set mobile menu and desktop menu.
Step 1 - you will have to check the width of the screen, you can do this with CSS media screen width but then your component will load all the data for the mobile menu as well as the desktop menu, so you can write a eventlistener of width change and do something as, (suppose width is a state) -
{width > 780px ? <DesktopMenu /> : <MobileMenu /> }
Now you can lazy load both components.
Now coming to the mobile menu part -
The "Open Menu Drawer Button" should always be visible by default on mobile screen (toggle button to open).
Write a click event on openMenuDrawerButton to open a drawer that actually holds the menu layout and use CSS for slide effect or anything.
again use a state to check whether a drawer is open or not lets call this state as visibleDrawer and then you toggle this state with openMenuDrawerButton {visibleDrawer : true} and also with close button present in the drawer itself.
{!visibleDrawer && < OpenMenuDrawerButton /> }
to hide the opendrawerbutton after you have clicked it (opened the drawer).
once the drawer open, show a close button on drawer which will hide the drawer {visibleDrawer : false} and make OpenMenuDrawerButton appear at its place.
Do write if you need more explanation.
I use the following trick to hide/reveal appropriate elements using CSS media queries, which runs before any state is set (hence "default" behavior). I use MaterialUI, hope you can adapt to your needs:
{/* Mobile Version */}
<Hidden smUp implementation="css">
...
</Hidden>
{/* Desktop Version */}
<Hidden xsDown implementation="css">
...
</Hidden>
You can apply additional logic in useState for any required set up post the default loading.

css when hover show multiple color around div using css or javascript

I use css to show colors around div when hover but I want to make it more than one color live demo I want it to be multiple colors instead of one
.container{
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
}
.container a{
position: relative;
width: 200px;
height: 60px;
display: inline-block;
background: #fff;
margin: 20px;
}
.container a::before,
.container a::after
{
content: '';
position: absolute;
inset: 0;
background: rgb(26, 214, 58);
transition: 0.5s;
}
.container a:hover:before
{
inset: -3px;
}
<style>
#import url('https://fonts.googleapis.com/css2?family=Zilla+Slab:ital,wght#1,300&display=swap');
body {
background-color: #0e1538;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
a {
text-decoration: none;
}
.btn-wrapper {
width: 200px;
height: 60px;
}
.btn {
font-family: 'Zilla Slab', serif;
font-size: 2em;
color: #ffffff;
background-color: #0e1538;
width: 190px;
height: 50px;
}
.center-flex {
display: flex;
justify-content: center;
align-items: center;
}
.btn-wrapper:hover {
background-image: linear-gradient(90deg, #00C0FF 0%, #FFCF00 49%, #FC4F4F 100%);
}
</style>
<body>
<div class="btn-wrapper center-flex">
Hover Me</div>
</div>
</body>
Instead of having a background set as a single RGB color you can set it as a background-image with multi colors introduced through a gradient.
This snippet uses a linear-gradient (which gives a sort of image type) with just 3 colors but you can have more and you can adjust how much of each you get and whether the changes go from top to bottom or left to right - see MDN documentation for the various options.
The result here is:
#import url('https://fonts.googleapis.com/css2?family=Zilla+Slab:ital,wght#1,300&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Zilla Slab', serif;
}
body {
display: flex;
justify-content: center;
min-height: 100vh;
background: #0e1538;
}
.container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
}
.container a {
position: relative;
width: 200px;
height: 60px;
display: inline-block;
background: #fff;
margin: 20px;
}
.container a::before,
.container a::after {
content: '';
position: absolute;
inset: 0;
background: rgb(26, 214, 58);
background-image: linear-gradient(red, green, blue);
/* ADDED */
transition: 0.5s;
}
.container a:hover:before {
inset: -3px;
}
.container a span {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: inline-block;
background: #0e1538;
z-index: 10;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
color: #fff;
}
<div class="container">
<a href="#">
<span>Hover Me</span>
</a>
</div>

scale a div properly on all devices

How is it possible to scale a div element correctly to fit into a specific area? I have a div element with that I want to scale on hover to fit into the circle in the middle (see code snippet). Now I can set the scaling values but this will not work correctly on smaller or bigger screens. The div with the coral color should have 100vw.
The following is my HTML, css an jquery:
$(".contact-button").hover(function(){
$(".holdeer").addClass('contact');
}, function(){
$(".holdeer").removeClass('contact');
});
.getintouch {
border-top-left-radius: 0px;
border-top-right-radius: 0px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
height: 20vh;
width: 100vw;
}
.contact-us {
position: relative;
z-index: 0;
display: flex;
height: 700px;
width: 100vw;
flex-direction: column;
justify-content: space-around;
align-items: stretch;
background-color: transparent;
}
.contact-button {
position: absolute;
z-index: 100;
display: flex;
width: 200px;
height: 200px;
margin-top: 0px;
flex-direction: row;
justify-content: center;
align-items: center;
align-self: center;
flex-grow: 0;
flex-shrink: 0;
flex-basis: auto;
border-top-style: solid;
border-top-width: 1px;
border-right-style: solid;
border-right-width: 1px;
border-bottom-style: solid;
border-bottom-width: 1px;
border-left-style: solid;
border-left-width: 1px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
border-bottom-left-radius: 50%;
border-bottom-right-radius: 50%;
background-color: transparent;
color: blue;
font-size: 32px;
font-weight: 700;
}
.holdeer {
position: absolute;
left: 0%;
top: 0%;
right: 0%;
bottom: 0%;
display: block;
width: auto;
height: auto;
justify-content: center;
align-items: center;
align-self: auto;
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
background-color: coral;
}
.contact {
animation: shrink 1s ease forwards;
transition: all 1s;
}
#keyframes shrink {
from {
border-radius: 100%;
}
to{
border-radius: 100%;
transform: scale(0.15, 0.28);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="getintouch">
<div class="contact-us">
<div class = "contact-button"></div>
<div class="holdeer"></div>
</div>
</section>
You can do that with much easier & cleaner code, also with just css.
.contact-us {
overflow: hidden; /* to hide anything that goes outside */
display: flex;
height: 700px;
width: 100vw;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: transparent;
}
.contact-button {
position: relative; /* to make 'absolute' children relative to it */
display: flex;
width: 200px;
height: 200px;
flex-direction: row;
justify-content: center;
align-items: center;
border: 1px solid;
border-radius: 50%;
transition: all 1s;
}
.holdeer {
position: absolute;
z-index: -1;
display: block;
width: 100%; /* takes full width of parent */
height: 100%; /* takes full height of parent */
border-radius: 50%;
transform: scale(15); /* scale to very large value to cover .contact-us and overflow: hidden with cut off what goes outside */
transition: all 1s;
background-color: coral;
pointer-events: none; /* to disable hover on this child */
}
.contact-button:hover .holdeer {
transform: scale(1); /* normal scale to fit the parent circle */
}
<div class="contact-us">
<div class="contact-button"><div class="holdeer"></div></div>
</div>

How to center object with CSS transform scale

I'm trying to implement a zoom in/out functionality, just like you would have on a document editor like google docs or any word processor application. The problem I'm having is that I cannot keep the "document" centered and also be able to scroll all of its parts into view. Here is a small demonstration of the problem: https://codepen.io/liviu_vasut/pen/dyGbwwO
document.getElementById('objectToScale').style.transform = "scale(3)";
.container {
display: flex;
align-items: center;
justify-content: center;
padding: 5px;
width: 300px;
height: 200px;
border: 1px solid #000000;
left: 0px;
top: 0px;
margin: 10px;
overflow: auto;
}
.object {
position: relative;
width: 120px;
height: 120px;
display: inline-block;
background-color: grey;
border-radius: 25px;
padding: 5px;
transform-origin: center;
transform: scale(1);
}
<div class="container">
<div id="objectToScale" class="object">x</div>
</div>
Thanks for your time.
You scale only some inner element inside the whole box, but expect see the whole box scaled. if you want to scale the white padding and all the inner content to stay visible (and be able to scroll to) you should add some wrapper inside with width: 100% and height: 100%, and scale it, so the whole content become scaled.
Also, as #HaoWu mentioned, you should set the transform-origin to 0 0.
The final product should look somewhat like this:
var scaled = false;
function toggleScale() {
var objects = document.getElementsByClassName('wrapper');
for (var i = 0; i < objects.length; i++) {
objects[i].style.transform = scaled ? 'scale(1)' : 'scale(3)';
}
scaled = !scaled;
}
.container {
width: 300px;
height: 200px;
border: 1px solid #000000;
left: 0px;
top: 0px;
margin: 10px;
overflow: auto;
}
.wrapper {
width: 100%;
height: 100%;
display: flex;
box-sizing: border-box;
align-items: center;
justify-content: center;
padding: 5px;
transform-origin: 0 0;
}
.object {
position: relative;
width: 120px;
height: 120px;
display: inline-block;
background-color: grey;
border-radius: 25px;
padding: 5px;
transform-origin: center;
transform: scale(1);
}
<input type="button" onclick="toggleScale()" value="Toggle Scale" />
<div class="container">
<div class="wrapper">
<div id="objectToScale" class="object">x</div>
</div>
</div>
Actually, use the transform-origin: 0 0; and manually set the scrollbar to the center:
var scaled = false;
function toggleScale() {
[...document.getElementsByClassName('object')].forEach(e => {
e.classList.toggle('scaled');
e.parentElement.scrollTop = (e.parentElement.scrollHeight - e.parentElement.clientHeight) / 2;
e.parentElement.scrollLeft = (e.parentElement.scrollWidth - e.parentElement.clientWidth) / 2;
});
}
* {
box-sizing: border-box;
}
.container {
display: flex;
align-items: center;
justify-content: center;
padding: 5px;
width: 300px;
height: 200px;
border: 1px solid #000000;
left: 0px;
top: 0px;
margin: 10px;
overflow: auto;
}
.object {
position: relative;
width: 120px;
height: 120px;
display: inline-block;
background-color: grey;
border-radius: 25px;
padding: 5px;
transform-origin: 0 0;
}
.scaled {
transform: scale(3);
}
<input type="button" onClick="toggleScale()" value="Toggle Scale" />
<div class="container">
<div class="object">cannot see the entire object when scaled</div>
</div>

Resources