I have almost finished my web dashboard which can be seen here. Here is the source code. The last few bits I would like to improve are the mobile adaptation of the navbar and the footer. I'm I struggling to position elements properly, and on some devices, it looks worse than on others. I'm not the best when it comes to CSS yet, so I need a little bit of help here.
Few issues I have is the title not being positioned correctly:
.
I would like it to be positioned so the distance between the top and bottom is equal. And I want it to be at the same distance from the left as the burger button is from the right.
Same problem with the footer. it doesn't look very well organized:
or even worse:
I want these elements to be on an equal distance between top and bottom and never overlap, preferably all in one line. I'm sure there is a number of solutions here, but any solution that is simple and makes it looks more organized will be appraciated.
One thing I need to mention is that HTML elements are defined in Python code in Plotly Dash environment, but I'm pretty sure it makes no difference.
I'm attaching some of the Plotly HTML and CSS code here, but the full code is here:
HTML Code of the navbar:
app.layout = html.Div([
html.Nav([
html.Div("Covid-19 global data Dashboard", className="dashboard-title"),
html.A(
id="toggle-button",
children=[
html.Span(className="bar"),
html.Span(className="bar"),
html.Span(className="bar"),
],
href="#",
className="toggle-button"),
html.Div(
id="navbar-links",
children=html.Ul(
children=[
html.Li(html.A("Home", href=homeURL)),
html.Li(html.A('Source Code', href=sourceCodeURL)),
html.Li(html.A("CSV Data", href=sourceDataURL))]),
className="navbar-links active"
)]
HTML Code of the footer:
html.Footer([
html.Div("created by Sebastian Meckovski", id='footer-text'),
html.Div([
html.P(['Find Me On:'], id='find-me-on'),
html.A([html.Img(src=app.get_asset_url('linkedInLogo.png'), style={'height': '2rem'})],
href=linkedInURL),
html.A([html.Img(src=app.get_asset_url('facebookLogo.png'), style={'height': '2rem'})],
href=facebookURL)
], id='footer-links')
CSS Desktop view:
body {
background-color: var(--LightBlue);
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background-color: var(--DarkBlue);
color: white;
}
.container {
position: relative;
min-height: 100% ;
}
.dashboard-title{
font-size: 1.2rem;
margin: .5rem;
}
.navbar-links ul{
margin: 0;
padding: 0;
display: flex;
}
.navbar-links li {
list-style: none;
}
.navbar-links li a{
text-decoration: none;
color: white;
padding: 1.5rem;
display: block;
}
.navbar-links li a:hover{
background-color: var(--DarkBlueHover);
}
.toggle-button{
position: absolute;
top: .8rem;
right: 1rem;
display: none;
flex-direction: column;
justify-content: space-between;
width: 30px;
height: 21px;
}
.toggle-button .bar{
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}
#footer {
position: absolute;
display: flex;
justify-content: space-between;
align-items: center;
bottom: 0;
width: 100%;
background: var(--DarkBlue);
color: white;
height: 3.5rem;
}
#footer-links{
display: flex;
}
#find-me-on{
padding-right: 20px;
font-size: .8rem;
}
#footer-text {
margin: .5rem;
font-size: .8rem;
}
Mobile View:
#media only screen and (max-width: 500px) {
.dashboard-title {
font-size: 1rem;
padding-right: 80px;
}
.toggle-button {
display: flex;
}
.navbar-links {
display: flex;
width: 100%;
}
.navbar{
align-items: center;
flex-direction: column;
min-height:45px;
}
.navbar-links ul {
width: 100%;
flex-direction: column;
}
.navbar-links li{
text-align: center;
}
.navbar-links li a{
padding: .5rem 1rem;
}
.navbar-links.active {
display: none;
}
H2{
font-size: 15px;
}
#footer-text {
margin: .5rem;
font-size: .8rem;
}
#find-me-on{
padding-right: 15px;
}
}
For .navbar class set justify-content property to center, because if you set flex-direction to column, it "rotates" view object, so align-items starts working horizontally.
so:
.navbar{
justify-content: center;
flex-direction: column;
min-height: 45px;
}
And now for navbar horizontal centering: because you set .toggle-button to be absolute, it is set 1rem from right side, while .dashboard-title is centered inside of its parent. To fix this you can simply change margin inside of .navbar class to 1rem, it's now at 0.5rem; Also make sure that .navbar is not centered horizontally by its parent.
After these corrections navbar looks like this:
And for the footer it's also flexbox case. Your images are inside of a element, so you have to vertically center a content.
Image below is only in preview debugging purposes, set your CSS where you previously did.
a{
display: flex;
align-items: center; // Now all <a> element children are centered vertically.
}
Related
I am designing the header of my homepage with react, html and css. In the header I have a button and an h2 tag, each one inside a different div tag. I apply display:flex to these elements and with the justify-content property I try to move them.
In the div that I have the button I have the property: justify-content: end and it positions it at the end of it and that's fine, but in the div that I have the h2 tag, I put justify-content: start and it doesn't position it at the beginning from the div tag. Why does this happen ? what should I do?
EncabezadoHome.tsx:`import {useBlockchain} from "../../hooks/useBlockchain";
import './EncabezadoHome.css'
export const EncabezadoHome = () => {
const {wallet, web3Provider, connectWallet} = useBlockchain();
return (
<header>
<div className={"container-header-home"}>
<div className={"logo-header-home"}>
<h2>Emprendamos</h2>
</div>
<div className={"button-header-home"}>
<button onClick={connectWallet}>Conectar Wallet</button>
</div>
</div>
</header>
);
};
`
EncabezadoHome.css:
.container-header-home {
display: flex;
padding-top: 20px;
}
.logo-header-home {
display: flex;
flex: 1;
justify-content: start;
}
.logo-header-home h2 {
padding-left: 20px;
color: white;
}
.button-header-home {
display: flex;
flex: 1;
justify-content: end;
padding-right: 20px;
}
.button-header-home button {
color: #ffffff;
background-color: #1D9BF0;
width: 200px;
height: 50px;
border-radius: 10px;
}
The proper syntax is flex-end and flex-start. What effect do you try to achieve, other than that there are other options like space-between.
I've tested this as well and it seems to work fine. You can see the layout from this image with the flex-box outlines added via Dev Tools:
I'd try writing the CSS as follows and see if it makes a difference. I moved the padding to the container div of the h2 element to match the button container and updated the flexbox terms.
.container-header-home {
display: flex;
padding-top: 20px;
}
.button-header-home {
display: flex;
flex: 1;
justify-content: flex-end;
padding-right: 20px;
}
.logo-header-home {
display: flex;
flex: 1;
justify-content: flex-start;
padding-left: 20px;
}
.logo-header-home h2 {
color: white;
}
.button-header-home button {
color: #ffffff;
background-color: #1d9bf0;
width: 200px;
height: 50px;
border-radius: 10px;
}
I already solved the problem was that in the index.css file, I had this:
* {
padding: 0px;
margin: 0px;
margin: 0px automatic;
}
still keeping some vertical space while list-style: none is defined.
https://product-landing-page.freecodecamp.rocks/#how-it-works
If you check this project and scroll to pricing labels there is defined exactly like mine and it behave differently.
My price label:
Project's label
MY CSS:
/* ========== PRICE ======= */
#cost {
display: flex;
flex-direction: row;
justify-content: center;
}
.price-box {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
width: calc(100% / 3);
margin: 10px;
border: 1px solid #000;
border-radius: 3px;
}
.lvl {
background-color: #dddddd;
text-align: center;
font-size: 1.4em;
color: rgb(65, 65, 65);
padding: 5px;
width: 100%;
}
#cost ol li {
padding: 5px 0;
margin: 0;
}
li {
list-style: none;
}
Project's CSS:
#pricing {
margin-top: 60px;
display: flex;
flex-direction: row;
justify-content: center;
}
.product {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
width: calc(100% / 3);
margin: 10px;
border: 1px solid #000;
border-radius: 3px;
}
.product > .level {
background-color: #ddd;
color: black;
padding: 15px 0;
width: 100%;
text-transform: uppercase;
font-weight: 700;
}
.product > h2 {
margin-top: 15px;
}
.product > ol {
margin: 15px 0;
}
.product > ol > li {
padding: 5px 0;
}
Maybe someone can explain it to me so i can understand what is happening here
If you look at all the CSS that is being applied to that ol element you'll notice that there is a setting of everything (*) which includes padding: 0;
This overrides the browser's default settings for padding related to ol elements.
You may or may not want to set padding: 0 for everything at the start. It depends on your precise use case.
As in the case of the site you reference, something like this can be seen at the top of some stylesheets - meaning the author will add any padding and margin settings themselves rather than relying on default browser settings.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
[The box-sizing setting means e.g. padding and border dimensions are included in the dimensions of the element].
If you don't want such a global approach to unsetting padding then put the padding: 0 in the style setting for the ol element itself. For example: [I can't tell exactly what this should be as I don't know your HTML stucture]
#cost ol {
padding: 0;
}
Just add padding: 0
ul, ol {
list-style:none;
padding:0;
}
Everything else works in my media query code: changing the font color, repositioning elements on the screen etc.. But changing font-size never works.
The code looks like this (note that font-size is within the div without a name:
.sidebar {
position: fixed;
background-color: white;
left: 0;
bottom: 0;
top: 57px;
width: 72px;
z-index: 200;
padding-top: 4px;
.sidebar-link {
height: 74px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
cursor: pointer;
img {
height: 24px;
margin-bottom: 6px;
}
div {
font-size: 10px;
}
&:hover {
background-color: #eeeeee;
}
}
}
And like this:
#media (min-width: 1200px) {
.sidebar {
width: 250px;
.sidebar-link {
flex-direction: row;
justify-content: left;
margin-left: 22px;
}
div {
font-size: 16px;
}
}
}
In the media query div is defined as a class and not as an element.
.div {
font-size: 16px;
}
change in
div {
font-size: 16px;
}
The first section of CSS you posted sets .sidebar .sidebar-link div to 10px. But then your media query sets .sidebar div to 16px. I think this is probably a mistake rather than what you intended to write. The reason the media query isn't changing the size is that .sidebar div is a less specific selector than .sidebar .sidebar-link div. If you change the code in your media query to target .sidebar .sidebar-link div, it should work.
I'm trying to create a banner that has two buttons on it:
.banner {
width: 100%;
display: flex;
flex-direction: column;
margin-top: -4%;
}
.banner img {
width: 100%;
/*image is 1232x317 by default and defines the size of the banner*/
}
.banner-buttons {
display: flex;
flex-direction: row;
margin-left: 6.2%;
position: absolute;
top: 10%;
}
.banner button {
display: flex;
font-size: 200%;
border: none;
border-radius: 5px;
font-weight: bold;
align-items: center;
padding: 5px 15px;
}
<div class="banner">
<img src="https://picsum.photos/1200/300">
<div class="banner-buttons">
<button>Assistir</button>
<button>Mais Informações</button>
</div>
</div>
but the problem is, the height of the buttons change based on the viewport, destroying the banner, how can I position it without ruining it?
I would personally avoid absolute positioning and use background image to create the layers.
You can set a min height on your banner if you desire.
I would also use em and media queries to reduce the font size when the screen resolution is smaller.
.banner {
background:url(https://picsum.photos/1200/300);
padding:10px;
}
.banner-buttons {
display: flex;
justify-content:center;
align-items: center;
}
.banner button {
font-size: 2em;
border: none;
border-radius: 5px;
font-weight: bold;
padding: 5px 15px;
margin:5px;
}
<div class="banner">
<div class="banner-buttons">
<button>Assistir</button>
<button>Mais Informações</button>
</div>
</div>
Actually what solved for me was adding position: relative to .banner, now the buttons are displayed at the exact same position at every screen size.
I need help with the code of a project I've been working on. I can't make my navigation bar fixed so it always appears on the top of the viewport. I understand the rules of CSS position and I've been looking at examples and tutorials, but for some reason, I'm stuck.
I tried to make the position fixed for the navbar and relative to the container, along many other changes. Every thing I tried, it messes up my content. I guess this is one of those moments when you need help.
This is the link to the project and the code of the navbar without any position rules.
https://codepen.io/aitormorgado/pen/MWayXPy
#title-wrapper {
display: flex;
justify-content: center;
align-items: center;
font-family: "Aclonica", serif;
color: #281c1c;
font-size: 6rem;
text-transform: uppercase;
margin: 3rem 1rem;
}
li {
list-style: none;
}
#header-img {
width: 6rem;
padding-right: 1.5rem;
}
#nav-bar ul {
display: flex;
flex-direction: column;
align-items: center;
font-family: "Libre Baskerville", serif;
background-color: #990000;
color: #e0e0e0;
border-top: 1px solid black;
border-bottom: 1px solid black;
text-transform: uppercase;
font-size: 4rem;
}
#nav-bar ul li {
border: 2px solid black;
width: 100%;
text-align: center;
}
#nav-bar ul li a {
text-decoration: none;
color: inherit;
padding: 1.4rem;
display: block;
}
#nav-bar ul li:hover {
background: #cc3300;
}
A million thanks for your help!
as per your question, I just chnaged a small thing in your code and now your header is fixed at the top while scrolling,
all you need to do is use of
#header {
position: sticky;
top: 0;
z-index: 99999;
background-color: #E0E0E0;
}
on your header. and the background color is given so that rest of the bottom elements will not appear below it while scrolling,
If need something else, or I have not understood your question, then feel free to share.
just check the codepen here.