How to make text appear next to centered image after it moves? - css

Basically, I have this one block that I want to center in the middle of the page. This block should technically have both an image, and text. When the block isn't hovered over, I want only the image to appear centered on the page. When the block is hovered over, I want the image to move to the left a bit, and have the text appear to the right of it. So far, I've managed to make the image centered, to make it move to the left on hover, and to get the hover to display the text. The problem is, the text is under the image. I understand this is because the image and the text are in different div's, and so it would make sense that it would appear after. However, I'm not sure how to both make them in the same div, and ensure that the image is dead-center on the page when the block isn't hovered over. Is this possible?
Specifically, here's the HTML code for that section so far:
<section class="about-us">
<!-- Image division -->
<div class="chatBox">
<img src='./art/chatboxAbout.png' width= "150" height = "150" />
</div>
<!-- Text division, the actual about-us part -->
<div class="about-us-text-container">
<!-- Header part -->
<h1>about us</h1>
<!-- Horizontal line for styling -->
<hr />
<!-- Actual text -->
<p>For artists, not by artists</p>
</div>
</section>
and the CSS:
/* General sizing and format for the about-us segment */
.about-us{
width: 100%;
height: 200vh;
background-color: #fff;
}
/* Formatting for the chatBox image, basic stuff */
.about-us .chatBox{
position: relative;
margin-top: 50px;
text-align: center;
transition: transform 0.3s ease; /* Preparing for transition */
transition: translateX(0px); /* What the transition is */
}
/* Move left on hover effect */
.chatBox:hover{
transform: translateX(-200px);
}
/* Formatting for the general text div */
.about-us .about-us-text-container{
margin-top: 50px;
text-align:center;
margin-left: 15px;
opacity: 0; /* Don't display unless hovered */
transform: 1s; /* Setting duration for the hover opacity transition */
}
/* Show on hover effect */
.chatBox:hover + .about-us-text-container{
opacity: 1;
}
/* Formatting for the header */
.about-us .about-us-text-container h1{
}
/* Formatting for the horizontal line */
.about-us .about-us-text-container hr{
}
/* Formatting for the paragraph */
.about-us .about-us-text-container p{
}
Here's the JSFiddle link for the entire code so far: https://jsfiddle.net/bypvm6fu/
Any help is appreciated! Thank you so much!

Here's a possible solution for you. I changed a lot in your code, main things being: The container just contains the elements you already have (not 200vh = twice the window height). Just add another container around that, and sibling following it. The transition affects all, i.e. width, opacity and transform: scale to keep the image centered when not hovered. And the hover is on the container, not on the image, that way preventing the jumping effect you had before:
.about-us {
width: 100%;
height: 150px;
background-color: #fff;
display: flex;
justify-content: center;
align-content: center;
margin-top: 50px;
}
.about-us .chatBox {
position: relative;
text-align: center;
}
.about-us .about-us-text-container {
text-align: center;
margin-left: 15px;
opacity: 0;
transition: all 1s;
width: 0;
transform: scale(0);
}
.about-us:hover .about-us-text-container {
opacity: 1;
width: 150px;
transform: scale(1);
}
<section class="about-us">
<div class="chatBox">
<img src="https://picsum.photos/150" width="150" height="150" />
</div>
<div class="about-us-text-container">
<h1>about us</h1>
<hr />
<p>For artists, not by artists</p>
</div>
</section>

Related

Complex layout using React and CSS Grid

So I'm attempting to create the above. Is there a smart way of making the menu component? Or does the container element have to cover most of the content component (it has to be a rectangle and in itself use CSS Grid to position the left part and the top part of the menu)?
This doesn't exactly match your single 'L' shape component requirement, but should get you closer than you were before.
A couple notes worth mentioning:
This simply answers your question as a html/css question, not in React style. You could split this up into two components like you were hoping for by using every html/css element other than body-content, then having body-content html/css as the child
Not sure exactly how you want to handle your content, but with this code the App-Header will scroll with your content. If you want it to be fixed and stay above the content copy the MainMenu's css, but style it for vertical scrolling.
Hope this gets you going in the right direction.
body {
margin: 0px;
}
.App-header {
background-color: #203764;
height: 80px;
padding: 10px;
color: white;
}
/* Style page content */
.main-content {
margin-left: 160px; /* Same as the width of the MainMenu */
}
.body-content {
padding: 20px;
}
/* The MainMenu menu */
.MainMenu {
height: 100%; /* Full-height: remove this if you want "auto" height */
width: 160px; /* Set the width of the sidebar */
position: fixed; /* Fixed Sidebar (stay in place on scroll) */
z-index: 1; /* Stay on top */
top: 0; /* Stay at the top */
left: 0;
background-color: #111; /* Black */
overflow-x: hidden; /* Disable horizontal scroll */
color: #FFF;
}
<div class="App">
<div class="MainMenu">Main Menu</div>
<div class="main-content">
<header class="App-header">Header</header>
<div class="body-content">Content</div>
</div>
</div>

Some white space appeared after translatey css animation

i have a side build like this
<body>
<header>blabla</header>
<main> some content </main>
<footer>blabla</footer>
</body>
i have to animate some part of the main content UNDER the header,
so i have put a div around the main and the footer and used it as like an anchor.
<body>
<header>blabla</header>
<div id="myId">
<main> some content </main>
<footer>blabla</footer>
</div>
</body>
and than i used this to translatey.
but after the animation of translatey there is some white space.
how to remove this white space after animation?
Transform only change the position of the element not the space he is taking at the time DOM load. See below snippet
.parent {
padding: 20px;
background: black;
}
.child {
height: 100px;
background: red;
transition: all .3s ease;
}
.parent:hover .child {
transform: translateY(-50px);
}
<div class="parent">
<div class="child"></div>
</div>
What you are looking for is use margin negative values to animate like below...margin affects the width of the element which it carrying at the time of DOM load Using margin parent and child will both move.
.parent {
padding: 20px;
background: black;
}
.child {
height: 100px;
background: red;
transition: all .3s ease;
}
.parent:hover .child {
margin-top: -50px;
}
<div class="parent">
<div class="child"></div>
</div>
I hope this will help you the difference between the translateY and margin-top
If anyone else is facing this, try removing box shadows

Transition only one kind of transform at a time in CSS

I can't find a way to transition only one kind of transform in CSS.
I have a bunch of images (arms, legs, head etc.), that upon loading need to have a "transform: translate" applied to them so that they are in the right place before I do anything else.
I then want to have, say an arm, do a transition on just transform: rotate(20deg) on hover. The problem is that when transitioning, it will repeat the initial translation along with that rotation, but I only want it to do the rotation on hover. How can I make it only transition that one rotation?
In the example code below you can see that I make sure that the triangle is initially touching the other shape, using transform: translate. Then I do a transform: rotate on hover. It works, but it ALSO does the translate again.
(I suppose if the images could be placed in their right location without needing a transform: translate, that could perhaps solve it, but I don't know how to do that either. And It would just be better if I could isolate the transition to only transform: rotate.)
/* INITIAL PLACING OF THE IMAGES IN THEIR RIGHT PLACES */
img#Arm {
position: relative;
transform: translate(220px, 100px);
}
img#Body {
position: relative;
transform: translate(150px, 180px);
}
/* END OF PLACING OF THE IMAGES IN THEIR RIGHT PLACES */
/* THESE ARE THE TRANSFORMATIONS ON HOVER */
img#Arm {
transform-origin: 200px 200px;
transition: 1s ease-in-out;
}
section:hover img#Arm {
transform: rotate(25deg);
}
/* END OF TRANSFORMATIONS ON HOVER */
/* THIS IS TO KEEP THE LOGO PARTS GROUPED TOGETHER DESPITE WINDOW CHANGING RESOLUTIONS */
section.wrapper {
margin: 0 auto;
/* this keeps the logo centered on the page */
min-width: 800px;
/* Minimum width of your wrapper element */
max-width: 800px;
min-height: 800px;
max-height: 800px;
}
<section class="wrapper">
<img id="Arm" src="https://i.stack.imgur.com/yCC87.png" />
<img id="Body" src="https://i.stack.imgur.com/mmzDM.png" />
</section>
As you're overwriting your non-hover transform value on hover with the rotate, your arm gets positioned to its original position (like when there wouldn't be any translate on it).
As you can set multiple values for the transform property, just also add the translate on hover to it. This will keep your arm on the right position and just rotates it.
/* INITIAL PLACING OF THE IMAGES IN THEIR RIGHT PLACES */
img#Arm {
position: relative;
transform: translate(220px, 100px);
}
img#Body {
position: relative;
transform: translate(150px, 180px);
}
/* END OF PLACING OF THE IMAGES IN THEIR RIGHT PLACES */
/* THESE ARE THE TRANSFORMATIONS ON HOVER */
img#Arm {
transform-origin: 200px 200px;
transition: 1s ease-in-out;
}
section:hover img#Arm {
transform: translate(220px, 100px) rotate(25deg);
}
/* END OF TRANSFORMATIONS ON HOVER */
/* THIS IS TO KEEP THE LOGO PARTS GROUPED TOGETHER DESPITE WINDOW CHANGING RESOLUTIONS */
section.wrapper {
margin: 0 auto;
/* this keeps the logo centered on the page */
min-width: 800px;
/* Minimum width of your wrapper element */
max-width: 800px;
min-height: 800px;
max-height: 800px;
}
<section class="wrapper">
<img id="Arm" src="https://i.stack.imgur.com/yCC87.png" />
<img id="Body" src="https://i.stack.imgur.com/mmzDM.png" />
</section>

Text appear from left to right

I want the caption of a image (which is not necessary is a real caption) appear when the image is hovered. The text should appear from left to right so it's container should grow on the X axis as the image is hovered. I have been able to make the text appear on hover but the transition effect is where I am stuck.
This is what I have tried:
CSS:
.morbid {
display: none;
margin-left:10px;
}
a:hover + .morbid {
position:relative;
display: block;
}
HTML:
<a>
<img src='.../goals/sia.png'>
</a>
<div class="morbid">
This text will appear as you hover your mouse over the image.
</div>
Actually the whole div must grow, text remaining static and I want the text to be hidden unless the image is hovered.
You cannot transition the display property's change of value. The full list of properties which can be animated is available in the spec.
For the content to grow and appear you need to transition the max-width (or width, if you can set a fixed width to the element) like done in below snippet.
.morbid {
width: auto;
max-width: 0%;
white-space: nowrap;
overflow: hidden;
transition: max-width 1s linear;
}
a:hover + .morbid {
max-width: 100%;
}
<a href="#">
<img src='http://picsum.photos/400/200'>
</a>
<div class="morbid">
This text will appear as you hover your mouse over the image.
</div>
JSFiddle
Alternately, if you want the text to grow from the center then you can make use of absolute positioning with transform like in the below snippet.
.container {
position: relative;
width: 400px;
}
.morbid {
position: absolute;
left: 50%;
max-width: 0%;
white-space: nowrap;
overflow: hidden;
transform: translateX(-50%);
transition: max-width 1s linear;
}
a:hover + .morbid {
max-width: 100%;
}
<div class="container">
<a href="#">
<img src='http://picsum.photos/400/200'>
</a>
<div class="morbid">
This text will appear as you hover your mouse over the image.
</div>
</div>
JSFiddle
Use the transition property on the element you want to add a transition effect.
Make the caption grow and shrink in size:
CSS:
.morbid {
/* display: none; */
margin-left:10px;
opacity: 0;
font-size: 12pt;
-webkit-transition: font-size 0.25s;
transition: font-size 0.25s;
}
a:hover + .morbid {
position:relative;
/* display: block; */
font-size: 16pt;
opacity: 1;
}
HTML:
<a>
<img src='.../goals/sia.png'>
</a>
<div class="morbid">
This text will appear as you hover your mouse over the image.
</div>
I had to change opacity for the caption instead of changing the display property because the animation doesn't show when changing the display property.

Css help - background images and margins

im trying to change the custom CSS of my page headers, but every time i try to add a top-margin to one of the nested divs, it moves the entire background image down.
Current code (abridged):
<header id="fancy-header" style="
background-color: #ffffff; background-size: cover;
background-repeat: no-repeat;
background-image: url(http://thegsc.co.il/wp-content/uploads/2014/07/about-header.jpg);
background-position: center center; min-height: 150px">
<div class="wf-wrap">
<div class="wf-table" style="
background:rgba(32,117,200, .6) !important;
margin-top:25px;">
<div class="title">title</div><div class="breadcrumbs">breadcrumbs</div>
</div>
</div>
</header>
Ideally, all Im trying to do is have a background image that spans the entire header (150px), and then a transparent-color overlay on the background text.
Using margin-top:25px because when i tried adding a vertical-align:middle to .wf-table, nothing happened.
Suggestions?
The trouble is that your .wf-table element was not respecting #fancy-header as its parent. The trick is to add float: left and it'll behave in this case as you expect. Check it out here - https://jsfiddle.net/96hf0nqa/
Floats are a little tricky but super important for effective CSS. You can read a lot about them here. Good luck!
If your <header> is the main container to be full-screen then ideally you should be using a grid approach.
Not sure if you use a grid or not? In this case the wf-wrap is your grid-row:
.wf-wrap {
box-sizing: border-box; /* box model fix */
max-width: 94rem; /* 960px - gutter */
padding: 0 1rem; /* grid gutter */
margin: 0 auto; /* to center */
height: 100%;
position: relative; /* to init the absolute offset of it's children */
}
Then your wf-table should be positioned absolute to have full control:
.wf-table {
position: absolute; /* to control top left right bottom */
top: 2.5rem; /* default offset */
z-index: 1 /* if overlap requires it */;
}
DEMO
Typically a grid-row is divided into columns. The position relative belongs on a column actually.
.column {
box-sizing: border-box;
padding: 0 1rem;
float: left;
height: 100%;
position: relative;
}
.g50 { max-width: 50%; width: 100%; }
So your HTML can be made a little bit more generic to gain control on the position of elements inside columns.
<div class="header">
<div class="row">
<div class="column g50">
<div class="wf-table"><!--content here--></div>
</div>
<div class="column g50"></div>
</div>
</div>
DEMO
Additionally there is a way to vertically align a block element in CSS3:
.middle-y {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-o-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}

Resources