What I know so far is that a parent with nothing but floated children has no height.
In the header for example, I have the left logo floated and the text has an absolute position. The only thing keeping the header from collapsing is the right logo.
header {
position: relative;
font-family: "Felix Titling Regular", Times, serif;
border-bottom: 2px double white;
font-size: 0;
}
header a {
display: inline-block;
}
header>a:first-child {
position: relative;
padding-left: 3%;
}
header>a:last-child {
float: right;
padding-right: 3%;
}
#center-wrapper {
position: absolute;
left: 50%;
top: 25%;
font-size: 16px;
}
header h1 {
font-size: 300%;
display: inline-block;
position: relative;
text-transform: capitalize;
right: 50%;
}
<header>
<img src="media/logo-small.png" alt="Godfather Logo" title="Godfather Small Logo" />
<div id="center-wrapper">
<h1> loyal capos to the don </h1>
</div>
<img src="media/logo-small.png" alt="Godfather Logo" title="Godfather Small Logo" />
</header>
However, I don't understand why my first parent 'ul' in the 'nav' still has height even though all the li elements are floated.
nav {
position: relative;
border-top: 2px double #660000;
text-align: center;
font-size: 0;
background-color: #660000;
}
nav>ul {
display: inline-block;
font-size: 16px;
width: 100%;
}
ul {
list-style-type: none;
}
nav>ul>li {
min-width: 20%;
float: left;
}
nav ul ul,
nav ul ul ul {
position: absolute;
display: none;
width: 100%;
}
nav>ul>li:hover>ul {
display: block;
}
nav>ul>li>ul>li:hover>ul {
left: 100%;
top: 0%;
display: block;
}
ul>li {
position: relative;
padding: .4em 0;
background-color: white;
}
li>a {
color: #660000;
text-decoration: none;
text-transform: capitalize;
font-size: 150%;
font-family: "Felix Titling Regular", Times, serif;
background-color: white;
}
<nav>
<ul>
<li>wiki</li>
<li>media
<ul>
<li>images</li>
<li>videos
<ul>
<li>best scenes</li>
<li>bloopers</li>
</ul>
</li>
</ul>
</li>
<li>home</li>
<li>facts</li>
<li>about
<ul>
<li>feedback</li>
<li>contact us</li>
</ul>
</li>
</ul>
</nav>
Here's my code on fiddle (https://jsfiddle.net/vwb6g740/1/)
This is true for block elements and inline elements, but not for inline-block elements.
What I know so far is that a parent with nothing but floated children has no height.
You have display:inline-block; set on that parent element.
I can't explain why, but I can reproduce that behavior.
p {
display: inline-block;
background: blue;
}
div {
background: red;
}
span {
background: green;
}
i {
float: left;
}
<div><i>test</i></div>
<span><i>test</i></span>
<p><i>test</i></p>
Related
I'm trying to make a navigation bar with a center-aligned menu, but the logo is stopping the menu from being in the center of the page and instead pushing the menu to the right. So the menu is offset from being horizontally centered by the width of the logo. I want it so that my menu is in the center of the page rather than pushed 150px to the right by the logo.
How can I make it so that my logo doesn't shift my menu to the right, stopping it from being center-aligned in the body?
body {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 16px;
margin: 0;
}
h1 {
font-size: 6rem;
font-weight: 400;
}
h2 {
font-size: 4.5rem;
font-weight: 400;
}
header {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
margin: 20px 5% 0;
}
header img {
width: 150px;
}
nav {
width: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline-block;
padding-right: 1rem;
text-transform: uppercase;
}
nav ul li a {
text-decoration: none;
color: black;
}
<header>
<img src="../Images/Logo/Black-Logo.png" alt="Logo" />
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
You could erase the flex settings from the container, apply absolute position to the logo (and relative to the header), add text-align: center; to the header to center the nav and erase/reset most of the paddings and margins except those shown below (exact settings see snippet below).
body {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 16px;
margin: 0;
}
header {
width: 90%;
margin: 20px 5% 0;
text-align: center;
position: relative;
}
header img {
width: 50px;
position: absolute;
left: 0;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline-block;
padding-right: 1rem;
text-transform: uppercase;
}
nav ul li:last-child {
padding-right: 0;
}
nav ul li a {
text-decoration: none;
color: black;
}
img {
}
<header>
<img src="../Images/Logo/Black-Logo.png" alt="Logo" />
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
Use position: absolute for the logo and it will make it "not take up space" in the navbar:
#logo {
position: absolute;
top: 0 left: 0;
}
body {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 16px;
margin: 0;
}
h1 {
font-size: 6rem;
font-weight: 400;
}
h2 {
font-size: 4.5rem;
font-weight: 400;
}
header {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
margin: 20px 5% 0;
}
header img {
width: 150px;
}
nav {
width: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline-block;
padding-right: 1rem;
text-transform: uppercase;
}
nav ul li a {
text-decoration: none;
color: black;
}
<header>
<img id="logo" src="../Images/Logo/Black-Logo.png" alt="Logo" />
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
I'd use a container (see div with class wrapper below) and make the logo position: absolute. This simplifies your CSS.
.logo {
position: absolute;
}
.wrapper {
margin: 0 auto;
}
<div class="logo"><img src="../Images/Logo/Black-Logo.png" alt="Logo" /></div>
<div class="wrapper">
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</div>
You can get rid of this CSS:
nav {
width: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
I have a question, is it possible to have a full width dropdown menu when my wrapper has a width of 1024px (all contents are centered on screen)? Because I am having problems with my dropdown menu. Though, it is not yet working with the hover but I'm still trying to style my dropdown menu.
Here's my code:
#lower-header {
background-color: #ffffff;
height: 100px;
position: relative;
width: -webkit-fill-available;
z-index: 1;
img {
float: left;
margin-top: 33px;
}
ul {
list-style: none;
display: block;
float: left;
margin: 17px 0px;
padding-left: 30px;
li {
display: inline-block;
font-size: 17px;
font-weight: bold;
padding: 16px 19px;
height: 73px;
.sub-menu-whole {
background-color: #ffffff;
height: 360px;
/*position: absolute;*/
z-index: 1;
margin-top: 44px;
&:after {
content: "";
display: table;
clear: both;
}
div {
position: absolute;
margin: -33px 0;
padding: 0;
div {
float: right;
}
}
}
a {
text-decoration: none;
color: #000000;
&:hover {
color: red;
}
}
}
}
}
<div id="lower-header">
<div class="wrapper">
<img src="images/logo/logo_01.png">
<ul>
<li>
KU 스타트업
<div class="sub-menu-whole">
<div>
<img src="images/bg/bg_sub_01.png">
</div>
<div class="column">
<ul>
<li>
<a>인사말</a>
</li>
<li>
<a>창업부서소개</a>
</li>
</ul>
</div>
</div>
</li>
<li>프로그램</li>
<li>스타트업 리더</li>
<li>창업보육</li>
<li>창업멘토단</li>
<li>커뮤니티</li>
</ul>
</div>
</div>
Remove the float: left; property from the ul and add width:100%;
I am designing a hotel website with a fixed navbar. I am having trouble getting the items to be inline with an image item. It's getting quite frustrating.
Here's my HTML
<nav id="navigation">
<ul>
<li class="left">Rooms</li>
<li class="left">Dining</li>
<li class="home"><img src="assets/navbarimg.png" height="64.5px" weight="250px"></img></li>
<li class="right">Activities</li>
<li class="right">Book a Stay</li>
</ul>
</nav>
and here's my CSS
#navigation ul {
text-align: center;
position: fixed;
width: 100%;
max-width: 100%;
font-size: 2vh;
font-family: 'Raleway', sans-serif;
z-index: 100;
background-color: rgba(255,255,255,.55);
display: inline-block;
}
#navigation li {
display: inline-block;
list-style-type: none;
}
#navigation a {
text-decoration: none;
margin: 0px 75px 0 75px;
color: black;
font-weight: 600;
text-transform: uppercase;
display: inline-block;
}
.left {
display: inline;
}
.home {
display: inline;
}
.right {
display: inline;
}
My code is super basic, so work with me here.
#navigation ul {
text-align: center;
position: fixed;
width: 100%;
max-width: 100%;
font-size: 2vh;
font-family: 'Raleway', sans-serif;
z-index: 100;
background-color: rgba(255, 255, 255, .55);
display: inline-block;
}
#navigation li {
display: inline-block;
list-style-type: none;
}
#navigation a {
text-decoration: none;
margin: 0px 75px 0 75px;
color: black;
font-weight: 600;
text-transform: uppercase;
display: inline-block;
}
.left {
display: inline;
}
.home {
display: inline;
}
.right {
display: inline;
}
<nav id="navigation">
<ul>
<li class="left">Rooms</li>
<li class="left">Dining</li>
<li class="home">
<a href="index.html"><img src="https://placehold.it/250x65" height="64.5px" weight="250px"></img>
</a>
</li>
<li class="right">Activities</li>
<li class="right">Book a Stay</li>
</ul>
</nav>
Live Example I made: https://akainth015.github.io/Inked-Out
Often, you can align things with vertical-align(CSS). However, it is a little counter-intuitive. When you use the vertical-align style, the element it is applied to becomes the standard for the rest of the elements. So, if you have this structure:
li {
display: inline-block;
}
ul {
list-style-type: none;
}
li img {
vertical-align: middle;
}
<ul>
<li>Home</li>
<li><img src="https://placehold.it/64x64"></li>
<li>About</li>
</ul>
Notice how it is applied to the image, not the the text you want in the center. Good luck!
This question already has answers here:
How to disable margin-collapsing?
(12 answers)
Closed 5 years ago.
Here is the HTML code (the white gap started appearing as soon as I added h3 to the last div):
body {
margin: 0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #343434;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
nav li {
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a {
text-decoration: none;
color: white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover {
color: yellow;
}
.welcome {
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3 {
text-align: center;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
I am fairly new to web development and stackoverflow. So I am sorry for any inconveniences. Any help is appreciated. Thank you.
Set margin: 0px; on h3 tag to resolve this issue. Check updated Snippet below..
body{
margin:0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container{
width: 80%;
margin : 0 auto;
}
header{
background: #343434;
}
header::after{
content: '';
display: table;
clear:both;
}
.logo{
float: left;
padding:10px;
}
nav{
float:right;
}
nav ul{
margin: 0;
padding: 0;
list-style-type: none;
}
nav li{
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a{
text-decoration: none;
color:white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover{
color:yellow;
}
.welcome{
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3{
text-align: center;
margin: 0px;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
Just remove the margin from h3 like
.welcome h3 {
text-align: center;
margin:0;
}
body {
margin: 0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #343434;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
nav li {
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a {
text-decoration: none;
color: white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover {
color: yellow;
}
.welcome {
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3 {
text-align: center;
margin:0;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
This is due to collapsing margins
Remove the margin on the h3. Replace it with padding if you want to create space between the header and maintain the background colour.
body {
margin: 0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #343434;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
nav li {
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a {
text-decoration: none;
color: white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover {
color: yellow;
}
.welcome {
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3 {
text-align: center;
margin-top: 0;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
You can try adding style="display: inline; margin:0px; padding:0px;" to your <h3> Tag.
Another way is to apply a rule of overflow: auto to the .welcome div... thus creating a new block formatting context and avoiding the collapsing margins.
Edit: Let's add a little more context. In the spec, you can read that adjoining margins will collapse under certain circumstances. In particular, the margins need to belong to block-level boxes participating in the same block formatting context.
Even though .welcome and h3 are block-level boxes in your example, neither automatically establishes a new block formatting context (meaning they participate in the same block formatting context, meaning their margins collapse). Looking at the spec again, we see that some of the ways to establish a new block formatting context is to have a float, an absolutely positioned element, or a block box with the property of overflow set to something else than visible.
That's why the suggestions regarding overflow: auto or floating one of the elements work. My understanding is that if we make .welcome establish a new block formatting context, the context it participates in is different from the one it establishes itself. Removing the margin (possibly replacing it with padding) is another way to get around the problem.
Either apply margin-top:0 for H3-Tag
or
apply a float:left for .welcome
Both will fix your issue
So I'm working on a navigation bar for my website and I'm trying to space everything centred evenly in the bar, but for some reason, I can't get things to work out evenly. Either margins don't work, or they do but not all the way, and it's getting quite frustrating. Here's my code:
body {
margin: 0 px;
font - family: Helvetica;
}
.navbar {
background - color: grey;
width: 100 % ;
height: 70 px;
text - align: center;
}
.items li {
display: inline - block;
padding - left: 50 px;
}
.items a {
text - decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
padding-top: -25px;
}
<body>
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
</body>
To center vertically links with the logo, use vertical-align:middle;.
Also, If you set margin or padding don't set it to <a> because it's an item with inline properties but set it on li items which are inline-block.
In addition to that, it's strange to write li items without ul container.
body {
margin: 0px;
font-family: Helvetica;
}
.navbar {
background-color: grey;
width: 100% ;
height: 70px;
text-align: center;
}
.items img
{
vertical-align:middle;
}
.items li {
display: inline-block;
vertical-align:middle;
padding-left: 50px;
}
.items a {
text-decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
}
<body>
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
</body>
Note:See it in full page!
body {
margin: 0px;
font-family: Helvetica;
}
.navbar {
background-color: grey;
width: 100%;
height: 70px;
text-align: center;
position: relative;
}
.items {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.items li {
display: inline-block;
padding-left: 50px;
}
.items a {
text-decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
display: block;
}
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>