How to align elements vertically, without removing element body? [duplicate] - html

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed last year.
I am trying to make a LoL website, and I have a problem trying to display the list of matches, because I don't know how to align vertically the content of each match without leading to another problem. Initially, I have this
<li>
<div class="match_li lost_match">
<div class="match_li_champ">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/champion/Ekko.png"
alt="Ekko"
title="Ekko">
</div>
<div class="match_li_info">
<h3>3/13/14</h3>
<h6>ARAM</h6>
</div>
<div class="match_li_extra">
<img src="../../resources/images/lol_assets/farm_icon.png">
<h5>22<h5>
<br>
<img src="../../resources/images/lol_assets/gold_icon.png">
<h5>12838<h5>
</div>
<div class="match_li_items">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/3157.png">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/3916.png">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/1026.png">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/3089.png">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/6655.png">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/3020.png">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/item/2052.png">
</div>
<div class="match_li_spells">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/spell/SummonerFlash.png" alt="Flash" title="Flash">
<img src="https://ddragon.leagueoflegends.com/cdn/12.1.1/img/spell/SummonerBarrier.png" alt="Barrier" title="Barrier">
</div>
<div class="match_li_time">
<h4>23:20 • 2022/01/09</h4>
</div>
<div class="match_li_expand">
<h4>▼</h4>
</div>
</div>
</li>
and this CSS
.match_li {
display: flex;
flex-direction: row;
margin-bottom: 20px;
padding: 20px 30px;
color: rgba(0, 0, 0, 0.6);
box-shadow: 0 4px 6px 0 rgba(0, 0, 0, 0.2), 0 6px 15px 0 rgba(0, 0, 0, 0.2);
}
.match_li div {
margin-right: 40px;
background-color: lightgreen;
}
.won_match {
background-color: rgba(184, 212, 242, 0.9);
}
.lost_match {
background-color: rgba(244, 144, 144, 0.9);
}
.match_li_champ img {
display: block;
width: 80px;
border-radius: 50%;
}
.match_li_info {
width: 100px;
}
.match_li_extra {
width: 100px;
}
.match_li_extra img {
width: 20px;
}
.match_li_extra h5 {
display: inline;
}
.match_li_items {
display: flex;
flex-direction: row;
}
.match_li_items img {
display: block;
width: 40px;
height: 40px;
margin-right: 3px;
}
.match_li_spells {
display: flex;
flex-direction: row;
}
.match_li_spells img {
display: block;
width: 40px;
height: 40px;
margin-right: 3px;
}
When I try to align vertically using position and top, this happens:
new CSS:
.match_li_time {
position: relative;
}
.match_li_time * {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Does anyone know why the div body dissappears? And why does the text is contracted? Any source for better understanding of this topic would be appreciated c:

When you set the element to position:absolute it is removed from the normal flow so the parent is effectively empty hence the collapse. You can try using flex:
.match_li_time {
display: flex;
justify-content: center;
align-items: center;
}
More about flex here

Container with only position absolute children will not have width and height.
try css below on your prev version before position absolute
.match_li div {
align-items: center;
margin-right: 40px;
background-color: lightgreen;
}

Related

CSS <div> strange top padding [duplicate]

This question already has answers here:
What is the default padding and/or margin for a p element (reset css)?
(5 answers)
Closed 1 year ago.
Im new to web development and currently developing a webapp for my mothers company.
I'm stuck on writing text block above the background image and I'm getting strange top padding in the div. I can't figure out where that comes from so I can remove it and work from there, adding additional padding and applying more css to the .text-block.
Talking about this one:
$(window).scroll(function() {
var sticky = $('.main-banner'),
scroll = $(window).scrollTop();
});
html,
body {
font-family: cursive, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
}
.main-banner {
width: 100%;
height: 60px;
background-color: #51ad89;
box-shadow: 2px 2px 5px grey;
position: fixed;
z-index: 1;
top: 0;
left: 0;
right: 0;
}
.main-nav {
display: flex;
justify-content: center;
align-items: center;
}
.main-nav a {
text-decoration: none;
color: black;
font-size: 18px;
text-shadow: 2px 2px 5px #ccdbd5;
margin: 0px 80px 0px 80px;
}
.main-nav a:hover {
/*text-decoration: underline;*/
color: #e6f0ec;
}
.main-nav img {
display: flex;
align-items: center;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.main-div img {
width: 100%;
}
.main-div {
position: relative;
}
.text-block {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.678);
color: white;
display: flex;
flex-direction: column;
align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="main-banner" id="stickyHeader">
<nav class="main-nav">
О студии
Услуги
<img src="/src/tree.png" height="60px" width="60px">
Работы
Контакты
</nav>
</header>
<section class="main-div">
<img src="/src/mainphoto.png">
<div class="text-block">
<p>СТУДИЯ «GREEN ERA»</p>
<p>АВТОРСКИЙ ЛАНДШАФТНЫЙ ДИЗАЙН</p>
<button>Консультация дизайнера</button>
</div>
</section>
You can set the margin on the top to 0 for your <p> elements;
p {
margin-top: 0;
}
It is because of <p> tag's default margin see the picture.
Use a <span> or <div> instead.

Center Vertically Image and UL both at Once (HTML CSS) [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 2 years ago.
I am actually trying to design the navbar for website and I am facing issue , I have one logo to show on the left and UL to right side on nav , I have created both and now I want them both to be center-vertical in their div container , unfortunately I am not able to center-vertical UL tag , need some help in css or html !
Here I am attaching the html
header {
height: 100vh;
background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(../../img/static/cover.jpg);
background-size: cover;
background-repeat: no-repeat;
}
.div_nav {
overflow: auto;
width: 80%;
margin: 0px auto;
padding: 16px 32px;
background-color: rgba(139, 0, 139, 0.459);
}
.nav_logo {
background-color: darkorange;
height: 72px;
width: auto;
}
.nav_ul {
background-color: darkslateblue;
float: right;
margin: auto 0px;
list-style: none;
}
.nav_ul li {
margin-left: 16px;
display: inline-block;
}
.nav_ul li a {
color: #fff;
text-decoration: none;
text-transform: uppercase;
font-family: 'Montserrat', sans-serif;
}
<header>
<nav>
<div class="div_nav">
<img src="././img/static/logo.png" alt="Bbooster_Logo" class="nav_logo">
<ul class="nav_ul">
<li>Para</li>
<li>Sagro</li>
<li>Miasa</li>
<li>Varga</li>
</ul>
</div>
</nav>
</header>
here I am attaching the image of output which I am currently getting
add flex rules for the .div_nav class and remove float: right from the .nav_ul class, since justify-content: space-between is used
header {
height: 100vh;
background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(../../img/static/cover.jpg);
background-size: cover;
background-repeat: no-repeat;
}
.div_nav {
display: flex;
align-items: center;
justify-content: space-between;
overflow: auto;
width: 80%;
margin: 0px auto;
padding: 16px 32px;
background-color: rgba(139, 0, 139, 0.459);
}
.nav_logo {
background-color: darkorange;
height: 72px;
width: auto;
}
.nav_ul {
background-color: darkslateblue;
/*float: right;*/
margin: auto 0px;
list-style: none;
}
.nav_ul li {
margin-left: 16px;
display: inline-block;
}
.nav_ul li a {
color: #fff;
text-decoration: none;
text-transform: uppercase;
font-family: 'Montserrat', sans-serif;
}
<header>
<nav>
<div class="div_nav">
<img src="././img/static/logo.png" alt="Bbooster_Logo" class="nav_logo">
<ul class="nav_ul">
<li>Para</li>
<li>Sagro</li>
<li>Miasa</li>
<li>Varga</li>
</ul>
</div>
</nav>
</header>
Well, you want to say horizontal UL. You can use flex for this..
.div_nav {
width: 80%;
margin: 0px auto;
padding: 16px 32px;
background-color: rgba(139, 0, 139, 0.459);
/* Edit */
display: flex;
align-items: center;
/* This centers the elements consisting inside a flexible row div */
justify-content: space-between;
/*to keep the maximum space between consisting childs */
}
Remove the float from nav_ul

How can i get 4 divs next to each other [duplicate]

This question already has answers here:
How do you make div elements display inline?
(20 answers)
Closed 4 years ago.
Hello everyone i have a header with a div in my header with a pic in it but i want to display my pics next to eachother but instead of that its displays it under each other how can i fix this i tried to change my css but didnt work.
its now like this when i add anoter div
How it is now:
I want it like this:
this is my code
#import url('https://fonts.googleapis.com/css?family=Josefin+Sans:400,400i,600,600i');
html,
body {
margin: 0;
height: 120%;
font-family: 'Josefin Sans', sans-serif;
}
.header {
position: relative;
overflow: hidden;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: flex-start;
align-content: flex-start;
height: 50vw;
min-height: 400px;
max-height: 550px;
min-width: 300px;
color: #eee;
}
.header:before {
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
-webkit-backface-visibility: hidden;
-webkit-transform: translateZ(0) scale(1.0, 1.0);
transform: translateZ(0);
background: #1B2030 url(https://unsplash.it/1999/999?image=1063) top center no-repeat;
background-size: cover;
background-attachment: fixed;
animation: grow 60s linear 10ms infinite;
transition: all 0.2s ease-in-out;
z-index: -2
}
.header a {
color: #eee
}
.info {
width: 100%;
padding: 25% 10% 0 10%;
text-align: center;
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.2)
}
.author {
display: inline-block;
width: 50px;
height: 50px;
border-radius: 50%;
background: url(http://favim.com/media/uploads/images/610/140308/black-n-white-cute-funny-iron-man-Favim.com-1462744.jpg) center no-repeat;
background-size: cover;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.3);
margin-bottom: 3px
}
.info h4,
.meta {
font-size: 0.7em
}
.meta {
font-style: italic;
}
.twtr {
margin-top: 100px
}
.btn.twtr:after {
content: "\1F426";
padding-left: 5px
}
<body>
<div class="header">
<div class="info">
<h1>TestHeader</h1>
<div class="meta">
test
</div>
<div class="meta">
<br> By James Nodws on May 30, 2017
</div>
</div>
</div>
</body>
You First Have clear the body from margin and padding just in case
body{padding:0;margin:0;}
after that give each div 25% width and display either inline or inline-block
div{width:25%;display:inline;}
Add :
.meta {
display: inline-block
}
div are int display mode block by default, so it wraps the entire ligne. Change to inline-block to allow multiple divs horizontally
You have two options here
You can use Bootsrap as described in here.
Use below CSS
.meta {
float:left;
width: 30%;
}
You can use any other class which is applied only for image divs.
I think you should try using .
.meta {
display : flex;
}
This also aligns all the the <divs> in a single row
you can use flex property for,
Display:flex apply flex layout to the flex items or children of the container only. So, the container itself stays a block level element and thus takes up the entire width of the screen. ... Display:inline-flex apply flex layout to the flex items or children as well as to the container itself.

HTML and CSS Navigation image hovering and Main content appearing underneath bar

Code: https://hastebin.com/otemiveduh.xml
Problem Description: There are two problems being that when another element, such as p-tag, is added underneath the section for the navigation bar. It'll be hidden underneath the navigation bar. In addition, the second error is that when highlighting over the image it doesn't change the background-colour of the whole entire height and if I change any of the other code in different sections, it ruins the vertical aligning of the image. I've been stuck with this error(s) for awhile and have to turn to the community for aid.
<HTML>
<Head>
<style>
body {
margin: 0;
}
div.Header {}
div.Navigation {
padding: 0;
margin: 0;
list-style: none;
line-height: 50px;
height: 50px;
width: 100%;
background-color: #001a33;
position: absolute;
overflow: hidden;
z-index: 2;
flex-direction: row;
display: flex;
align-items: center;
justify-content: space-around;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}
div.Navigation>a {
display: block;
flex-grow: 1;
text-align: center;
}
div.Navigation a img {
height: 22.1;
width: 44;
vertical-align: middle;
}
div.Navigation a:visited,
div.Navigation a:active,
div.Navigation a:link {
color: white;
text-decoration: none;
}
div.separator {
margin-left: 60%;
}
div.Navigation a:hover {
background-color: #000d1a;
}
</style>
</Head>
<Body>
<div class="Header">
<div class="Navigation">
<img src="icons/home-button.png" />
Ongoing Projects
Purchase Service
<div class="separator"></div>
Employment
Portfolio
About
</div>
</div>
<div class="Main">
</div>
<div class="Footer">
</div>
</Body>
</HTML>
You are using absolute position on the navigation so you need to add padding to main content in order to be able to add content without going under the navigation. You have also a useless element to make the separation between both parts of the navigation. Instead you can simply use margin-left:auto.
You are also missing units in the height/width of the image :
body {
margin: 0;
}
div.Navigation {
padding: 0;
margin: 0;
list-style: none;
line-height: 50px;
height: 50px;
width: 100%;
background-color: #001a33;
position: absolute;
overflow: hidden;
z-index: 2;
flex-direction: row;
display: flex;
align-items: center;
justify-content: space-around;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}
div.Navigation>a {
display: block;
text-align: center;
padding:0 10px;
}
div.Navigation>a:nth-child(4) {
margin-left: auto;
}
div.Navigation a img {
height: 22.1px;
width: 44px;
vertical-align: middle;
}
div.Navigation a:visited,
div.Navigation a:active,
div.Navigation a:link {
color: white;
text-decoration: none;
}
div.Navigation a:hover {
background-color: #000d1a;
}
.Main {
padding-top:55px;
}
<div class="Header">
<div class="Navigation">
<img src="https://lorempixel.com/100/100/" />
Ongoing Projects
Purchase Service
Employment
Portfolio
About
</div>
</div>
<div class="Main">
<p>Content</p>
</div>
<div class="Footer">
</div>

Issue with Flexbox Height Growth

The issue I am having is with my Flexbox growing in height when I enter content inside of it. I am not 100% sure why this is happening, but I believe it has something to do with it not being in an absolute position and thus keeping its height property.
Included is the CodePen where I have the HTML and CSS setup for the Flexbox style site that I am making (Also included the Snippet below) Here is so info about it
body p {
color: white;
font-size: 30px;
margin-top: 10px !important; }
.home-wrap {
display: flex;
min-height: 100vh;
justify-content: center;
transition: background 1.5s ease-in-out; }
.home-sections {
display: flex;
flex-direction: row;
flex-wrap: wrap;
min-width: 775px;
max-width: 775px; }
.home-sections section {
border-radius: 20px;
border: 3px solid white;
overflow: hidden; }
.home-sections .flex {
display: flex;
flex-direction: row; }
.home-sections .flex--center {
justify-content: center; }
.col-1 {
text-align: center; }
.alignTop {
display: flex;
flex-direction: row;
flex-wrap: wrap;
min-width: 100%;
align-items: flex-start;
transition: opacity 1.5s ease-in-out; }
.alignCenter {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
position: relative;
width: 50%; }
.alignBottom {
display: flex;
flex-direction: row;
flex-wrap: wrap;
min-width: 100%;
align-items: flex-end;
transition: opacity 1.5s ease-in-out; }
section.about {
position: absolute;
width: 100%;
height: 220%;
left: 100%;
box-shadow: -6px 6px 8px rgba(0, 0, 0, 0.6);
z-index: 1;
transition: transform 1.5s ease-in-out;
animation: enticing-grow 2s 2;
background-color: #CB7E15; }
section.about .displayB {
display: block;
text-align: center; }
section.about .displayB h1 {
margin-top: 10px;
text-align: center;
font-size: 30px;
text-shadow: 5px 5px 8px white; }
section.about .displayB p {
font-size: 16px; }
section.about .mike-faq {
display: flex;
opacity: 0;
flex-direction: row;
justify-content: space-around;
flex-wrap: wrap;
max-width: 100%;
transition: all 1.5s ease-in-out; }
section.setup {
width: calc(55% - 6px);
height: 80%;
box-shadow: -8px 8px 8px rgba(0, 0, 0, 0.6);
background-color: #004EFF; }
section.social {
width: calc(45% - 6px);
height: 55%;
box-shadow: 8px 8px 8px rgba(0, 0, 0, 0.6);
opacity: 0.5;
transition: all 1s ease-in-out;
background-color: #585656; }
section.social:hover {
opacity: 1;
transform: scale(1.5);
z-index: 2; }
section.social .displayB {
display: block; }
section.social .displayB p {
text-align: center;
margin: 10px; }
section.social .social-wrap {
display: flex;
flex-direction: row;
max-width: 100%;
justify-content: space-around; }
section.social .social-wrap .twitter-logo {
background-image: url(/assets/img/twitter-logo.gif);
background-size: contain;
width: 55px;
height: 55px;
box-shadow: 0 5px 20px 0 #1DA1F2; }
section.social .social-wrap .email-logo {
background-image: url(/assets/img/gmail-logo.png);
background-size: contain;
width: 55px;
height: 55px;
box-shadow: 0 5px 20px 0 #E70F10; }
section.social .social-wrap .facebook-logo {
background-image: url(/assets/img/facebook-logo.png);
background-size: contain;
width: 55px;
height: 55px;
box-shadow: 0 5px 10px 5px #3C5A98; }
section.social .social-wrap .github-logo {
background-image: url(/assets/img/github-logo.png);
background-size: contain;
width: 55px;
height: 55px;
box-shadow: 0 5px 20px 0 #E04006; }
section.twitch {
width: calc(55% - 6px);
height: 80%;
box-shadow: -8px -8px 8px rgba(0, 0, 0, 0.6);
background-color: #5A3E85; }
section.twitch h1 {
margin-top: 10px;
font-size: 30px;
text-align: center; }
section.twitch p {
font-size: 16px; }
section.twitch .displayB {
display: block;
text-align: center; }
.button {
color: #5A3E85; }
section.webdesign {
width: calc(45% - 6px);
height: 53%;
box-shadow: 8px -8px 8px rgba(0, 0, 0, 0.6);
background-color: #1F843A; }
section.youtube {
position: absolute;
width: 95%;
height: 140%;
margin-right: 5px;
box-shadow: -8px 0 8px rgba(0, 0, 0, 0.6);
transition: opacity 1.5s ease-in-out;
background-color: #CC181E; }
<div class="home-wrap">
<div class="home-sections">
<div class="alignTop">
<section class="twitch">
<div class="flex flex--center displayB">
<h1>Twitch</h1>
<!-- <p>I've been livestreaming on Twitch since it's days as Justin.tv - I plan to pick it back up again once I can figure out a schedule</p> -->
</div>
</section>
<section class="webdesign">
<div class="flex flex--center">
<p>Web Design</p>
</div>
</section>
</div>
<div class="alignCenter">
<section class="youtube">
<div class="flex flex--center">
<P>YouTube</P>
</div>
</section>
<section class="about grown">
<div class="flex flex--center displayB">
<h1>What's Going on, Mike here, and Welcome to my Site!</h1>
<p>I'm your typical 26 year old nerd, who loves computers, video games, pretty much anything tech. Along with that, I have a ton of hobbies that range from playing music to stock investments. </p>
<p>I've always been interested in, and worked on my web design skills, but it wasn't until the past few months where I decided this is what I am passionate about.</p>
<p>With that said, click around the site to learn more about me. All panels are expandable clickable which will allow you to learn more about "Mike" and who I am!</p>
<div class="mike-faq">
<p>FAQ 1</p>
<p>FAQ 2 </p>
<p>FAQ 3</p>
<p>FAQ 4</p>
</div>
</div>
</section>
</div>
<div class="alignBottom">
<section class="setup">
<div class="flex flex--center">
<p>Setup</p>
</div>
</section>
<section class="social">
<div class="flex flex--center displayB">
<p>Social</p>
<div class="social-wrap">
<div class="twitter-logo"></div>
<div class="email-logo"></div>
<div class="facebook-logo"></div>
<div class="github-logo"></div>
</div>
</div>
</section>
</div>
</div>
1 - I have a wrapping div around the site content so I can control the flow and background of the back
2 - I have another wrapping div for each "Row" of Flexbox so I can have them align in different areas (Top is aligned Flex-Start, Center is aligned Flex-Center and Bottom is aligned Flex-Bottom)
3 - The 2 Center Flexboxes (YouTube and About) are set with Absolute position so I am able to have the "About" section grow out and overlap the top and bottom sections.
4 - I have all the fleboxes set with % height and width so they will scale with the window value.
5 - Inside of each FlexBox section, I have a container so that I can manipulate the content and move it around so it fits within their parent flexbox.
Now, I am not 100% sure if all the nesting and parent/child elements is the best way to handle what I am trying to do, but I haven't been able to visualize it any other way and get it work (outside of the way I am doing it).
So - to go over it again, inside of the "Twitch: panel I have a <p> section that when I uncomment, the flexbox will grow in height, rather than remain the same. However, in the "About" section, this doesn't happen. The only difference I am able to see is that the "About" section is position: absolute.
You can play around by giving alignTop, alignCenter and alignBottom heights.
http://codepen.io/anon/pen/GWEgMK : try commenting in and out what you said then find the adequates values for heights you are happy with.
.alignTop {
height:33%;}
.alignCenter {
height:25%;}
.alignBottom {
height:20%;}
Not sure that's exactly what you want but to be honnest there is way too much clutter in your codepen for us to really want to try :) Are the animations even relevant ?
rather than % heights you may want to let flexbox handle the heights for you, as in grow to the viewport as it is designed to do but then use a max-height with a vh unit rather than a % value (like max-height: 75vh instead of max-height:75%). vh uses the viewport to scale not the parent div.