Align SVG and Text in a column CSS - html

I am currently creating a website and i am having problems with the mobile layout. My mobile navbar is at the bottom of the screen and the text should be directly under the displayed SVG.
This is how it currently looks. I tried using both flexbox and grid but could get it to work. margin-left was also an option but then the left button is too farther away from the border then most right button.
Currently my Code looks like this:
CSS:
#media only screen and (max-width: 981px){
.navbar{
bottom: 0;
width: 100vw;
height: 5.5rem;
padding-top: -0.1rem;
}
.logo{
display: none;
}
.navbar-nav{
flex-direction: row;
}
.nav-link {
justify-content: center;
height: 100%;
color: var(--accent-color);
}
main{
margin: 0;
margin-bottom: 4rem;
}
.nav-link svg {
width: 2.3rem;
min-width: 2rem;
margin: 0 1.5rem;
margin-top: -1rem;
}
.nav-item .link-text {
display: flex;
}
.nav-item {
display: flex;
flex-direction: column;
}
}
HTML/JS:
function Navbar() {
return (
<header>
<nav className="navbar">
<ul className="navbar-nav">
<li className="logo">
<a href="/" className="nav-link">
<svg id="svg" xmlns="http://www.w3.org/2000/svg"
viewBox="0, 0, 400,368.05555555555554">
</svg>
<span className="link-text">CCR</span>
</a>
</li>
<li className="nav-item">
<a href="/" className="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" className="svg" viewBox="0 0 576 512"></svg>
<span className="link-text home">Start</span>
</a>
</li>
<li className="nav-item">
<a href="/training" className="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" className="svg" viewBox="0 0 576 512"></svg>
<span className="link-text training">Training</span>
</a>
</li>
<li className="nav-item">
<a href="/about" className="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" className="svg" viewBox="0 0 640 512"></svg>
<span className="link-text about">Über uns</span>
</a>
</li>
<li className="nav-item">
<a href="/gallary" className="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" className="svg" viewBox="0 0 512 512"></svg>
<span className="link-text gallary">Bildgalerie</span>
</a>
</li>
<li className="nav-item">
<a href="/contact" className="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" className="svg" viewBox="0 0 512 512</svg>
<span className="link-text contact">Kontakt</span>
</a>
</li>
</ul>
</nav>
</header>
)
}
Note: I edited out all SVG Files to shorten the code

I believe the problem is with your use of flex-box. When you go to use display: flex; the affected elements are only the direct children, not the children's children. In this case you put flex-direction: columns; a nav-item, but the only thing that will go to column is the element a(nav-link), and since it is the only child the difference will be zero.
So, intead of
.nav-item {
display: flex;
flex-direction: column;
}
try this
.nav-link {
display: flex;
flex-direction: column;
}

Related

Why can you take nav links out of the navbar container, and re-flow in HTML navbar/header tag? Will that cause layout/performance issues?

Suppose I have a Navbar wrapped by header tag with links on it, I structured it this way
<header id="header" class="header active">
<nav class="nav">
<!-- NAV LOGO -->
<div class="nav__logo">
<a href="">
<img src="./img/logo.svg" alt="" class="nav__img">
</a>
</div>
<!-- NAVLINKS -->
<ul class="nav__list">
<li class="nav__item">
Home
</li>
<li class="nav__item">
About
</li>
<li class="nav__item">
Courses
</li>
<li class="nav__item">
Contact
</li>
</ul>
<!-- ACTIONS -->
<div class="nav__actions">
<button>
<i class='bx bx-search'></i>
</button>
<button>
<i class='bx bx-cart' ></i>
</button>
<button>
<i class='bx bx-menu' ></i>
</button>
</div>
</nav>
</header>
And on my CSS,
header{
background: white;
padding: 1.25rem;
position: relative;
z-index: 10;
}
.header .active{
position: fixed;
background: white;
box-shadow: rgba(0, 0, 0, 0.3);
}
nav{
display: flex;
align-items: center;
justify-content: space-between;
}
.nav__logo{
flex-shrink: 0;
flex-wrap: nowrap;
}
.nav__list{
display: flex;
align-self: center;
justify-content: center;
padding: 0 1.25rem;
gap: 2.25rem;
}
.nav__link{
}
.nav__actions{
display: flex;
gap: 1.25rem;
padding: 0px 1rem;
}
My questions are quite simple, yet I would like to confirm them somehow.
If I set my media queries and change my CSS nav__list - if it hits (max-width: 420px) to become a side menu by taking it out of the flow by .nav__list: position:fixed, will that cause problems and can be seen in dev tools? Like overlapping, layouts/performance, Is this the right way? Just take out certain elements even if they are under one root layout (header>nav - nav_list)?
Or do I need to create a new dedicated (menu list) for the sidebar menu?
P.S: I come from Android background, XML layouting is different, they are structured in a way by not taking certain elements out of the tag/flow.

Separated link and image in list items

Anyone knows, why does it happen? It looks like images and links in list items are separated. I can't really understand it, because it looks really strange. I have always seen list items that include both images and links under one border. Thx in advice.
html {
box-sizing: border-box;
}
*,
*::after,
*::before {
box-sizing: inherit;
}
body {
background: #070A1B;
font-size: 16px;
line-height: 120%;
font-weight: 400;
color: #fff;
font-family: 'Roboto Condensed', sans-serif;
}
a {
text-decoration: none;
color: inherit;
}
.header__top {
justify-content: space-between;
height: 100px;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 10px;
}
.header {}
<header class="header">
<div class="container">
<div class="header__top">
<button class="header__nav">
<img class="header__nav-img" src="images/menu-btn.svg" alt="menu botton">
</button>
<a href="#" class="logo">
<img src="images/logo.svg" alt="logo" class="logo__img">
</a>
<ul class="list">
<li class="list_item">
<a href="#" class="list__link">
<img src="images/twitter.svg" alt="twitter" class="list__img">
</a>
</li>
<li class="list_item">
<a href="#" class="list__link">
<img src="images/google.svg" alt="google" class="list__img">
</a>
</li>
<li class="list_item">
<a href="#" class="list__link">
<img src="images/facebook.svg" alt="facebook" class="list__img">
</a>
</li>
</ul>
</div>
</div>
</header>
idk why, but here we go...
What you're seeing is the default vertical-align value of baseline affecting the img elements. Setting this to middle will allows them align better with the text.
let imgData = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pjxzdmcgdmlld0JveD0iMCAwIDI0IDI0IiB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIj48cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSJncmVlbiIgLz48L3N2Zz4=';
document.querySelectorAll('img').forEach(img => img.src=imgData);
img {
vertical-align: middle;
border: 1px solid black;
}
<button><img></button>
<img>
<ul>
<li><img></li>
<li><img></li>
<li><img></li>
</ul>
The link will still affect the whole image if that´s your concern.
Anyway, you can solve this by changing the "display" value in css.
Try adding "display:block;" or "display:inline-block;" to the "a" element and this way it will cover the whole image.

I have a problem about rtl my page in css, can anyone help me?

I face the problem of creating a rtl(right to left) stylesheet when I want to rtl Elements include <i> tag beside any texts or spans.
For example, there is something like this:
There are three elements inside this tag:
<li><i class="sl sl-icon-docs"></i>Requests<span>2</span></li>
First image: this is showing my theme before rtl:
An icon located before the main button title
Button title
a tag for showing number of new requests as a label.
an order should reverse and show all items inside <a> tag from the right, it means from the right we will have 1.icon 2.title 3.label
Second image: rtl by using flex-direction: row-reverse; on main content:
I applied direction: rtl; to the <li> element but nothing works, you can see the changes on second image.
You can reverse the content of the link <a> by using flex-direction: row-reverse on it :
.reverse{
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
}
<ul>
<li><i class="sl sl-icon-docs">Icon</i>Requests<span>2</span></li>
<li><i class="sl sl-icon-docs">Icon</i>Requests<span>2</span></li>
</ul>
<!-- Other examples -->
<a class="reverse">(1) Click me<i class="sl sl-icon-docs">(2) icon</i></a>
<h3 class="reverse">(1) Title example<i class="sl sl-icon-docs">(2) icon</i></h3>
RTL works but not on the <span> part. If you can change span into a division then it'll work.
Else you can use flex-direction: row-reverse after making <li> a display:flex
Here's my code:
#rtl {
direction: rtl;
}
#flex>a {
display: flex;
flex-direction: row-reverse;
}
li {
background: red;
padding: 1em;
border-radius: 3em;
margin: 1em;
text-decoration: none;
width: 200px;
text-align: center;
}
span {
background: white;
border-radius: 10em;
padding: 0 0.7em;
}
ul {
list-style-type: none;
}
<ul>
<li id="normal">
<a href="#">
<img src="https://picsum.photos/30/30?image=0" />Normal <span>1</span>
</a>
</li>
<li id="rtl">
<a href="#">
<img src="https://picsum.photos/30/30?image=0" />RTL <span>2</span>
</a>
</li>
<li id="flex">
<a href="#">
<img src="https://picsum.photos/30/30?image=0" />Row-Reverse <span>3</span>
</a>
</li>
</ul>
The code needs some polishing but I think you've got what you were looking for.
I hope this helps.
Peace 🖖

Adaptative width with flexbox and flex-grow

I'm trying to set up a menu bar with the
following template
I'm trying this with FlexBox but I can't figure out what's wrong. Here's the HTML :
<nav>
<ul>
<li id="navleft">
<img src="images/logo.png" alt="logo wikihow">
</li>
<li class="navothers">
<img src="images/contribuye.png" alt="contribuye">
<p>CONTRIBUYE</p>
</li>
<li class="navothers">
<img src="images/explora.png" alt="explora">
<p>EXPLORA</p>
</li>
<li class="navothers">
<img src="images/entrar.png" alt="entrar">
<p>ENTRAR</p>
</li>
<li class="navothers"><img src="images/mensajes.png" alt="mensajes">
<p>MENSAJES</p>
</li>
</ul>
</nav>
And I apply the following styles in CSS :
nav {
padding: 0 30% 0 30%;
}
nav ul {
display:flex;
justify-content:center;
align-items:center;
background-color: #93B874;
}
#navleft{
flex-grow:32;
flex-shrink:1;
}
#navleft img{
width: 144px;
vertical-align:center
}
.navothers{
flex-grow:1;
flex-shrink:4;
}
I get the following result
My problem is that all the elements in the right part (with the "navothers" class) dont have the same width ! They just adapt depending on the size of the text they have.
I may have mixed a lot of things, what have I done wrong ?
Your flex-grow value for #navleft is too big, and your container is too small. Your items can't grow they don't have enough room. Try this :
CSS :
nav {
padding: 0 10% 0 10%; // imho it's a strange way to center your nav. Reduced to show you that you're lacking room.
}
nav ul {
display: flex;
justify-content: center;
align-items: center;
background-color: #93B874;
}
#navleft {
flex: 2; // You may tune this one but keep room for your other items !
}
#navleft img {
width: 144px;
vertical-align: center
}
.navothers {
flex: 1;
border: solid 1px #000;
}
HTML :
<nav>
<ul>
<li id="navleft">
<img src="images/logo.png" alt="logo wikihow">
</li>
<li class="navothers">
<img src="images/contribuye.png" alt="contribuye">
<p>CONTRIBUYE</p>
</li>
<li class="navothers">
<img src="images/explora.png" alt="explora">
<p>EXPLORA</p>
</li>
<li class="navothers">
<img src="images/entrar.png" alt="entrar">
<p>ENTRAR</p>
</li>
<li class="navothers">
<img src="images/mensajes.png" alt="mensajes">
<p>MENSAJES</p>
</li>
</ul>
</nav>
https://jsfiddle.net/me7t2hv3/

How to vertical allign list items with icons

Can someone help me understand how can I vertical align this?
JSFIDDLE
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/2.0.46/css/materialdesignicons.css"
></link>
<aside class="menu column is-2 full-height">
<ul class="menu-list">
<li>
<a href="#" class="">
<i class="mdi mdi-file-document-box mdi-48px"></i>
<span>Document</span>
</a>
</li>
<li>
<a href="#" class="">
<i class="mdi mdi-file-document-box mdi-48px"></i>
<span>Document</span>
</a>
</li>
</ul>
</aside>
I need align icon and text next to it?
there is some more css since I'm using bulma.io and something custom but not so relevant I think
You can delete that css and create new one if it is not ok
There's a few options at your disposal, depending on your needs.
Updated jsFiddle
Use display: inline-block; on the element(s) that are next to each other and need to be aligned. Then you can use vertical-align: [top|middle|baseline] as needed for each element
i, span {
display: inline-block;
vertical-align: middle;
}
If that alignment doesn't work for you, then you should set the vertical-align to top, to get them positioned how you like, and then you can use line-height to fine-tune the vertical positioning of each element. Example:
i, span {
display: inline-block;
vertical-align: top;
}
i {
line-height: 39px;
}
span {
line-height: 39px;
}
You can add display: flex and align-items: center rules to li a
fiddle
aside {
background-color: #0067ad;
height: 100%;
}
li a {
display: flex;
align-items: center;
}
.menu-list {
list-style: none;
}
i {
color: white;
}
a:hover,
a:active,
.router-link-active {
background-color: black;
}
span {
color: white;
}
}
a {
color: white
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/2.0.46/css/materialdesignicons.css" rel="stylesheet"/>
<aside class="menu column is-2 full-height">
<ul class="menu-list">
<li>
<a href="#" class="">
<i class="mdi mdi-file-document-box mdi-48px"></i>
<span>Document</span>
</a>
</li>
<li>
<a href="#" class="">
<i class="mdi mdi-file-document-box mdi-48px"></i>
<span>Document</span>
</a>
</li>
</ul>
</aside>