Why adding 1px padding adding 10px in height of div [duplicate] - html

This question already has answers here:
Why does this CSS margin-top style not work?
(14 answers)
Does UL have default margin or padding [duplicate]
(2 answers)
Closed 2 years ago.
I am trying to learn CSS basics. Below is the HTML and CSS for which I am unable to understand something
* {
box-sizing: border-box;
}
body {
font-family: 'Montserrat', sans-serif;
margin: 0;
}
.section-title {
color: #2ddf5c;
}
main {
background-color: burlywood;
}
#product-overview {
background: #ff1b68;
width: 100%;
height: 528px;
padding: 10px;
}
.main-header {
background: #2ddf5c;
padding: 0px 16px;
}
<header class="main-header">
<div>
<a href="index.html">
uhost
</a>
</div>
<nav class="main-nav">
<ul class="main-nav__items">
<li class="main-nav__item">Packages</li>
<li class="main-nav__item">Customers</li>
<li class="main-nav__item">Start hosting</li>
</ul>
</nav>
</header>
<main>
<section id="product-overview">
<h1>Get the freedom you deserve</h1>
</section>
<section id="plans">
<h1>Choose your plan</h1>
</section>
</main>
We can see that there is a small gap between red and green area.
As per my understanding, this gap exists because both nav and main are block elements.
Right now, height of header element is 90px(approx.)
But if I am trying to update padding of main-header, then browser is removing blank area between header and main elements, and also updating the height of header to 106px
.main-header {
background: #2ddf5c;
padding: 1px 16px;
}
Please help me understand why this is happening.

This has nothing to do with block elements. What happens: The ul at the bottom of your header has a default margin, top and bottom. The margin-bottom goes out of the header and creates the white space (this is known as "collapsing margins").
If you manually set that margin to zero:
.main-nav ul {
margin-bottom: 0;
}
, the space disappears. But when you add a padding-bottom to the header (regardless which value), the (default) margin-bottom of the ul remains inside the header element, also causing the white space to disappear.
Here's the situation without the added padding, but with header's margin-bottom set to zero:
* {
box-sizing: border-box;
}
body {
font-family: 'Montserrat', sans-serif;
margin: 0;
}
.section-title {
color: #2ddf5c;
}
main {
background-color: burlywood;
}
#product-overview {
background: #ff1b68;
width: 100%;
height: 528px;
padding: 10px;
}
.main-header {
background: #2ddf5c;
padding: 0px 16px;
}
.main-nav ul {
margin-bottom: 0;
}
<header class="main-header">
<div>
<a href="index.html">
uhost
</a>
</div>
<nav class="main-nav">
<ul class="main-nav__items">
<li class="main-nav__item">Packages</li>
<li class="main-nav__item">Customers</li>
<li class="main-nav__item">Start hosting</li>
</ul>
</nav>
</header>
<main>
<section id="product-overview">
<h1>Get the freedom you deserve</h1>
</section>
<section id="plans">
<h1>Choose your plan</h1>
</section>
</main>

Well, I'm not sure If I understood you right,
you are bothered about the white gap between the two elements? (I don't have the Privilege to comment your question)
If so the reason of the white gap is because UL elements have "default" style that the browser gives them.
you need to change those default style settings by doing:
ul {
margin: 0;
}
Hope I got you right :)

Related

Unexplainable vertical gap between my navbar and the div below it

There is currently an unexplainable gap between my nav element and the div element that's below it.
Using inspect element on said gap refers me to the body. Meaning that there's literally nothing in that gap. I've used Chrome developer tools to check the margin and padding of every element that exists in the html file already and from what I can see there really isn't anything that should be causing the gap.
Here's the code. Running the code snippet in a new window would be best:
/* Load required font faces */
#font-face {
font-family: Nunito;
src: url("../fonts/Nunito/Nunito-Regular.ttf") format('truetype');
font-weight: normal;
}
/* CSS for text */
p, a, h1, h2, h3 {
font-family: Nunito;
}
/* CSS Styles for body */
body {
margin: 0 !important;
}
/* CSS Styles for the Navbar */
nav {
height: 8vh;
padding: 0% 2% 0% 2%;
background-image: linear-gradient(to right, #00B9FF, #9316FF);
}
nav a {
color: white;
font-size: 1.5em;
text-decoration: none;
}
nav a:hover {
color: #adadad;
}
#nav-wrapper {
height: 100%;
display: flex;
align-items: center;
}
#nav-content {
width: 100%;
}
.nav-logo {
float: left;
}
#nav-navigation {
width: 20%;
float: right;
margin: 0;
}
.nav-button {
display: inline-block;
margin: 0% 2% 0% 2%;
}
/* CSS for the page header */
#page-header {
height: 9vh;
background-color: #F3F3F3;
}
<html>
<head>
<title>Webpage</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<!-- Navbar -->
<nav>
<div id="nav-wrapper" class="align-middle">
<div id="nav-content">
<!-- Company name on the left of navbar -->
<div class="nav-logo">
Company Name
</div>
<!-- Navigation buttons for the navbar -->
<ul id="nav-navigation">
<li class="nav-button">
Nav 1
</li>
<li class="nav-button">
Nav 2
</li>
<li class="nav-button">
Nav 3
</li>
<li class="nav-button">
Nav 4
</li>
<li class="nav-button">
Nav 5
</li>
</ul>
</div>
</div>
</nav>
<!-- Main content -->
<div id="page-header">
<div>
<div>
<p>About Company Name</p>
</div>
<div>
<p>Over here at company....</p>
</div>
</div>
</div>
</body>
</html>
Any help would be welcome. Thank you.
The "p" element in the "page-header" div is causing the gap. Elements may have a default margin or padding. You can reset all default margins and paddings at the beginning of your css file like this:
* {
margin: 0;
padding: 0;
}
or you can select that certain "p" element and remove its margin.
Your p element is getting a margin on it. that's what's causing your gap. If you set a new css rule of:
p {
margin-top:0;
}
or even:
p {
margin:0;
}
then it'll work as expected. At least - it works for me running your snippet - there may be other stuff on your page doing that, but it's worth a shot I think.
Always remember to reset the rules that browsers set by default. A typical reset is
* {
margin: 0,
padding: 0,
box-sizing: border-box,
}

CSS filter property disabling fixed position on navigation bar [duplicate]

This question already has an answer here:
Why does clip-path (and other properties) affect the stacking order (z-index) of elements later in DOM?
(1 answer)
Closed 2 years ago.
I made my navigation bar and positioned it (fixed), and it works fine. I was able to scroll down and all. As soon has I added filter (brightness) to it the image on my page, the navigation bar disappeared. I have tried using pseudo-elements and setting the position (absolute/relative), I set the filter property to the container of the child element of the image, it still didn't work. Can someone help me on how to have my navigation bar display on fixed position and still have the image filtered. Thanks in Advance.
nav {
position: fixed;
background-color: #fff;
}
nav ul {
margin: 0;
padding: 0;
}
.navbar-brand {
padding-right: 20px;
}
nav li {
display: inline-block;
padding: 10px;
}
nav a {
color: #000;
text-decoration: none;
}
nav a:hover {
color: #ff6600;
text-decoration: none;
}
.title-image img {
width: 100%;
height: 300px;
filter: brightness(60%);
}
<nav>
<ul>
<li>
<a class="navbar-brand" href="#">Navbar Brand</a>
</li>
<li>
Home
</li>
<li>
About
</li>
<li>
services
</li>
</ul>
</nav>
<div class="title-image">
<img src="https://images.unsplash.com/photo-1599546824091-f49550ce8cbc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60">
</div>
JSfiddle Demo
Just add z-index to your nav element as follow
nav{
position: fixed;
background-color: #fff;
z-index:999;
}
nav ul{
margin: 0;
padding: 0;
}
.navbar-brand{
padding-right: 20px;
}
nav li{
display: inline-block;
padding: 10px;
}
nav a{
color: #000;
text-decoration: none;
}
nav a:hover{
color: #ff6600;
text-decoration: none;
}
.title-image img{
width: 100%;
height: 300px;
filter: brightness(60%);
}
<nav>
<ul>
<li>
<a class="navbar-brand" href="#">Navbar Brand</a>
</li>
<li>
Home
</li>
<li>
About
</li>
<li>
services
</li>
</ul>
</nav>
<div class="title-image">
<img src="https://images.unsplash.com/photo-1599546824091-f49550ce8cbc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60">
</div>
The navbar didn't disappear, it is just beneath the image. To have it in front, you should use z-index: 10; (or any value greater than 0).
See more at : https://developer.mozilla.org/en-US/docs/Web/CSS/z-index
In addition, keep in mind that your image - or any element after your navbar - will be positioned on top of your page. May be you'll want to let the equivalent of the navbar height as space before any content.

Why is my paragraph element displaying behind a background color

So im making a website for a school project and all was hunky dory until i tried to put a paragraph element in and it displays above the title text behind the background color
.container {
width: 80%;
margin: 0;
padding: 0;
}
#logotext {
float: left;
font-family: 'Doppio One';
margin-left: 25px;
}
nav {
float: right;
}
#nav {
list-style-type: none;
text-decoration: none;
margin-top: 35px;
}
ul li {
display: inline;
}
li a {
text-decoration: none;
color: white;
}
li a:hover {
color: #fc9516;
}
.darkwrap {
background-color: #414a4c;
position: fixed;
overflow: hidden;
}
.active {
color: #22cc25;
}
#clock {
position: absolute;
top: 0;
right: 0;
margin-top: 25px;
margin-right: 25px;
font-family: Rajdhani;
font-size: 30px;
}
<div class="container darkwrap">
<div id="logotext">
<h1>JF Web Design</h1>
</div>
<!-- Navigation Bar -->
<nav>
<ul id="nav">
<li> Page 1 </li>
<li> About </li>
<li> Page 3 </li>
</ul>
</nav>
</div>
</header>
<span id="clock"></span>
<p>
Hello
</p>
<footer></footer>
i usedchrome to highlight the faulty element so its clear whats happening here its literall positioned at the top behind the bg color
Console Highhlighted element
.darkwrap is position: fixed.
This takes it out of normal flow and locks its position relative to the viewport.
The rest of the content is laid out as normal as if the .darkwrap element didn't exist … so it ends up covered up by it.
You could use margins to compensate for the space covered up by .darkwrap when the viewport is scrolled to the top. I would simply avoid using position: fixed in the first place: The benefits of having the menu on screen all the time very rarely outweigh the drawback of using up all that vertical space all the time.
If you use float: left and float:right please remember to add clear:both to the next element on the website. Here is fixed code:
https://codepen.io/anon/pen/jKRqLz

Li elements in navbar don't fill the available space of navbar

I make a hybrid mobile app with PhoneGap and jQuery Mobile.
I made a footer navbar, and my problem is that the list elements of the navbar don't fill the available space of the navbar, only the 4th list element, the other 3 is shorter a bit, and remains a little white line at the bottom of the navbar. Here is the HTML code:
<div data-role="footer" data-position="fixed" data-tap-toggle="false">
<div data-role="navbar" class="ui-custom-navbar">
<ul id="navbarul">
<li><img src="img/lips.png" /><p>thing_1</p></li>
<li><img src="img/lips.png" /><p>thing_2</p></li>
<li><img src="img/lips.png" /><p>thing_3</p></li>
<li><img src="img/lips.png" /><p>thing_4</p></li>
</ul>
</div>
</div>
The problem hasn't appeared until I had put for the images the max-width: 100%; property in the CSS file, because without it the images would be too large.
Here is the CSS code:
.ui-custom-navbar a {
background-color: #3BA6D2 !important;
}
.ui-custom-navbar a:hover{
background-color: #317996 !important;
}
.ui-custom-navbar p {
color: #ffffff !important;
text-shadow: none;
margin-top: 0;
margin-bottom: 0;
text-align: center;
}
.ui-grid-c > .ui-block-a, .ui-grid-c > .ui-block-b, .ui-grid-c > .ui-block-c, .ui-grid-c > .ui-block-d {
max-height: 6em !important;
}
.ui-block-a .ui-btn-active, .ui-block-b .ui-btn-active, .ui-block-c .ui-btn-active, .ui-block-d .ui-btn-active{
border-color: #ffffff !important;
}
#navbarul li img{
border: 0;
padding: 0;
margin: 0;
max-width: 100%;
}
The result is here on the picture: image
What would be the solution?
Thank you for the answers in advance.
jQM has a CSS rule that applies a negative right margin to the last ui-btn in the navbar. You can override it by adding this CSS:
.ui-navbar li:last-child .ui-btn {
margin-right: 0;
}
Here is a working DEMO

Issue with bottom padding/margin with Twitter Bootstrap

So, I'm working on quickly building a website theme (Wordpress) with the aid of Twitter bootstrap, and I'm running into a problem.
I've got a header thrown together, and I've got this weird gap going on inside the "pull-right" section, not sure why:
I'm not sure what the deal is, I want them sitting on the line right at the same height of the text on the left.
Anyway, I've got the following relevant sections of code:
(HTML for header section):
<!-- Header -->
<div class="row header-container">
<div class="col-md-8 col-md-offset-2">
<div class="pull-left">
<h3>CharlesBaker.net</h3>
</div>
<div class="pull-right">
<ul>
<li>Test1</li>
<li>Test2</li>
<li>Test3</li>
</ul>
</div>
</div>
</div>
(CSS for the same section):
.header-container {
padding-top: 50px;
background-color: #c0c0c0;
border-bottom: 5px solid #880000;
}
.header-container h3 {
margin: 0;
padding: 0;
font-family: 'Montserrat', serif;
}
.header-container ul {
margin: 0;
padding: 0;
}
.header-container ul li {
display: inline;
margin: 0;
padding: 0;
}
.header-container a {
padding: 3px;
color: black;
}
.header-container a:hover {
text-decoration: none;
background-color: #880000;
color: white;
}
Not sure what the issue is, unless it's related to the fact that I'm using a tag instead of just styling a <span> or something, but since I removed the padding/margin using CSS, I wouldn't think that would be the problem.
Any help would be great. The idea is that when I hover over the links on the right, that they're enclosed in a scarlet colored box that "extends" from the 5px bottom border.
Thanks in advance!
Are you looking for this?
You need to set your .header-container as display: inline-block to align all elements inside. Therefore, you need to float your pull div elements (float and left).
Just one last change, set the width size of your header: I set 100%, but you can set whatever you like :)
CSS:
.header-container {
padding-top: 50px;
background-color: #c0c0c0;
border-bottom: 5px solid #880000;
width: 100%;
display: inline-block;
}
.pull-left {
float: left;
}
.pull-right {
float: right;
}
There is a particular line-height property for h3 tag with bootstrap.
h1, h2, h3 {
line-height: 40px;//line 760
}
So you will have to add style to negotiate this additional height.
Also another set for your ul as :
ul, ol {
margin: 0 0 10px 25px; //line 812
}
Solution :
Over-ride the ul margin as follows :
.pull-right ul{
margin: 0;
}
Over-ride the line-height for the h3 as follows :
.pull-left h3{
line-height:20px;
}
First one is pretty straight forward and gives you correct alignment straighaway. Second solution will need you to work some more with tweaking the negative-margins for .pull-right.
Debugging URL : http://jsbin.com/oToRixUp/1/edit?html,css,output
Hope this helps.