I'm trying to achieve the effect of having text show when I hover over it. I looked at another question: Using only CSS, show div on hover over <a> , and tried out some of the solutions, but they are not working for me. Here is my code: https://codepen.io/anon/pen/PLYGyg. Thanks!
div#thesishereyay {
margin-top: 50px;
margin-left: 120px;
position: absolute;
opacity: 0;
background-color: #000000;
}
h4.thesis {
margin: 10px;
}
li.thesisbutton {
position: absolute;
margin-top: 15px;
margin-left: 112px;
opacity: 1;
}
.thesisbutton:hover div.thesishereyay {
opacity: 1;
}
<ul class="nav">
<li class="thesisbutton">Thesis</li>
</ul>
<div id="thesishereyay">
<h4 class="thesis">
Thirst for triumph during the<br>
Vietnam war in order to prevent<br>
a U.S. humiliation led to Lyndon<br>
Johnson lying to the American public<br>
about the Gulf of Tonkin incident to<br>
gain support for the Gulf of Tonkin<br>
resolution, which resulted in the<br>
continuation of the war and the<br>
tragedy of more lives being lost<br>
for a war being fought in fear of what<br>
might happen if the U.S. was defeated.</h4>
</div>
You can make something like this.
div#thesishereyay {
margin-top: 50px;
margin-left: 120px;
position: absolute;
opacity: 0;
background-color: #000000;
}
h4.thesis {
margin: 10px;
}
li.thesisbutton {
position: absolute;
margin-top: 15px;
margin-left: 112px;
opacity: 1;
}
.nav:hover ~ #thesishereyay {
opacity: 1;
}
<div class="main">
<ul class="nav">
<li class="thesisbutton">Thesis</li>
</ul>
<div id="thesishereyay">
<h4 class="thesis">
Thirst for triumph during the<br>
Vietnam war in order to prevent<br>
a U.S. humiliation led to Lyndon<br>
Johnson lying to the American public<br>
about the Gulf of Tonkin incident to<br>
gain support for the Gulf of Tonkin<br>
resolution, which resulted in the<br>
continuation of the war and the<br>
tragedy of more lives being lost<br>
for a war being fought in fear of what<br>
might happen if the U.S. was defeated.</h4>
</div>
</div>
This the basic markup:
ul > li > div {display:none;} /* hide div inside li */
ul > li:hover > div {display: block;} /* show div when li hovered */
<ul class="nav">
<li class="thesisbutton">Thesis
<div id="thesishereyay">...</div>
</li>
</ul>
Hope that helps.
I know this is not css which you want, but maybe will help to somebody :)
$('#thesishereyay').on('mouseenter mouseleave', function(e) {
var $block = $('#thesishereyay');
if(e.type == 'mouseenter') {
$block.show();
} else if(e.type == 'mouseleave') {
$block.hide();
}
});
Not sure if this is what you're looking for but take a look:
li.hoverbutton {
position: relative;
}
#hoveron {
top: 100%;
left: 0;
position: absolute;
display: none;
background-color: #000000;
}
h4.showonhover {
padding: 10px;
color: #ffffff;
}
li.hoverbutton:hover #hoveron {
display: block;
}
<ul class="nav">
<li class="hoverbutton">
Hover!
<div id="hoveron">
<h4 class="showonhover">Text to show</h4>
</div>
</li>
</ul>
Related
How would I create a background that only effects the area within the yellow lines?
I have tried adding padding, but that expands the page and does not effect the background color
How would I align the writing to the correct red squares?
If possible I would like pointers to good resources to learn CSS styling. I have tried align:centre, flexbox.
#import url("https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght#0,400;1,700&display=swap");
* {
font-family: "Noto Sans", sans-serif;
}
.body {
background-color: #32a89d;
}
.homepage_text {
font-family: "Noto Sans", sans-serif;
text-decoration: none;
}
.container {
margin-top: 5%;
margin-left: 7%;
margin-right: 7%;
}
.logo_header {
margin-left: 7%;
}
.header_list {
list-style: none;
float: right;
font-family: "Noto Sans", sans-serif;
}
header {
display: flexbox;
}
/* Animated Text */
.wrapper {
height: 45px;
/*This part is important for centering*/
display: flexbox;
/* align-items: right; */
justify-content: right;
margin-right: 7%;
}
.typing-demo {
width: 56ch;
animation: typing 2s steps(40), blink 0.5s step-end infinite alternate;
white-space: nowrap;
overflow: hidden;
border-right: 3px solid;
font-family: "Noto Sans", sans-serif;
font-size: 2em;
}
#keyframes typing {
from {
width: 0;
}
}
#keyframes blink {
50% {
border-color: transparent;
}
}
/* End of Animated */
/* Navigation Bar */
#menu__toggle {
opacity: 0;
}
#menu__toggle:checked ~ .menu__btn > span {
transform: rotate(45deg);
}
#menu__toggle:checked ~ .menu__btn > span::before {
top: 0;
transform: rotate(0);
}
#menu__toggle:checked ~ .menu__btn > span::after {
top: 0;
transform: rotate(90deg);
}
#menu__toggle:checked ~ .menu__box {
visibility: visible;
left: 0;
}
.menu__btn {
display: flex;
align-items: center;
position: fixed;
top: 20px;
left: 20px;
width: 26px;
height: 26px;
cursor: pointer;
z-index: 1;
}
.menu__btn > span,
.menu__btn > span::before,
.menu__btn > span::after {
display: block;
position: absolute;
width: 100%;
height: 2px;
background-color: #808080;
transition-duration: 0.25s;
}
.menu__btn > span::before {
content: "";
top: -8px;
}
.menu__btn > span::after {
content: "";
top: 8px;
}
.menu__box {
display: block;
position: fixed;
visibility: hidden;
top: 0;
left: -100%;
width: 300px;
height: 100%;
margin: 0;
padding: 80px 0;
list-style: none;
background-color: #eceff1;
box-shadow: 1px 0px 6px rgba(0, 0, 0, 0.2);
transition-duration: 0.25s;
}
.menu__item {
display: block;
padding: 12px 24px;
color: #333;
font-family: "Roboto", sans-serif;
font-size: 20px;
font-weight: 600;
text-decoration: none;
transition-duration: 0.25s;
}
.menu__item:hover {
background-color: #cfd8dc;
}
/* footer */
.facebook_icon {
align-items: centre;
}
.footer_icons {
list-style: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Doorways Derby</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='/style.css'>
<script src='script.js'></script>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
</head>
<header>
<img class = 'logo_header' src = '/img/logo.png'
<div class="hamburger-menu">
<input id="menu__toggle" type="checkbox" />
<label class="menu__btn" for="menu__toggle">
<span></span>
</label>
<ul class="menu__box">
<li><a class="menu__item" href="about.html">About Doorways</a></li>
<li><a class="menu__item" href="contact.html">Contact Us</a></li>
<li><a class="menu__item" href="get_involved.html">Get Involved</a></li>
<li><a class="menu__item" href="more.html">More</a></li>
<li><a class="menu__item" href="#"></a></li>
</ul>
</div>
<div class="wrapper">
<div class="typing-demo">
BE SOMEBODY THAT MAKES EVERYBODY FEEL LIKE A SOMEBODY
</div>
</div>
<li class = 'header_list'>
<ul><b>Contact Number: 07754128763</b></ul>
</li>
</header>
<body class = 'body'>
<div class="container">
<img class = 'img' src = '/img/122562126_1810506009098238_1438181275282.webp'>
<h3> How we started </h3>
<p class = 'homepage_text'> Doorways was co foundered by Craig (a full time working fabricator) and Lisa Wheatley (a school cook) with friends Patrick and Tina Hall in May 2015 to help homeless rough sleepers (around 15) in Derby city centre, by providing service users with a hot meal, a sandwich pack and a hot drink. </p>
<p class = 'homepage_text'>This issue was brought to our attention after Lisa had helped another group within the city by cooking meals for them to give out to homeless people. We set up the group with friends and a few family members to help make sandwiches and distribute the items and went out three nights a week.</p>
<p class = 'homepage_text'> It soon became apparent that there was a bigger issue within the city of more vulnerable people than we had anticipated. People were coming to use our service from hostels, shared accommodation, people in their own property on low income and even people with no life skills to cook for themselves. Our numbers increased quite quickly and numbers went up to around 60 visitors by the end of 2015.</p>
<p class = 'homepage_text'> Four years on and with the help and backing of several businesses, who help by donating end of day food items. and having a Facebook group with over 8000 members the support has been overwhelming. We have a core group of volunteers who help with cooking and the running of Doorways on our nights out, we regularly see over 100 visitors on each of our nights out now and have recently become a non-profit making registered charity (1182774).</p>
<p class = 'homepage_text'> Our aim is to have access to a building which we can use to cook the meals, use for storage and have as a drop in centre for people to come to for information in our sector, this is alongside still having our pop up street kitchen 3 nights a week.</p>
<h2> THEN COVID-19 HIT.</h2>
<p class = 'homepage_text'> Since March 2020, our plans have been put on hold as we have been unable to run the street kitchen and keep within the government guidelines set regarding social distancing and large gatherings in public spaces. We have however still been able to continue helping vulnerable people/families by setting up a food bank and produce table from our home address. We set up a food produce table twice a week on a Tuesday and a Saturday, where people can come along and take what they need to put meals on the table.</p>
<p class = 'homepage_text'> We regularly see over 120 households each time we are open. In addition to this, we have delivered or had collected over 2000 food parcels to those in need.</p>
</div>
</body>
<footer class = 'footer'>
<li class = 'footer_icons'>
<ul class = 'facebook_icon'> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M22.675 0h-21.35c-.732 0-1.325.593-1.325 1.325v21.351c0 .731.593 1.324 1.325 1.324h11.495v-9.294h-3.128v-3.622h3.128v-2.671c0-3.1 1.893-4.788 4.659-4.788 1.325 0 2.463.099 2.795.143v3.24l-1.918.001c-1.504 0-1.795.715-1.795 1.763v2.313h3.587l-.467 3.622h-3.12v9.293h6.116c.73 0 1.323-.593 1.323-1.325v-21.35c0-.732-.593-1.325-1.325-1.325z"/></svg></ul>
</li>
</footer>
</html>
<!-- <ul class="menu__box">
<li><a class="menu__item" href="about.html">About Doorways</a></li>
<li><a class="menu__item" href="contact.html">Contact Us</a></li>
<li><a class="menu__item" href="get_involved.html">Get Involved</a></li>
<li><a class="menu__item" href="more.html">More</a></li>
<li><a class="menu__item" href="#"></a></li>
</ul> -->
I think you have miss-spelled the flexbox property. The property is display: flex;
I don't think it's a good idea to have inside the tag. You can add the inside the container. It will help you to remove to the spacing issue.
To get justify-content to work, the element itself needs to be displayed as flex:
display: flex;
justify-content: right;
Aplying this to your .wrapper and the "Be somebody..." text will actually go to the right.
For learning some more CSS i recommend W3Schoool(free) or Codecademy(not free).
Flex is the answer!
CSS-Tricks provides a lot of good, useful information about css.
And flexbox froggy also helped me a lot to learn flexbox and grid.
If you flex a container, the children inside that container will align along eachother.
<div style="display: flex">
<p>LEFT</p>
<p>RIGHT</p>
</div>
You can then use justify-content and align-items, you align the content to your liking.
For quick solution :
position: absolute;
right: 0;
top: 35px;
There are many errors in your code, But hope so you will fix them, Best of luck.
Unfortunately it wont display my drop-down nav block on hover, hope someone can help. I'm trying to change display:none to display: block on the class .moreMenu.
html
css
Here is my code...
HTML
<div class="navigationBar">
<div class = "navigationBarTitles">
HOME
VERBS
NOUNS
ADVERBS
ADJECTIVES
GRAMMAR
<a href="contact.html" class= "more">MORE
<div class="moreMenu">
TRIAL VIDEOS
ABOUT
CONTACT
GCSE EXAM PREP
PLANS & PRICING
GRAMMAR
</div>
</a>
</div>
CSS
.moreMenu {
display: none;
position: absolute;
width: 140px;
height: 180px;
background-color: white;
z-index: 2;
}
.more:hover .moreMenu {
display: block;
}
You have an error in your HTML: you can't nest interactive elements. Since the .moreMenu div contains <a> elements, and is itself contained in an <a> element, it won't work.
Solution: don't put the <div> inside the MORE link, but put them next to each other in a container.
.more {
display: inline;
}
.moreMenu {
display: none;
position: absolute;
width: 140px;
height: 180px;
background-color: white;
z-index: 2;
}
.more:hover .moreMenu {
display: block;
}
<header>
<div class="navigationBar">
<div class="navigationBarTitles">
HOME
VERBS
NOUNS
ADVERBS
ADJECTIVES
GRAMMAR
<div class="more">MORE
<div class="moreMenu">
TRIAL VIDEOS
ABOUT
CONTACT
GCSE EXAM PREP
PLANS & PRICING
GRAMMAR
</div>
</div>
</div>
</div>
</header>
I've been trying to figure out the issue for hours. I've tried to use .about_container {justify-content: space-evenly;} However, it doesn't add any spacing between the child items. Also, the items do not adjust size. I've tried adding flex-flow, flex-basis, and flex-shrink/grow but none of them make any difference. I'm also having issues getting the images to be a specific size while also being responsive.
I would appreciate any advice!
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
max-width: 1300px;
width: 100%;
}
body {
margin: 0 auto;
max-width: 80%;
height: 100%;
background-attachment: fixed;
background-image: url(2019-12-11-mountain-day-featured-01.jpg);
background-size: cover;
}
h1 {
font-size: 3em;
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
text-align: center;
margin-bottom: 10%;
}
.about_container {
display: flex;
color: #A3DAE3;
background-color: #784C87;
box-shadow: -10px -10px 15px #A3DAE3 inset;
border-radius: 25px;
margin-bottom: 10%;
}
#about_jason {
margin-left: 20px;
}
#about_sam {
margin-right: 20px;
}
#about_mike {
margin-left: 20px;
margin-right: 20px;
}
.about_container img {
height: 300px;
width: 300px;
object-fit: scale-down;
}
article {
margin-top: 10px;
}
#about_mike a {
text-decoration: none;
color: #450175;
}
#about_mike a:hover {
color: #89A1C9;
}
footer {
text-align: center;
padding-top: 5%;
}
#links li {
transition: all .2s ease-in-out;
}
#links li:hover {
transform: scale(1.5);
}
#media only screen and (max-width: 768px) {
html {
width: 100%;
}
.about_container {
display: flex;
flex-direction: column;
justify-content: space-around;
}
article {
padding-bottom: 20px;
padding-top: 20px;
}
.about_jason > h2 {
padding-top: 30px;
}
#about_mike a {
text-decoration: none;
color: #450175;
}
#about_mike a:hover {
color: #784C87;
}
}
<body>
<div class="menu-wrap">
<input type="checkbox" class="toggler">
<div class="hamburger"><div></div></div>
<div class="menu">
<div>
<div>
<ul id="links">
<li>Home</li>
<li>About Us</li>
<li>How-to</li>
<li>Portfolio</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sharethis-inline-follow-buttons"></div>
<h1>About Us</h1>
<div class="about_container">
<div id="about_jason">
<h2>Jason R.</h2>
<img src="90528548_10213477312766132_4964276883236585472_n.jpg" />
<p>Hey guys! My name is Jason. I graduated with my Associates of
Applied Science degree in Computer Programming at Minnesota State
Community and Technical college. I am now a Computer Science major
at Davenport University. I work on both the front-end and back-end
within our small team. Outside of work, I enjoy playing video games,
biking, hiking, programming and learning about anything and everything!
I look forward to working with you!</p>
</div>
<div id="about_mike">
<h2>Michael J.</h2>
<img src="Mike.jpg"/>
<p>Hey there! My name is Mike. I live in Montrose,
Colorado where I remotely work with Jason and Sam.
I am a self-taught graphic designer/illustrator.
For several years, I have developed a strong graphic
design portfolio and have a collection of my journey
over the years on my own personal Wordpress at
my wordpress.
I am advanced in Adobe InDesign, Illustrator, and
Photoshop. Outside of work, I enjoy working on digital
art, reading, photography, and teaching myself how to program.
I am currently learning web development and python. I look
forward to working with my team and I look forward to working
with our clients we work with.</p>
</div>
<div id="about_sam">
<h2>Samantha K.</h2>
<img src="cropped_profile_picture.jpg"/>
<p>Hi everyone! My name is Samantha K. I started as a self
taught designer and am now making the big move to pursue
a degree in web design. I have experience in affinity designer,
html and css. I have a beautiful 2 year old daughter, Emily, and
a one year old puppy, Tobi. I live in Clifton, Colorado with my
fiance, my daughter, our best friend and our dog. I enjoy bike rides,
swimming, movies, photography, writing and creating digital art.
I look forward to learning, gaining experience and working with my
team to create beautiful designs.</p>
</div>
</div>
</body>
the problem is here. you have given the images the width and height values to remain the same in all sizes.
.about_container img {
height: 300px;
width: 300px;
object-fit: scale-down;
}
I created a single class for each person div (about). and I gave each picture the width property as 350 px. instead of giving a fixed width height value to the images, I added the properties display block, width 100%. So they will always adjust themselves according to it, no matter how much the container width is.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
margin: 0 auto;
max-width: 80%;
height: 100%;
background-attachment: fixed;
background-image: url(2019-12-11-mountain-day-featured-01.jpg);
background-size: cover;
}
h1 {
font-size: 3em;
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
text-align: center;
margin-bottom: 10%;
}
.about_container {
display: flex;
color: #A3DAE3;
background-color: #784C87;
box-shadow: -10px -10px 15px #A3DAE3 inset;
border-radius: 25px;
margin-bottom: 10%;
justify-content: space-evenly;
}
.about {
width: 350px;
}
#about_jason {
margin-left: 20px;
}
#about_sam {
margin-right: 20px;
}
#about_mike {
margin-left: 20px;
margin-right: 20px;
}
.about_container img {
display: block;
width: 100%;
object-fit: scale-down;
}
article {
margin-top: 10px;
}
#about_mike a {
text-decoration: none;
color: #450175;
}
#about_mike a:hover {
color: #89A1C9;
}
footer {
text-align: center;
padding-top: 5%;
}
#links li {
transition: all .2s ease-in-out;
}
#links li:hover {
transform: scale(1.5);
}
#media only screen and (max-width: 768px) {
html {
width: 100%;
}
.about_container {
display: flex;
flex-direction: column;
justify-content: space-around;
}
article {
padding-bottom: 20px;
padding-top: 20px;
}
.about_jason>h2 {
padding-top: 30px;
}
#about_mike a {
text-decoration: none;
color: #450175;
}
#about_mike a:hover {
color: #784C87;
}
}
<body>
<div class="menu-wrap">
<input type="checkbox" class="toggler">
<div class="hamburger">
<div></div>
</div>
<div class="menu">
<div>
<div>
<ul id="links">
<li>Home</li>
<li>About Us</li>
<li>How-to</li>
<li>Portfolio</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sharethis-inline-follow-buttons"></div>
<h1>About Us</h1>
<div class="about_container">
<div id="about_jason" class="about">
<h2>Jason R.</h2>
<img src="https://i.pinimg.com/originals/72/1d/13/721d13848e02189b9c7aed0d27ae433a.jpg" />
<p>Hey guys! My name is Jason. I graduated with my Associates of Applied Science degree in Computer Programming at Minnesota State Community and Technical college. I am now a Computer Science major at Davenport University. I work on both the front-end
and back-end within our small team. Outside of work, I enjoy playing video games, biking, hiking, programming and learning about anything and everything! I look forward to working with you!</p>
</div>
<div id="about_mike" class="about" >
<h2>Michael J.</h2>
<img src="https://i.pinimg.com/originals/72/1d/13/721d13848e02189b9c7aed0d27ae433a.jpg" />
<p>Hey there! My name is Mike. I live in Montrose, Colorado where I remotely work with Jason and Sam. I am a self-taught graphic designer/illustrator. For several years, I have developed a strong graphic design portfolio and have a collection of my
journey over the years on my own personal Wordpress at
my wordpress. I am advanced in Adobe InDesign, Illustrator, and Photoshop. Outside of work, I enjoy working on digital art, reading, photography, and teaching myself how to program. I am currently
learning web development and python. I look forward to working with my team and I look forward to working with our clients we work with.</p>
</div>
<div id="about_sam" class="about">
<h2>Samantha K.</h2>
<img src="https://i.pinimg.com/originals/72/1d/13/721d13848e02189b9c7aed0d27ae433a.jpg" />
<p>Hi everyone! My name is Samantha K. I started as a self taught designer and am now making the big move to pursue a degree in web design. I have experience in affinity designer, html and css. I have a beautiful 2 year old daughter, Emily, and a one
year old puppy, Tobi. I live in Clifton, Colorado with my fiance, my daughter, our best friend and our dog. I enjoy bike rides, swimming, movies, photography, writing and creating digital art. I look forward to learning, gaining experience and
working with my team to create beautiful designs.</p>
</div>
</div>
This is my problem:
https://youtu.be/5I9K05FJFBI
I'm just a beginner and my problem is that when I click on the logo so it goes as it should but on the far left of the menu is a square and the reverse logo and I do not want there. I just want the logo to be clickable (href = "index.html") and there is no square on the left. I'm a beginner so please write how to fix it, I did something myself, something from the book and some tutorials on youtube but I can't solve this. Thanks for help.
CSS:
a:hover {
background-color: #e67e22;
transform:scale(1.2) rotate(5deg);
}
.topnav-right {
float: right;
}
.topnav {
overflow: hidden;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 20px;
}
.stred {
position: absolute;
left: 50%;
top: 0;
content: "";
text-align: center;
}
.zaoblenie {
border-radius: 50%;
}
.zmensit {
width: 120px;
height: 120px;
}
HTML:
<div class="topnav">
<li>O nás</li>
<li>Admin Team</li>
**<img class="zmensit zaoblenie stred" src="images/logo.png" alt="logo" style="border-right-style: solid; left: 46.224% !important;border-right-width: 0px;">**
<div class="topnav-right">
<li>Vecičky</li>
<li>Youtuberi</li>
</div>
</div>
a:not(.logo-block):hover {
background-color: #e67e22;
transform:scale(1.2) rotate(5deg);
}
The code for removing orange colour from logo.
You need to give "logo-block" class to "a" tag of logo
**<a class="logo-block" href="index.html"><img class="zmensit zaoblenie stred" src="http://hdwallpapersrocks.com/wp-content/uploads/2013/12/Amazing-boat-in-sea-marvelous-wallpapers.jpg" alt="logo" style="border-right-style: solid; left: 46.224% !important;border-right-width: 0px;"></a>**
Thank You
Firstly your question is very unclear.
Secondly the HTML format is also not proper according to the coding standards.
But still I can help you.
please find below html updated code
<div class="topnav">
<ul>
<li>O nás</li>
<li>Admin Team</li>
</ul>
**<a class="logo-block" href="index.html"><img class="zmensit zaoblenie stred" src="http://hdwallpapersrocks.com/wp-content/uploads/2013/12/Amazing-boat-in-sea-marvelous-wallpapers.jpg" alt="logo" style="border-right-style: solid; left: 46.224% !important;border-right-width: 0px;"></a>**
<div class="topnav-right">
<ul>
<li>Vecičky</li>
<li>Youtuberi</li>
</ul>
</div>
</div>
Also, find css updated
a:not(.logo-block):hover {
background-color: #e67e22;
transform:scale(1.2) rotate(5deg);
}
The css I have applied is for not giving hover effect to the "a" tag having class "logo-block".
Please let me know if you need any furthur help.
Thank You
For removing the square in the left please add the class "stred" to "a" tag instead of giving it to "img tag".
So i've come to live by these 3 CSS rules that almost always vertically center any block level element:
.vertically-center {
position: relative;
top: 50%;
transform: translateY( -50% );
}
It works often. But in the case of this particular layout I'm building it is pushing the elements too high ( partially off the screen ) and I don't know why.
This is how the webpage looks before adding my vertically-center class to my portrait-container div:
And this code snippet is how it appears after adding the vertically-center class to the portrait-container div:
.clearfix:after {
content: "";
display: block;
clear: both;
}
.vertically-center {
position: relative;
top: 50%;
transform: translateY( -50% );
}
body {
overflow: hidden;
}
main {
padding-top: 50px;
background: #fafafa;
text-align: left;
}
.portrait-container {
float: left;
}
img {
width: 150px;
border-radius: 50%;
}
.about-container {
width: 70%;
float: right;
}
<main class="clearfix">
<div class="portrait-container vertically-center">
<img src="http://i.imgur.com/Eb5sRZr.jpg" alt="Portrait of John Lesko">
</div>
<div class="about-container">
<h3>About</h3>
<p>
Hi, I'm John Lesko! This is my art portfolio where I share all
of my favorite work. When I'm not creating things, I enjoy excercising,
playing video games, drinking good Kool Aid, and more.
<br><br> If you'd like to follow me on Twitter, my username is
#jletsgo.
</p>
</div>
</main>
I just want the image container to be vertically-centered regardless of the height of it's parent. Help? Inspecting elements gave me no insights.
Edit: Just to show how this has always worked for me in the past. Here is a jsfiddle: https://jsfiddle.net/9kyjt8ze/4/. Why does it work for me there and not here?
Related question: What does top: 50%; actually do on relatively positioned elements?
Your CSS was not bad but I didn't get along with it. So here is another approach on how you could solve it, maybe it helps also. It will always center the image vertically and does not matter how much text the box on the right will have. The colored borders are just there to help show the visual effect of the box sizes.
* {
box-sizing: border-box;
}
.portrait-container {
position: relative;
margin: 20px 0;
}
.portrait-container:after {
content: '';
display: block;
clear: both;
}
.portrait-container img {
position: absolute;
top: calc(50% - 80px); /* 50% from top minus half img height*/
width: 150px;
height: 160px;
border-radius: 50%;
float: left;
}
.portrait-container {
border: solid 2px orange;
}
.portrait-container .about-container {
border: solid 2px green;
padding: 0 50px;
margin-left: 150px; /* this elements should be at least 150px away from left side */
width: calc(100% - 150px); /* the max width this element should have to be placed */
/* next to the image is the total width(100%) - the image width */
}
<main>
<div class="portrait-container">
<img src="http://i.imgur.com/Eb5sRZr.jpg" alt="Portrait of John Lesko">
<div class="about-container">
<h3>About</h3>
<p>
Hi, I'm John Lesko! This is my art portfolio where I share all
of my favorite work. When I'm not creating things, I enjoy excercising,
playing video games, drinking good fruit punch, and more.
<br><br> If you'd like to follow me on Twitter, my username is
#jletsgo.
</p>
</div>
</div>
</main>
<main>
<div class="portrait-container">
<img src="http://i.imgur.com/Eb5sRZr.jpg" alt="Portrait of John Lesko">
<div class="about-container">
<h3>About</h3>
<p>
Hi, I'm John Lesko! This is my art portfolio where I share all
of my favorite work. When I'm not creating things, I enjoy excercising,
playing video games, drinking good fruit punch, and more.
<br><br> If you'd like to follow me on Twitter, my username is
#jletsgo.
</p>
<p>
Hi, I'm John Lesko! This is my art portfolio where I share all
of my favorite work. When I'm not creating things, I enjoy excercising,
playing video games, drinking good fruit punch, and more.
<br><br> If you'd like to follow me on Twitter, my username is
#jletsgo.
</p>
</div>
</div>
</main>
UPDATE
Edit: Just to show how this has always worked for me in the past. Here is a jsfiddle: https://jsfiddle.net/9kyjt8ze/4/. Why does it work for me there and not here?
The black circle is the only element there in the Fiddle, there's no obstructions. In the code you are having trouble with, you have many elements either in the way or wrapped around other elements trapping them. Your ruleset will work if you start stripping away the layers. Or you can just add a property and change another property as per Snippet 1.
One important note a relative element is actually occupying the original spot, so if given a left:40px it appears to be moved 40px to the left, but in reality it still occupies the space 40px to the right of where it appears to be. So relative elements are not really in a flow different from static elements. Therefore they are affected by and affect static layout, it's just not noticeable normally because they stack with z-index.
Snippet 2 is an interactive demo, I figured maybe that'll help explain things better.
The 3 CSS ruleset is a common way to vertically align elements, but it was originally position: absolute instead of position:relative and it had to be in another positioned element if I remember correctly.
REFERENCE
Specific Ruleset
W3Schools
MDN
SOLUTION
.vertically-center {
/* Changed to absolute from relative */
position: absolute;
top: 50%;
transform: translateY( -50% );
}
main {
/* Added position: relative */
position: relative;
padding-top: 50px;
background: #fafafa;
text-align: left;
}
SNIPPET 1
.vertically-center {
position: relative;
top: 50%;
transform: translateY( -50%);
}
body {}
main {
padding-top: 50px;
overflow: scroll;
background: #fafafa;
text-align: left;
}
img {
width: 150px;
border-radius: 50%;
float: left;
}
.about {
width: calc(100% - 150px);
float: right;
}
<main class="clearfix">
<img src="http://i.imgur.com/Eb5sRZr.jpg" alt="Portrait of John Lesko" class="vertically-center">
<article class="vertically-center about">
<h3>About</h3>
<p>
Hi, I'm John Lesko! This is my art portfolio where I share all of my favorite work. When I'm not creating things, I enjoy excercising, playing video games, drinking good Kool Aid, and more.</p>
<p>If you'd like to follow me on Twitter, my username is
#jletsgo.
</p>
</article>
</main>
SNIPPET 2
$('#b1').click(function() {
$('body').toggleClass('R S');
});
$('#b2').click(function() {
$('#N1,#N2,#N3').toggleClass('N M');
});
$('input[id$="2"]').on('input', function() {
var grp = "." + $(this).attr('class');
var num = parseInt($(this).val(), 10);
grp !== '.S' ? $('section' + grp).css('left', num + '%') : $('section.S').css('margin-left', num + '%');
});
$('input[id$="3"]').on('input', function() {
var grp = "." + $(this).attr('class');
var num = parseInt($(this).val(), 10);
grp !== '.S' ? $('section' + grp).css('top', num + '%') : $('section.S').css('margin-top', num + '%');
});
html,
body {
height: 100%;
width: 100%;
}
body {
overflow: scroll;
font: 400 12px/1.2 Consolas;
}
section {
width: 50px;
height: 150px;
border: 2px dashed grey;
text-align: center;
color: white;
}
.R {
position: relative;
background: rgba(0, 0, 255, .3)
}
.A {
position: absolute;
background: rgba(255, 0, 0, .3)
}
.F {
position: fixed;
background: rgba(0, 255, 0, .3)
}
.S {
position: static;
background: rgba(122, 122, 0, .3)
}
.N {
position: absolute;
background: yellow;
color: blue;
}
.M {
position: relative;
background: black;
color: yellow;
}
#R1 {
left: 20%;
top: 3%;
z-index: 1;
}
#A1 {
left: 42%;
top: 44%;
z-index: 2;
}
#F1 {
right: 20%;
top: 44%;
z-index: 3;
}
#S1 {
margin-left: 0;
margin-top: -28%;
}
#N1 {
bottom: 0;
right: 0;
width: 25px;
height: 80px;
z-index: 4;
}
input {
width: 6ex;
position: static !important;
}
button {
font: inherit;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body class='S'>
<fieldset>
<button id='b1'>Body Relative/Static</button>
<button id='b2'>Nested Absolute/Relative</button>
<br><br> RLeft
<input id='R2' class='R' type='number' value='20'> RTop
<input id='R3' class='R' type='number' value='3'> ALeft
<input id='A2' class='A' type='number' value='44'> ATop
<input id='A3' class='A' type='number' value='44'><br> FLeft
<input id='F2' class='F' type='number' value='64'> FTop
<input id='F3' class='F' type='number' value='44'> SLeft
<input id='S2' class='S' type='number' value='0'> STop
<input id='S3' class='S' type='number' value='-28'><br> NLeft
<input id='N2' class='N' type='number' value='45'> NTop
<input id='N3' class='N' type='number' value='45'>
</fieldset>
<section id='R1' class='R'>RELATIVE
<section id='N1' class='N'>N<br>E<br>S<br>T<br>E<br>D</section>
</section>
<section id='A1' class='A'><br><br><br>ABSOLUTE</section>
<section id='F1' class='F'><br><br>FIXED</section>
<section id='S1' class='S'><br><br><br><br><br>STATIC</section>
</body>
You can achieve this by using flexboxwith a lot less code. The below code will do the trick.
.clearfix {
display: flex;
align-items: center;
}
img {
width: 150px;
border-radius: 50%;
}
.about-container {
width: 70%;
padding-left: 30px;
}
Check it out in codepen http://codepen.io/anon/pen/OWYxrb