I would like to imitate the effect and output that I have on my #boxLeft, but when I do so, and add a css3 transition, it fails. I had to use fix it with a -100px margin-top value to hide it. Could anyone help?
HTML and CSS:
body {
padding: 0;
margin: 0;
}
p, h1, h2, h3, h4, h5, h6 {
padding: 0;
margin: 0;
}
#box {
width: 100%;
height: 100px;
background-color: #636363;
}
.boxLeft,
.boxRight {
width: 50%;
height: 100px;
/* display: inline-block; */
float: left;
padding: 10px;
box-sizing: border-box;
z-index: 1;
overflow: hidden;
}
.boxLeft {
background-color: red;
}
.blHeader {
text-align: right;
}
.boxLeft:hover {
background-color: orange;
width: 100%;
transition: .5s;
}
.boxLeft:hover ~.boxRight {
display: none;
}
.boxLeft:hover > .blHeader {
text-align: left;
}
.boxLeft:hover > .blCon {
display: block;
}
.blCon,
.brCon {
display: none;
}
.boxRight {
background-color: blue;
}
.boxRight:hover {
background-color: green;
width: 100%;
transition: .5s;
margin-top: -100px;
}
.boxRight:hover~.boxLeft {
display: none;
}
.boxRight:hover > .brHeader {
text-align: left;
}
.boxRight:hover > .brCon {
display: block;
transition: .5s;
}
<div id="box">
<div class="boxLeft">
<h2 class="blHeader">Left Header</h2>
<p class="blCon">Left content Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa harum recusandae perferendis animi, assumenda iste, repudiandae temporibus. Magni earum dicta perspiciatis tempore consequatur necessitatibus ullam recusandae dolore reiciendis,
repudiandae cumque!</p>
</div>
<div class="boxRight">
<h2 class="brHeader">RIght Header</h2>
<p class="brCon">Right content Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa harum recusandae perferendis animi, assumenda iste, repudiandae temporibus. Magni earum dicta perspiciatis tempore consequatur necessitatibus ullam recusandae dolore reiciendis,
repudiandae cumque!</p>
</div>
</div>
I'm sure I'm just missing out on something, but I've had a crack at it for hours. Thanks in advance. Here's a pen link: http://goo.gl/l2eqG0
The selector .boxRight:hover ~ .boxLeft isn't working as expected because the general sibling combinator, ~, only selects following sibling elements. Since the element .boxLeft precedes the element .boxRight, nothing is selected and the element .boxLeft isn't hidden when hovering over the element .boxRight.
To work around this, one option is to hide the first element when hovering over the parent #box element.
#box:hover .boxLeft {
display: none;
}
Then you can override this when actually hovering over the first element:
#box .boxLeft:hover {
display: block;
}
This works because there are only two elements. If you're not hovering over the first element, you can assume that you are otherwise hovering over the second element (which means that imitate the general sibling combinator since you can style the first element when hovering over the parent).
Updated Snippet:
body {
padding: 0;
margin: 0;
}
p, h1, h2, h3, h4, h5, h6 {
padding: 0;
margin: 0;
}
#box {
width: 100%;
height: 100px;
background-color: #636363;
}
.boxLeft,
.boxRight {
width: 50%;
height: 100px;
/* display: inline-block; */
float: left;
padding: 10px;
box-sizing: border-box;
z-index: 1;
overflow: hidden;
}
.boxLeft {
background-color: red;
}
.blHeader {
text-align: right;
}
.boxLeft:hover {
background-color: orange;
width: 100%;
transition: .5s;
}
.boxLeft:hover ~.boxRight {
display: none;
}
.boxLeft:hover > .blHeader {
text-align: left;
}
.boxLeft:hover > .blCon {
display: block;
}
.blCon,
.brCon {
display: none;
}
.boxRight {
background-color: blue;
float: right;
}
.boxRight:hover {
background-color: green;
width: 100%;
transition: .5s;
}
#box:hover .boxLeft {
display: none;
}
#box .boxLeft:hover {
display: block;
}
.boxRight:hover > .brHeader {
text-align: left;
}
.boxRight:hover > .brCon {
display: block;
transition: .5s;
}
<div id="box">
<div class="boxLeft">
<h2 class="blHeader">Left Header</h2>
<p class="blCon">Left content Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa harum recusandae perferendis animi, assumenda iste, repudiandae temporibus. Magni earum dicta perspiciatis tempore consequatur necessitatibus ullam recusandae dolore reiciendis,
repudiandae cumque!</p>
</div>
<div class="boxRight">
<h2 class="brHeader">RIght Header</h2>
<p class="brCon">Right content Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa harum recusandae perferendis animi, assumenda iste, repudiandae temporibus. Magni earum dicta perspiciatis tempore consequatur necessitatibus ullam recusandae dolore reiciendis,
repudiandae cumque!</p>
</div>
</div>
However, a more flexible option would be to remove the elements .boxLeft and .boxRight from the normal flow by absolutely position them. In doing so, you can avoid hiding the corresponding sibling elements on hover:
Updated Snippet:
body {
padding: 0;
margin: 0;
}
p, h1, h2, h3, h4, h5, h6 {
padding: 0;
margin: 0;
}
#box {
width: 100%;
height: 100px;
background-color: #636363;
}
.boxLeft,
.boxRight {
width: 50%;
height: 100px;
position: absolute;
top: 0;
padding: 10px;
box-sizing: border-box;
z-index: 1;
overflow: hidden;
}
.boxLeft {
background-color: red;
}
.blHeader {
text-align: right;
}
.boxLeft:hover {
background-color: orange;
width: 100%;
transition: .5s;
}
.boxLeft:hover ~ .boxRight {
z-index: auto;
}
.boxLeft:hover > .blHeader {
text-align: left;
}
.boxLeft:hover > .blCon {
display: block;
}
.blCon,
.brCon {
display: none;
}
.boxRight {
background-color: blue;
right: 0;
}
.boxRight:hover {
background-color: green;
width: 100%;
transition: .5s;
}
.boxRight:hover > .brHeader {
text-align: left;
}
.boxRight:hover > .brCon {
display: block;
transition: .5s;
}
<div id="box">
<div class="boxLeft">
<h2 class="blHeader">Left Header</h2>
<p class="blCon">Left content Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa harum recusandae perferendis animi, assumenda iste, repudiandae temporibus. Magni earum dicta perspiciatis tempore consequatur necessitatibus ullam recusandae dolore reiciendis,
repudiandae cumque!</p>
</div>
<div class="boxRight">
<h2 class="brHeader">RIght Header</h2>
<p class="brCon">Right content Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa harum recusandae perferendis animi, assumenda iste, repudiandae temporibus. Magni earum dicta perspiciatis tempore consequatur necessitatibus ullam recusandae dolore reiciendis,
repudiandae cumque!</p>
</div>
</div>
Related
I have created navbar with scroll effect but i face one problem with mobile responsive view. Please guide me how to solve this problem.
I want to place bottom nav-list below the navbar in mobile view while click on hamburger icon. So you can't see the below nav-list. I tried to put z-index in nav-list but it doesn't work, How do I do this?
const navbar = document.getElementById("navbar");
const navbarContainer = document.getElementById("navbar-container");
const navRight = document.querySelector('#navbar .right');
var prevScrollPos = window.pageYOffset;
window.addEventListener("scroll", () => {
var currScrollPos = window.pageYOffset;
if (prevScrollPos > currScrollPos) {
navbarContainer.style.top = "0";
if (currScrollPos !== 0) {
navbarContainer.classList.add("shadow");
navbar.style.padding = "1rem 6rem";
navRight.style.top = '61px';
} else {
navbarContainer.style.position = "absolute";
navbarContainer.classList.remove("shadow");
navbar.style.padding = "2rem 6rem";
navRight.style.top = '81px';
}
} else if (currScrollPos > navbarContainer.offsetHeight) {
navbarContainer.style.top = `-${navbarContainer.offsetHeight + 5}px`;
navbarContainer.style.position = "fixed";
if (currScrollPos <= navbarContainer.offsetHeight + 5) {
navbarContainer.style.transition = "none";
} else {
navbarContainer.style.transition =
"top 0.6s ease-in-out, box-shadow 0.3s cubic-bezier(1, 1, 1, 1)";
}
}
prevScrollPos = currScrollPos;
});
// mobile view
const hamBurger = document.getElementById("hamburger");
hamBurger.addEventListener("click", () => {
hamBurger.classList.toggle("is-active");
if (hamBurger.classList.contains("is-active")) {
navRight.style.transform = `translateY(0%)`;
} else {
navRight.style.transform = `translateY(-100%)`;
}
});
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: sans-serif;
}
html {
font-size: 62.5%;
/* 1rem = 10px */
}
body {
background-color: rgb(241, 241, 241);
}
.container {
max-width: 120rem;
margin: 0 auto;
padding: 6rem;
}
a {
text-decoration: none;
}
#navbar-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
background-color: rgb(241, 241, 241);
z-index: 9999;
transition: top 0.6s ease-in-out, box-shadow 0.3s cubic-bezier(1, 1, 1, 1);
}
#navbar-container.shadow {
box-shadow: rgba(0, 0, 0, 0.15) 1.95px 1.95px 2.6px;
}
#navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 6rem;
transition: all 0.3s cubic-bezier(1, 1, 1, 1);
padding: 2rem 6rem;
}
.site-logo a {
font-size: 3.2rem;
font-weight: 800;
position: relative;
color: #000000;
transition: color 0.3s cubic-bezier(1, 1, 1, 1);
}
.site-logo a::after {
content: "";
position: absolute;
top: 55%;
left: 110%;
width: 6px;
height: 6px;
background-color: #000000;
transition: background-color 0.3s cubic-bezier(1, 1, 1, 1);
}
#navbar .right {
display: flex;
justify-content: center;
align-items: center;
gap: 2.5rem;
}
#navbar ul {
display: flex;
justify-content: center;
align-items: center;
gap: 3rem;
}
ul li {
font-size: 1.8rem;
font-family: var(--local-font);
font-weight: 500;
list-style: none;
}
ul li a {
position: relative;
color: #000000;
}
/* ==========
Nav hamburger
============== */
#hamburger {
display: none;
}
#hamburger .line {
width: 30px;
height: 3px;
background-color: #000000;
display: block;
margin: 8px auto;
-webkit-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
#hamburger:hover {
cursor: pointer;
}
#hamburger.is-active .line:nth-child(2) {
opacity: 0;
}
#hamburger.is-active .line:nth-child(1) {
-webkit-transform: translateY(11px) rotate(45deg);
-ms-transform: translateY(11px) rotate(45deg);
-o-transform: translateY(11px) rotate(45deg);
transform: translateY(11px) rotate(45deg);
}
#hamburger.is-active .line:nth-child(3) {
-webkit-transform: translateY(-11px) rotate(-45deg);
-ms-transform: translateY(-11px) rotate(-45deg);
-o-transform: translateY(-11px) rotate(-45deg);
transform: translateY(-11px) rotate(-45deg);
}
#media (max-width: 768px) {
#hamburger {
display: block;
}
#navbar {
position: relative;
color: #ffffff;
}
#navbar .right {
position: absolute;
top: 81px;
right: 0;
width: 100%;
background-color: rgb(252, 70, 70);
flex-direction: column;
align-items: flex-start;
padding: 3rem 6rem 6rem 6rem;
gap: 3.5rem;
transform: translateY(-100%);
z-index: -999;
overflow: hidden;
transition: transform 400ms ease 0s;
}
#navbar .right .nav-list {
flex-direction: column;
align-items: flex-start;
gap: 3.5rem;
}
}
section p {
font-size: 3rem;
text-align: justify;
margin-top: 5rem;
}
<header id="navbar-container">
<nav id="navbar" class="container">
<div class="site-logo">Hi</div>
<div class="right">
<ul class="nav-list">
<li class="nav-btn">
<a href="./">
<p class="nav-button-text">Home</p>
</a>
</li>
<li class="nav-btn">
<a href="#">
<p class="nav-button-text">Resume</p>
</a>
</li>
<li class="nav-btn">
<a href="#">
<p class="nav-button-text">Contact</p>
</a>
</li>
</ul>
</div>
<div id="hamburger">
<span class="line"></span>
<span class="line"></span>
<span class="line"></span>
</div>
</div>
</nav>
</header>
<section class="container">
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Distinctio saepe facilis cumque laudantium! Atque, delectus ipsum esse obcaecati optio eum quibusdam saepe, sapiente rem fugiat iure reprehenderit totam odit? Temporibus, asperiores perferendis.
Ducimus, asperiores. Pariatur in alias doloremque delectus esse reprehenderit non odio iusto, qui vero tempore ducimus odit sapiente dolore distinctio explicabo quisquam natus ratione obcaecati. Eos sint quae explicabo dolores et enim! Fuga labore
quaerat optio eius tempora animi saepe nisi debitis corporis harum. Assumenda, cupiditate sunt excepturi architecto distinctio ipsa natus explicabo officia autem modi id possimus? Incidunt quod odit expedita totam, distinctio qui quasi laudantium
possimus? Odio inventore facilis, expedita totam beatae dolores cupiditate enim tempore? Sequi rerum corporis nam labore sapiente amet ex porro. Dicta libero officiis quis molestiae nobis! Exercitationem quis doloribus maiores earum similique, eveniet
numquam praesentium vel quae rem dolore eum, odit beatae illo atque in consequuntur commodi omnis autem dolorem, quidem consequatur eius unde temporibus! Natus rerum neque molestias totam, consequatur nemo quia obcaecati repellat doloremque quod,
iure fugit ducimus consectetur porro, voluptas quibusdam dicta maiores ipsam dolore eveniet. Dolore, ducimus. Officia eos incidunt perspiciatis hic delectus! Ad error, ducimus expedita, numquam modi, dolor tempora harum itaque facere possimus deleniti
placeat.
</p>
</section>
It is better to create a new menu during adaptation and give it a z-index smaller than the header. Because right now your <ul class="nav-list> is in navbar, so nav-list will be hard to become either
my html:-
```
<!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>i-Educate - New way to learn</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<nav class="navbar background">
<div class="navbar-left">
<img src="logo.jpg" alt="" id="logo">
<ul class="nav-list">
<li>Home</li>
<li>Services</li>
<li>About</li>
<li>Contact Us</li>
</ul>
</div>
<div class="navbar-right">
<input type="text" name="search" id="search" placeholder="Search here">
<button class="btn-search">Search</button>
</div>
</nav>
<section class="container background">
<div class="para">
<h1>This is the modern way of learning</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore magni, ut optio dolorum dicta minus repudiandae fuga itaque veniam corrupti nulla, cum fugit eveniet repellendus. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Reprehenderit
facere, vel consequuntur deserunt iusto inventore voluptas eaque atque tempora. Corporis voluptatibus, sed aliquid iure distinctio, ipsa perferendis necessitatibus quam, vero excepturi rem ratione recusandae. Quo nemo consequatur blanditiis
neque eos commodi quam debitis doloribus nostrum, numquam suscipit ipsa vitae veritatis?</p>
<div class="buttons">
<button class="btn">Subscribe</button>
<button class="btn">Watch here</button>
</div>
</div>
<div class="img">
<img src="logo.jpg" alt="laptop">
</div>
</section>
<section class="content">
<h1 class="heading">About us</h1>
<div class="main-box">
<div class="text">
<h1>This is a heading</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus dignissimos quibusdam quos laborum aliquid repellendus minima iure est temporibus voluptatibus voluptas possimus fuga vel, molestiae at hic quo necessitatibus voluptatum excepturi
animi rem doloribus omnis tempore. Optio sapiente quo, architecto dolore hic ea molestiae at adipisci consectetur libero quasi possimus? Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus velit perspiciatis pariatur earum
debitis blanditiis nemo, harum repell </p>
</div>
<div class="photo">
<img src="https://source.unsplash.com/1600x900/?books , student" alt="">
</div>
</div>
<div class="middle-box">
<div class="text">
<h1>This is a heading</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus dignissimos quibusdam quos laborum aliquid repellendus minima iure est temporibus voluptatibus voluptas possimus fuga vel, molestiae at hic quo necessitatibus voluptatum excepturi
animi rem doloribus omnis tempore. Optio sapiente quo, architecto dolore hic ea molestiae at adipisci consectetur libero quasi possimus? Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus velit perspiciatis pariatur earum
debitis blanditiis nemo, harum repell </p>
</div>
<div class="photo">
<img src="https://source.unsplash.com/700x900/?classroom , education" alt="">
</div>
</div>
<div class="main-box">
<div class="text">
<h1>This is a heading</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus dignissimos quibusdam quos laborum aliquid repellendus minima iure est temporibus voluptatibus voluptas possimus fuga vel, molestiae at hic quo necessitatibus voluptatum excepturi
animi rem doloribus omnis tempore. Optio sapiente quo, architecto dolore hic ea molestiae at adipisci consectetur libero quasi possimus? Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus velit perspiciatis pariatur earum
debitis blanditiis nemo, harum repell </p>
</div>
<div class="photo">
<img src="https://source.unsplash.com/1600x900/?coding" alt="">
</div>
</div>
</section>
<section class="contact">
<h1>Contact Us</h1>
<div class="contact-box">
<input type="text" name="name" id="name" placeholder="Enter Your Name">
<input type="email" name="email" id="email" placeholder="Enter Your email">
<input type="text" name="address" id="address" placeholder="Enter Your address">
<textarea name="query" id="query" cols="30" rows="10" placeholder="Enter your query"></textarea>
<button class="contact-btn">Submit</button>
</div>
</section>
<footer class="footer">
copyright preserved
</footer>
</body>
</html>
```
my style.css(styling+mediaQuery):-
* {
margin: 0;
padding: 0;
}
#logo {
width: 50px;
height: 50px;
margin-left: 5px;
border: 2px solid black;
border-radius: 50px;
}
.navbar {
display: flex;
position: sticky;
top: 0;
}
.navbar .navbar-left {
display: flex;
/* background-color: red; */
width: 50%;
}
.navbar .navbar-left .nav-list {
display: flex;
align-items: center;
justify-content: center;
}
.nav-list li {
list-style: none;
padding: 5px 5px;
}
.nav-list li a {
text-decoration: none;
cursor: pointer;
color: white;
padding: 5px 15px;
font-size: 17px;
}
.nav-list li a:hover {
background-color: white;
color: black;
}
.background {
background: rgba(0, 0, 0, 0.1) url('../bg.jpg');
background-size: cover;
background-blend-mode: darken;
}
.navbar .navbar-right {
width: 50%;
/* display: flex; */
/* background-color: purple; */
text-align: right;
padding-top: 15px;
padding-right: 10px;
}
.navbar .navbar-right input {
width: 20%;
border: 2px solid rgb(26, 8, 26);
padding: 2px 2px;
border-radius: 5px;
}
.btn-search {
padding: 2px 2px;
border: 2px solid black;
border-radius: 5px;
cursor: pointer;
}
.container {
display: flex;
height: 700px;
/* border: 2px solid red; */
}
.container .para {
/* background-color: red; */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 65%;
padding: 5px 70px;
margin-bottom: 10px;
color: white;
}
.para h1 {
font-size: 30px;
padding: 20px 0px;
}
.para p {
font-size: 18px;
margin-left: 160px;
}
.container .img {
display: flex;
/* background-color: purple; */
width: 35%;
/* justify-content: center; */
align-items: center;
}
.container .img img {
width: 150px;
height: 150px;
border: 2px solid black;
border-radius: 150px;
}
.buttons {
/* border: 2px solid green; */
width: 65%;
margin-top: 20px;
}
.buttons .btn {
padding: 5px;
font-size: 15px;
margin: 0px 3px;
border: 2px solid black;
color: white;
border-radius: 10px;
background: none;
}
.buttons .btn:hover {
background-color: white;
color: black;
}
.content {
/* border: 2px solid green; */
height: 83vh;
background-color: #f2d8b6;
}
.content .heading {
text-align: center;
font-size: 35px;
/* margin-top: 5px; */
}
.content .main-box {
border: 2px solid green;
height: 150px;
display: flex;
width: 70%;
margin: auto;
margin-top: 21px;
border-radius: 10px;
margin-bottom: 50px;
}
.content .middle-box {
border: 2px solid green;
border-radius: 10px;
height: 150px;
display: flex;
width: 70%;
margin: auto;
margin-top: 21px;
margin-bottom: 50px;
flex-direction: row-reverse;
}
.middle-box img {
padding-left: 10px;
}
.content .text {
padding: 5px 10px;
font-size: 17px;
}
.content .photo img {
width: 130px;
height: 130px;
padding-right: 20px;
padding-top: 10px;
/* border: 2px solid black; */
}
.contact {
background-color: rgb(255, 241, 241);
}
.contact h1 {
text-align: center;
padding: 5px 0px;
font-size: 35px;
}
.contact .contact-box {
display: flex;
flex-direction: column;
}
.contact .contact-box input,
textarea {
width: 50%;
margin: auto;
padding: 8px 0px;
margin-top: 5px;
margin-bottom: 5px;
border: 2px solid black;
border-radius: 8px;
font-size: 18px;
}
.contact-box .contact-btn {
width: 100px;
margin: auto;
padding: 5px;
border: 2px solid black;
border-radius: 8px;
font-size: 20px;
cursor: pointer;
}
.contact-box .contact-btn:hover {
background-color: black;
color: white;
}
.footer {
background-color: black;
height: 35px;
}
#media only screen and (max-width:1220px) {
.navbar {
border: 2px solid blue;
flex-direction: column;
height: 350px;
justify-content: center;
align-items: 'center';
width: 100%;
}
.navbar-left {
border: 2px solid red;
flex-direction: column;
margin: auto;
}
.navbar-right {
display: flex;
flex-direction: column;
width: 100%;
justify-content: center;
align-items: center;
margin: auto;
}
.navbar-right input {
width: 50%;
}
.nav-list {
flex-direction: column;
}
.container .para {
padding: 5px 0px;
}
}
Sir i am getting problem in setting width of the elements like navbarr-right and navbar-left , i want to center the navbar elements when the media query is applied and also want to change its width so that it should be a good looking but it is not happening.
please suggest me where i am doing wrong and in a beginner friendly way that where i have to change and what to change alongwith the clear reason because i am a begineer.
im not so familar with web but why you don't just use #navbarleft or #navbar-right
I meanwhy you don't directly access it with the name you gave on the class name , i hope this makes a difference , try and tell me
How can I make an image fill the full height in a flexbox item? The image should be stretched to fill the height while keeping the aspect ratio.
body {
background: #20262E;
padding: 20px;
font-family: Arial;
}
.banner-message {
background: #eee;
border-radius: 20px;
padding: 0px;
overflow: hidden;
width: 500px;
display: flex;
align-items: stretch;
}
.banner-message .banner-message-media {
flex: 1;
}
.banner-message .banner-message-content {
padding: 20px;
flex: 2;
}
<div class="banner-message">
<div class="banner-message-media">
<img src="https://picsum.photos/id/1068/250/150" />
</div>
<div class="banner-message-content">
<h3>Content Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae delectus, quod veniam necessitatibus saepe error dicta voluptatem vitae incidunt nulla fugit! Quasi minus libero reiciendis similique cupiditate eum veniam tenetur.</p>
</div>
</div>
Add height:100%; and width:fit-content;
img {
height: 100%;
width: fit-content;
}
SCCS:
body {
background: #20262E;
padding: 20px;
font-family: Arial;
}
.banner-message {
background: #eee;
border-radius: 20px;
padding: 0px;
overflow: hidden;
width: 500px;
display: flex;
align-items: stretch;
.banner-message-media {
border-radius: 10px;
flex: 1;
img {
height:100%;
width:fit-content;
}
}
.banner-message-content {
padding: 20px;
flex: 2;
}
}
body {
background: #20262e;
padding: 20px;
font-family: Arial;
}
.banner-message {
background: #eee;
border-radius: 20px;
padding: 0px;
overflow: hidden;
width: 500px;
display: flex;
align-items: stretch;
}
.banner-message .banner-message-media {
border-radius: 10px;
flex-grow: 1;
}
.banner-message .banner-message-content {
padding: 20px;
flex-grow: 2;
}
img {
height: 100%;
width: fit-content;
}
<div class="banner-message">
<div class="banner-message-media">
<img src="https://picsum.photos/id/1068/250/150" />
</div>
<div class="banner-message-content">
<h3>Content Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae delectus, quod veniam necessitatibus saepe error dicta voluptatem vitae incidunt nulla fugit! Quasi minus libero reiciendis similique cupiditate eum veniam tenetur.</p>
</div>
</div>
Use object-fit property in your .banner-message-media css class. Like this
object-fit: fill or object-fit: cover.
This question already has answers here:
What elements can be contained within a <a> tag?
(6 answers)
Closed 3 years ago.
I'm trying to create a card for a website I'm pulling together. On hover, some text slides into view (I've used :hover combined with max-height). To make the whole card clickable, I tried to change a <div> to <a>, I added display:block.
I'm surprised that the behaviour is different - I expected them to be the same.
Two questions:
How do I get the right behaviour - simply making the card clickable without JavaScript.
What is the underlying issue that I've missed?
This version has the <div>:
.card {
background: yellow;
border-radius: .5em;
height: 15em;
position: relative;
overflow: hidden;
transition: all 1s ease-in-out;
}
.card:hover img {
transform: scale(1.1);
}
.card:hover .card-slider {
max-height: 7em;
}
.card-content {
position: absolute;
bottom: 0;
display: block;
}
.card-slider {
transition: all 1s ease-in-out;
max-height: 0;
overflow: hidden;
}
.card h1 {
background: rgba(0, 0, 0, 0.1);
color: white;
}
.card img {
transition: all 1s ease-in-out;
position: relative;
width: 100%;
height: 100%;
object-fit: cover;
}
.card .meta-cat {
position: absolute;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.1);
text-transform: uppercase;
}
.card .meta {
display: flex;
margin: 0;
padding: 0;
justify-content: space-between;
}
.meta-author, .meta-date {
list-style: none;
}
<article class="card">
<img src="http://via.placeholder.com/350x200" alt="">
<header class="meta-cat">Lorem.</header>
<div href="#" class="card-content">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure, velit.</h1>
<section class="card-slider">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias dignissimos dolor ex in iusto magnam
molestias odit quaerat rem rerum, similique sit sunt totam veritatis vitae voluptate, voluptatum? Amet,
maxime.</p>
<ul class="meta">
<li class="meta-author">WO J Wright</li>
<li class="meta-date">Posted 5 days ago</li>
</ul>
</section>
</div>
</article>
This version changes the <div> to a <a>.
.card {
background: yellow;
border-radius: .5em;
height: 15em;
position: relative;
overflow: hidden;
transition: all 1s ease-in-out;
}
.card:hover img {
transform: scale(1.1);
}
.card:hover .card-slider {
max-height: 7em;
}
.card-content {
position: absolute;
bottom: 0;
display: block;
}
.card-slider {
transition: all 1s ease-in-out;
max-height: 0;
overflow: hidden;
}
.card h1 {
background: rgba(0, 0, 0, 0.1);
color: white;
}
.card img {
transition: all 1s ease-in-out;
position: relative;
width: 100%;
height: 100%;
object-fit: cover;
}
.card .meta-cat {
position: absolute;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.1);
text-transform: uppercase;
}
.card .meta {
display: flex;
margin: 0;
padding: 0;
justify-content: space-between;
}
.meta-author, .meta-date {
list-style: none;
}
<article class="card">
<img src="http://via.placeholder.com/350x200" alt="">
<header class="meta-cat">Lorem.</header>
<a href="#" class="card-content">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure, velit.</h1>
<section class="card-slider">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias dignissimos dolor ex in iusto magnam
molestias odit quaerat rem rerum, similique sit sunt totam veritatis vitae voluptate, voluptatum? Amet,
maxime.</p>
<ul class="meta">
<li class="meta-author">WO J Wright</li>
<li class="meta-date">Posted 5 days ago</li>
</ul>
</section>
</a>
</article>
Actually you can have block elements inside <a> tag, as you already said, just replacing the nested <a> tag inside .meta-author with a span, makes your last snipped works.
.card {
background: yellow;
border-radius: .5em;
height: 15em;
position: relative;
overflow: hidden;
transition: all 1s ease-in-out;
}
.card:hover img {
transform: scale(1.1);
}
.card:hover .card-slider {
max-height: 7em;
}
.card-content {
position: absolute;
bottom: 0;
display: block;
}
.card-slider {
transition: all 1s ease-in-out;
max-height: 0;
overflow: hidden;
}
.card h1 {
background: rgba(0, 0, 0, 0.1);
color: white;
}
.card img {
transition: all 1s ease-in-out;
position: relative;
width: 100%;
height: 100%;
object-fit: cover;
}
.card .meta-cat {
position: absolute;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.1);
text-transform: uppercase;
}
.card .meta {
display: flex;
margin: 0;
padding: 0;
justify-content: space-between;
}
.meta-author, .meta-date {
list-style: none;
}
<article class="card">
<img src="http://via.placeholder.com/350x200" alt="">
<header class="meta-cat">Lorem.</header>
<a href="#" class="card-content">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure, velit.</h1>
<section class="card-slider">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias dignissimos dolor ex in iusto magnam
molestias odit quaerat rem rerum, similique sit sunt totam veritatis vitae voluptate, voluptatum? Amet,
maxime.</p>
<ul class="meta">
<li class="meta-author"><span>WO J Wright</span></li>
<li class="meta-date">Posted 5 days ago</li>
</ul>
</section>
</a>
</article>
I have an info box but I want it to slide in from left to right. But the css animation begins with a very high div because of the p tag inside it. Is there a way to fix this ?
JsFiddle : https://jsfiddle.net/scj5vc3b/
Html :
<div class="info-box info">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore ipsam fugit vero, autem asperiores accusamus ea unde magni distinctio excepturi minus amet repudiandae, debitis nulla voluptatem earum deleniti error reiciendis!</p>
</div>
SCSS :
.info-box {
margin: 10px 0;
border-left: 3px solid black;
overflow: hidden;
display: inline-block;
-webkit-animation: slideIn 1s linear;
position: relative;
p {
display: block;
padding: 10px 20px 10px 10px;
margin-bottom: 0;
}
}
#keyframes slideIn {
0% {max-width: 0px;}
100% {max-width: 1000px;}
}
.info-box.info {
background-color: lighten(blue, 24);
}
Use a transform on the paragraph instead:
.info-box {
margin: 10px 0;
border-left: 3px solid black;
-webkit-animation: slideIn 1s linear;
position: relative;
display: inline-block;
overflow: hidden;
}
p {
padding: 10px 20px 10px 10px;
margin-bottom: 0;
transform: translateX(-100%);
-webkit-animation: slideIn 1s linear forwards;
}
i {
padding-right: 15px;
}
#keyframes slideIn {
to {
transform: translateX(0%);
}
}
.info-box {
background-color: lightblue;
}
<div class="info-box info">
<p><i class="fa fa-info"></i>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore ipsam fugit vero, autem asperiores accusamus ea unde magni distinctio excepturi minus amet repudiandae, debitis nulla voluptatem earum deleniti error reiciendis!</p>
</div>