Centering navbar title in center of screen [duplicate] - html

This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed 2 years ago.
I'm trying to center the title of the navbar in the middle of the screen. Is there a way to do this without custom margin/padding values (trying to keep it responsive)?
In the image below, I want to center "Title text goes here" in the middle of the screen.
<header>
<nav class="nav">
<img class="nav-logo" src="./jumbotron.jpg" alt="logo">
<h1 class="nav-title">Title text goes here</h1>
<ul class="nav-links">
<li class="nav-link">nav link 1</li>
<li class="nav-link">nav link 2</li>
<li class="nav-link">nav link 3</li>
</ul>
</nav>
</header>
/*****************************************************************************/
/* Navbar */
/*****************************************************************************/
.nav {
display: flex;
flex-direction: row;
justify-content: center;
background-color: white;
}
.nav-logo {
width: 10%;
}
.nav-title {
flex: 1;
margin: auto 0;
text-align: center;
font-size: 30px;
}
.nav-links {
flex: 0.5;
display: flex;
width: 100%;
padding: 20px;
justify-content: flex-end;
list-style: none;
}

You can achieve this with flexbox. By setting align-items to stretch on the header and flex-grow to 1 on the title, the title will expand to cover whatever space is unoccupied by the logo and the links. Then, text-align:center will center your title's text:
.nav {
display: flex;
flex-direction: row;
align-items: stretch
background-color: white;
}
.nav-logo {
width: 10%;
}
.nav-title {
flex-grow: 1;
margin: auto 0;
text-align: center;
font-size: 30px;
text-align: center;
}
.nav-links {
display: flex;
width: 100%;
padding: 20px;
justify-content: flex-end;
list-style: none;
}

Related

HTML / CSS: How to center element horizontally? [duplicate]

This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed 18 days ago.
I am trying to create a navbar / header, but I am having some trouble centering an h1-element.
This is my HTML:
body {
margin: 0;
padding: 0;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 2%;
}
nav {
display: flex;
}
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
li {
margin-right: 10px;
}
h1 {
margin: 0;
text-align: center;
flex-grow: 1;
}
button {
display: inline-block;
}
<header>
<nav>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</nav>
<h1>SalonM</h1>
<button>Button</button>
</header>
To be clear, I do not want to center the h1-element relative to the header, I want to center it in regards to the screen.
Any ideas on how to fix this?
Try this:
body {
margin: 0;
padding: 0;
}
.nav-logo {
position: absolute;
display: flex;
justify-content: center;
width: 100%;
height: 100px;
align-items: center;
}
.nav-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 2%;
height: 100px;
}
nav {
display: flex;
}
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
li {
margin-right: 10px;
}
h1 {
margin: 0;
text-align: center;
flex-grow: 1;
}
button {
display: inline-block;
}
<header>
<div class="nav-logo">
<h1>SalonM</h1>
</div>
<div class="nav-wrapper">
<nav>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</nav>
<button>Button</button>
</div>
</header>
If you put the H1 tag inside a Div tag, you will be able to center the div's content using "text-aling: center;" (The Div must have a defined width).

Horizontal blank space though margin property is not set [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
How to remove margin space around body or clear default css styles
(7 answers)
How can I get rid of margin around my HTML content?
(5 answers)
Closed 3 years ago.
I ran into this problem while learning CSS. I tried to search for this but couldn't find any proper answers. Some lead me to margin collapsing, but it just doesn't happen to horizontal margins.
#nav-bar {
position: fixed;
top: 0px;
left: 0px;
height: 60px;
width: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background-color: #be3144;
}
#nav-list {
display: flex;
margin-right: 4rem;
}
.nav-link {
text-decoration: none;
color: white;
padding: 0 1.6rem 0 1.6rem;
height: 60px;
weight: 100%;
display: flex;
align-items: center;
font-size: 1.3rem;
}
#welcome-section {
background-color: #3a3d40;
height: 100vh;
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
margin-top: 60px;
}
<nav id="nav-bar">
<div id="nav-list">
About
Work
Contact
</div>
</nav>
<section id="welcome-section">
<h1>Hey I am Mimic</h1>
<h4>a web developer</h4>
</section>
The #welcome-section below nav-bar has blank space both left and right sides, though I didn't set any margin properties.
Also, any advices and suggestions in styling HTML/CSS are appreciated. Thanks in advance.
It's because the <body> of the page has margin as default.
Get rid of that by adding the below, and it should work...
body {
margin:0;
}
body {
margin:0;
}
#nav-bar {
position: fixed;
top: 0px;
left: 0px;
height: 60px;
width: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background-color: #be3144;
}
#nav-list {
display: flex;
margin-right: 4rem;
}
.nav-link {
text-decoration: none;
color: white;
padding: 0 1.6rem 0 1.6rem;
height: 60px;
weight: 100%;
display: flex;
align-items: center;
font-size: 1.3rem;
}
#welcome-section {
background-color: #3a3d40;
height: 100vh;
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
margin-top: 60px;
}
<nav id="nav-bar">
<div id="nav-list">
About
Work
Contact
</div>
</nav>
<section id="welcome-section">
<h1>Hey I am Mimic</h1>
<h4>a web developer</h4>
</section>

Why can't I center my navigation bar in CSS?

I want the logo and nav links centered horizontally. There is too much space on the right side. What is the best way to fix this?
Tried inline-block on the nav element. Tried text-align instead of align-items.
I expect the content to be centered with even space on both sides, but the right side has more spacing.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
nav {
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: black;
font-family: 'Poppins', sans-serif;
}
.logo {
color: white;
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}
.nav-links {
display: flex;
justify-content: space-around;
width: 40%;
}
.nav-links li {
list-style: none;
}
.nav-links a {
color: white;
text-decoration: none;
letter-spacing: 3px;
font-weight: bold;
font-size: 14px;
}
<link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet">
<header>
<nav>
<div class="logo">
<h4>Great Falls</h4>
</div>
<ul class="nav-links">
<li><a href="#">Home</li>
<li><a href="#">Plan Your Visit</li>
<li><a href="#">Learn More</li>
<li><a href="#">Contact</li>
</ul>
</nav>
</header>
your have this in your css code:
nav {
display: flex;
justify-content: space-around;
align-items: center;
}
By default, flex-direction is set to row. Therefore justify-content is horizontal and align-items vertical. In this piece of CSS, you center all children vertically, but put equal spacing around the children horizontally.
You should set justify-content to center in order to align items centered on a row direction.
nav {
display: flex;
flex-direction: row;
justify-content: space-between;
}
should work
also here is a code pen https://codepen.io/anon/pen/mYvVgL?editors=1100

"hover" Selector Does Not Work with Flexbox to Change Dropdown Display to "none"

Hovering selector in css is not capable of making the child element disappear with display: none;
I have tried different selectors such as + it did not really work. I am a complete noob sorry for that.
*{
margin: 0;
padding: 0;
/* Width and height apply to all parts of the element: content, padding and borders */
box-sizing: border-box;
}
#menu{
display: flex;
flex-direction: row;
justify-content: space-between;
}
#logo{
height: 70px;
width: 70px;
}
div nav{
display: flex;
/* This flexbox property lets button area to fill-in the remainin space until the logo area */
flex: 1;
}
div nav ul{
display: flex;
flex-direction: row;
background-color: mediumaquamarine;
justify-content: space-between;
flex: 1;
/* This flexbox property lets button area to fill-in the remainin space until the logo area */
}
div nav ul li{
display: flex; /* These 3 lines or the align the bottons vertically */
flex-direction: column; /* These 3 lines or the align the bottons vertically */
justify-content: center; /* These 3 lines or the align the bottons vertically */
list-style-type: none;
background-color: blue;
flex: 1;
/* This flexbox property lets button area to fill-in the remainin space until the logo area */
position: relative;
}
div nav ul li a{
display: flex; /* These 3 lines or the align the bottons vertically */
flex-direction: column; /* These 3 lines or the align the bottons vertically */
justify-content: center; /* These 3 lines or the align the bottons vertically */
text-decoration: none;
background-color: orange;
height: 50px;
text-align: center;
margin: 0.5px;
}
div nav ul li a:hover{
background-color: #9f7934;
}
#dropdown{
display: flex;
flex-direction: column;
width: 100%;
top: 60px;
position: absolute;
}
#products:hover #dropdown{
display: none !important;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF8">
<meta http-equiv="refresh" content="180">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="menu">
<img src="logo.jpg" alt="Smiley face" id="logo">
<nav>
<ul>
<li>Anasayfa</li>
<li>Hakkımızda</li>
<li>Ürünler
<ul id="dropdown">
<li>Ürün 1</li>
<li>Ürün 2</li>
<li>Ürün 3</li>
<li>Ürün 4</li>
</ul>
</li>
<li>İletişim</li>
</ul>
</nav>
</div>
</body>
</html>
Child should disappear when products selected. Whenever I use hover on dropdown list i can make them disappear but it does not seem to work for parent.
You should target the next sibling element
#products:hover + #dropdown
* {
margin: 0;
padding: 0;
/* Width and height apply to all parts of the element: content, padding and borders */
box-sizing: border-box;
}
#menu {
display: flex;
flex-direction: row;
justify-content: space-between;
}
#logo {
height: 70px;
width: 70px;
}
div nav {
display: flex;
/* This flexbox property lets button area to fill-in the remainin space until the logo area */
flex: 1;
}
div nav ul {
display: flex;
flex-direction: row;
background-color: mediumaquamarine;
justify-content: space-between;
flex: 1;
/* This flexbox property lets button area to fill-in the remainin space until the logo area */
}
div nav ul li {
display: flex;
/* These 3 lines or the align the bottons vertically */
flex-direction: column;
/* These 3 lines or the align the bottons vertically */
justify-content: center;
/* These 3 lines or the align the bottons vertically */
list-style-type: none;
background-color: blue;
flex: 1;
/* This flexbox property lets button area to fill-in the remainin space until the logo area */
position: relative;
}
div nav ul li a {
display: flex;
/* These 3 lines or the align the bottons vertically */
flex-direction: column;
/* These 3 lines or the align the bottons vertically */
justify-content: center;
/* These 3 lines or the align the bottons vertically */
text-decoration: none;
background-color: orange;
height: 50px;
text-align: center;
margin: 0.5px;
}
div nav ul li a:hover {
background-color: #9f7934;
}
#dropdown {
display: flex;
flex-direction: column;
width: 100%;
top: 60px;
position: absolute;
}
#products:hover+#dropdown {
display: none;
}
<div id="menu">
<img src="logo.jpg" alt="Smiley face" id="logo">
<nav>
<ul>
<li>Anasayfa</li>
<li>Hakkımızda</li>
<li>Ürünler
<ul id="dropdown">
<li>Ürün 1</li>
<li>Ürün 2</li>
<li>Ürün 3</li>
<li>Ürün 4</li>
</ul>
</li>
<li>İletişim</li>
</ul>
</nav>
</div>

CSS Flexbox: How do you align child elements of a flexbox container to opposite far ends of the main axis?

I am styling a header of a webpage. I want the header to be a single line which includes a logo and some navigational links. I feel the best, most modern way to layout this header today is with CSS3's flexbox, so that is what I would like to use.
I would like for the logo to be as far left in the flex container as possible, and the remaining navigation items to be as far right as possible. This could easily be achieved by floating the elements left and right, but that is not what I would like to do. So...
How do you align child elements of a flexbox container to opposite far ends of the main axis?
There is a property for the flexbox child elements that allows you to do this on the cross axis, align-self, but it seems there is none to do this on the main axis.
The best way I have come up with to achieve this is to insert an additional, empty, element in between the logo and the navigational links to serve as a spacer. But part of the reason I am choosing to use flexbox for this header is to cohere with a responsive design and I do not know of a way to make the spacing element take up all the remaining space, regardless of the width of the viewing window.
Here is where I currently stand with the mark-up, simplified to only include the elements pertinent to this situation.
HTML
<ul>
<!-- Should be as far left as possible -->
<li id="main">Some Logo <span>Some tag line.</span></li>
<!-- Should be as far right as possible -->
<li>Home</li>
<li>Products</li>
<li>Price Sheet</li>
<li>Capabilities</li>
<li>Contact</li>
</ul>
CSS
ul {
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: flex-end;
align-items: center;
padding: 0;
margin: 0;
list-style: none;
}
li {
margin: 0 8px;
padding: 8px;
font-size: 1rem;
text-align: center;
font-weight: bold;
color: white;
background: green;
border: solid 4px #333;
}
#main { font-size: 2rem; }
#main span { font-size: 1rem; }
From your question:
I do not know of a way to make the spacing element take up all the remaining space, regardless of the width of the viewing window.
This is exactly what the flex-grow CSS rule was designed for. If only one child element has the flex-grow attribute set, then it will take up all the remaining space in the flex container. The only markup you will need in this case is the following:
HTML:
<li id="spacer"></li>
CSS:
#spacer {
visibility: hidden;
flex-grow: 1;
}
Full Live Demo:
ul {
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: flex-end;
align-items: center;
padding: 0;
margin: 0;
list-style: none;
}
li {
margin: 0 8px;
padding: 8px;
font-size: 1rem;
text-align: center;
font-weight: bold;
color: white;
background: green;
border: solid 4px #333;
}
#main { font-size: 2rem; }
#main span { font-size: 1rem; }
#spacer {
visibility: hidden;
flex-grow: 1;
}
<ul>
<!-- Should be as far left as possible -->
<li id="main">Some Logo <span>Some tag line.</span></li>
<!-- Spacer element -->
<li id="spacer"></li>
<!-- Should be as far right as possible -->
<li>Home</li>
<li>Products</li>
<li>Price Sheet</li>
<li>Capabilities</li>
<li>Contact</li>
</ul>
JSFiddle Version: https://jsfiddle.net/7oaahkk1/
I think the only flexibility that is needed, at least on large screens, should go on the first flex-item in the list. The one you want to place your logo at.
By setting this items flex-grow rule to 1 and the text-align to left it will stay on the left side, growing in size, making sure all other items stay on the right side. Since the logo may have a greater height value than all the other items it would make sense to change the align-items rule to baseline, making sure all items are horizontally aligned.
Furthermore i have added a few media queries to change the flex settings accordingly.
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: border-box;
}
body {
margin: 0;
}
ul {
width: 100%;
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: stretch;
padding: 0;
margin: 0;
list-style: none;
}
li {
margin: 0;
padding: 8px;
font-size: 1rem;
text-align: center;
font-weight: bold;
color: white;
background: green;
border: solid 4px #333;
}
li:first-child {
font-family: sans;
font-size: 2em;
text-align: center;
}
li:first-child span {
font-size: initial;
}
#media (min-width: 34em) {
ul {
flex-flow: row wrap;
justify-content: space-between;
align-items: flex-start;
}
li {
flex: 1;
}
#main {
flex: 0 0 100vw;
}
}
#media (min-width: 48em) {
ul {
justify-content: flex-end;
align-items: baseline;
}
li {
flex: none;
}
#main {
flex: 1 0 auto;
text-align: left;
}
}
<ul>
<!-- Should be as far left as possible -->
<li id="main">Some Logo <span>Some tag line.</span></li>
<!-- Should be as far right as possible -->
<li>Home</li>
<li>Products</li>
<li>Price Sheet</li>
<li>Capabilities</li>
<li>Contact</li>
</ul>
Essentially you need a row container with two child columns
container is a flexible div
column one is a flexed div with the logo
column two is a flexible ul with flexed li's
AND 'justify-content: space-between' to move the columns to the far ends
Check my snippet (full page)!
.container,
.menu {
display: flex;
}
.container {
justify-content: space-between;
width: 100%;
}
.menu {
padding: 0;
margin: 0;
list-style: none;
}
li,
.logo {
margin: 0 8px;
padding: 8px;
font-size: 1rem;
text-align: center;
font-weight: bold;
color: white;
background: green;
border: solid 4px #333;
}
#main {
font-size: 2rem;
}
#main span {
font-size: 1rem;
}
<div class="container">
<div id="main" class="logo">Some Logo <span>Some tag line.</span>
</div>
<ul class="menu">
<li>Home</li>
<li>Products</li>
<li>Price Sheet</li>
<li>Capabilities</li>
<li>Contact</li>
</ul>
</div>
JSfiddle Demo
Wrap a ul over the rest of the list items and use nested flex container. This is to provide flexbox to act on two elements.
Use justify-content: space-between on the main flexbox parent to equally space the two elements.
.parent-menu {
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
/* Modify */
align-items: center;
padding: 0;
margin: 0;
list-style: none;
}
li {
margin: 0 8px;
padding: 8px;
font-size: 1rem;
text-align: center;
font-weight: bold;
color: white;
background: green;
border: solid 4px #333;
}
#main {
font-size: 2rem;
}
#main span {
font-size: 1rem;
}
.right-menu {
display: flex;
/* Add */
}
<ul class="parent-menu">
<!-- Should be as far left as possible -->
<li id="main">Some Logo <span>Some tag line.</span>
</li>
<ul class="right-menu">
<!-- Should be as far right as possible -->
<li>Home</li>
<li>Products</li>
<li>Price Sheet</li>
<li>Capabilities</li>
<li>Contact</li>
</ul>
</ul>
One posibility is to set a right margin on the first element
ul {
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: flex-end;
align-items: center;
padding: 0;
margin: 0;
list-style: none;
}
li {
margin: 0 8px;
padding: 8px;
font-size: 1rem;
text-align: center;
font-weight: bold;
color: white;
background: green;
border: solid 4px #333;
}
#main {
font-size: 2rem;
margin-right: auto; /* create a right margin as needed */
}
#main span { font-size: 1rem; }
#spacer {
visibility: hidden;
flex-grow: 1;
}
<ul>
<!-- Should be as far left as possible -->
<li id="main">Some Logo <span>Some tag line.</span></li>
<!-- Should be as far right as possible -->
<li>Home</li>
<li>Products</li>
<li>Price Sheet</li>
<li>Capabilities</li>
<li>Contact</li>
</ul>