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
Related
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;
}
This question already has answers here:
How to remove the stripes that appears when using linear gradient property [duplicate]
(2 answers)
Closed 3 years ago.
I designed the main page of a website, but when I try to put the background colors, these are repeated many times I have already researched for 1 hour and I have not found a solution.
I've tried deleting DISPLAY: FLEX; and deleting the content of BODY but nothing happens.
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
background: conic-gradient(from 180deg,#86BBD8, #86BBD8, #5881AB, #5881AB);
}
header {
display: flex;
width: 90%;
height: 10vh;
margin: auto;
align-items: center;
}
nav {
flex: 1;
}
.nav-links {
display: flex;
justify-content: space-around;
list-style: none;
}
.nav-link {
text-decoration: none;
font-size: 24px;
line-height: 36px;
color: #564787;
text-shadow: 0px 4px 10px rgba(0, 0, 0, 0.25);
}
<body>
<header>
<nav>
<ul class="nav-links">
<li>Inicio</li>
<li>Institución</li>
<li>Noticias</li>
<li>Contacto</li>
</ul>
</nav>
</header>
</body>
I want the Conic-Gradient not to repeat itself
A simple solution would be to add height: 100vh; to your body styling. That will cause the body to expand vertically to fill the height of the viewport and, by default, a single conic pattern will scale and center to fit the viewport via the body element.
This addition to your CSS should produce the desired result:
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
/* Added no-repeat here */
background: conic-gradient(from 180deg, #86BBD8, #86BBD8, #5881AB, #5881AB);
/* Added height property here */
height: 100vh;
}
header {
display: flex;
width: 90%;
height: 10vh;
margin: auto;
align-items: center;
}
nav {
flex: 1;
}
.nav-links {
display: flex;
justify-content: space-around;
list-style: none;
}
.nav-link {
text-decoration: none;
font-size: 24px;
line-height: 36px;
color: #564787;
text-shadow: 0px 4px 10px rgba(0, 0, 0, 0.25);
}
<header>
<nav>
<ul class="nav-links">
<li>Inicio</li>
<li>Institución</li>
<li>Noticias</li>
<li>Contacto</li>
</ul>
</nav>
</header>
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.
I have header tag, which has full width/height image on the background.
I need to algin elements in the center of that page exactly as picture shows.
First goes image, than text, than buttons one after another one and on the very bottom of the page is slider
apart from slider, I've managed to put all content to the center with code below
Also, I'm using boostrap and jquery if needed
It's one page design, this is just a header
Thank you for any help :)
<header>
<img src="" alt="full">
<h1>The</h1>
Login
Browse
<div class="swiper-container">
</div>
</header>
SCSS
header {
margin-top: -56px !important;
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../img_cover_m.jpg);
background-size: cover;
background-position: center;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
img {
margin-top: 5%;
max-width: 50%;
height: auto;
}
h1 {
color: #fff;
}
.btn {
#include Myriad-Pro-Light;
}
.btn-green {
margin-top: 5%;
color: #fff;
}
.btn-grey {
color: #000;
}
.swiper-container {
margin-top: 5%;
width: 100%;
height: 300px;
}
}
You can achieve this by using the following properties at the appropriate places.
text-align:center;
display:flex;
align-center;
margin:0 auto;
Also as your using bootstrap so some of the classes may get overwritten so make sure to use !important on properties that you want to force your setting on.
Using your code i have made some changes using the above mentioned suggestions and here is the code:
<header>
<img src="http://placehold.it/350x150" alt="full">
<h1>The</h1>
Login
Browse
<div class="swiper-container">
<p>Large full width div, which must stay on the bottom of the header tag</p>
</div>
</header>
header {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../img_cover_m.jpg);
background-size: cover;
background-position: center;
height: 100vh;
min-width: 20%;
display: flex;
flex-direction: column;
align-items: center;
}
img {
margin: 0 auto;
display: block;
max-width: 50%;
height: auto;
}
h1 {
color: #fff;
text-align:center;
}
.btn {
#include Myriad-Pro-Light !important;
margin: 10px auto !important;
display: block !important;
width: 25vw !important;
text-align:center !important;
padding: 6px 0px !important;
}
.btn-green {
color: #fff;
display:block;
}
.btn-grey {
color: #000;
display:block;
}
.swiper-container {
background-color:red;
width: 100%;
height: 300px;
position:absolute;
bottom:0;
display: flex;
align-items: center;
justify-content: center
}
.swiper-container p{
#include Myriad-Pro-Light !important;
margin:0 auto;
color:white;
font-size: 18px;
}
Here is a sample using this code. http://codepen.io/Nasir_T/pen/PboZme
I am trying to make a navigation bar in which the buttons' text will be aligned in the center vertically.
Currently, everything is working fine with the navigation bar besides the vertical align.
I have tried many methods such as with line height, padding to the top and bottom (messes up my heights so the text divs overflow), flex, and table display.
html,
body {
height: 100%;
margin: 0px;
}
#nav {
height: 10%;
background-color: rgb(52, 152, 219);
position: fixed;
top: 0;
left: 0;
width: 100%;
color: white;
font-family: Calibri;
font-size: 200%;
text-align: center;
display: flex;
}
#nav div {
display: inline-block;
height: 100%;
align-items: stretch;
flex: 1;
}
#nav div:hover {
background-color: rgb(41, 128, 185);
cursor: pointer;
}
<div id="main">
<div id="nav">
<div><a>Home</a></div>
<div><a>Page2</a></div>
<div><a>Page3</a></div>
<div><a>Page4</a></div>
<div><a>Page5</a></div>
</div>
</div>
All help is appreciated, thank you!
You can use the table and table-cell method. Basically you need to add the css property display: table to the parent element and display: table-cell; vertical-align: middle to the children ones.
Increased height for demo purpose.
#nav {
height: 50%;
background-color: rgb(52, 152, 219);
position: fixed;
top: 0;
left: 0;
width: 100%;
color: white;
font-family: Calibri;
font-size: 200%;
text-align: center;
display: table;
}
#nav div {
display: table-cell;
height: 100%;
vertical-align: middle;
}
#nav div:hover {
background-color: rgb(41, 128, 185);
cursor: pointer;
}
<div id="main">
<div id="nav">
<div><a>Home</a>
</div>
<div><a>Page2</a>
</div>
<div><a>Page3</a>
</div>
<div><a>Page4</a>
</div>
<div><a>Page5</a>
</div>
</div>
</div>
With flexbox, you were very close.
Because a flex formatting context exists only between parent and child, your display: flex on the #nav container was reaching the divs, but not the anchors.
You need to make the individual divs flex containers, as well, so flex alignment properties can apply to the anchor elements.
html,
body {
height: 100%;
margin: 0px;
}
#nav {
height: 10%; /* This value will hide the nav bar on smaller windows */
background-color: rgb(52, 152, 219);
position: fixed;
top: 0;
left: 0;
width: 100%;
color: white;
font-family: Calibri;
font-size: 200%;
text-align: center;
display: flex; /* Will apply to child div elements, but not anchor elements */
}
#nav div {
/* display: inline-block; */
height: 100%;
align-items: stretch;
flex: 1;
display: flex; /* NEW; nested flex container */
justify-content: center; /* NEW; align anchor elements horizontally */
align-items: center; /* NEW; align anchor elements vertically */
}
#nav div:hover {
background-color: rgb(41, 128, 185);
cursor: pointer;
}
<div id="main">
<div id="nav">
<div><a>Home</a></div>
<div><a>Page2</a></div>
<div><a>Page3</a></div>
<div><a>Page4</a></div>
<div><a>Page5</a></div>
</div>
</div>