Why a gap between ul li [duplicate] - html

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 6 years ago.
In the following code, I am not able to understand that why is there a small margin between the green li's Home , Products , Services , About Us, Contact
I have set the margin and padding to 0px in both .ul and .li properties. Then why is there a gap between the green li's?
*{
box-sizing:border-box;
}
html,body {
margin: 0px;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
background-color: rgba(173,192,241,1);
}
.wrapper {
height: 725px;
max-width: 960px;
margin-left: auto;
left: 0px;
top: 0px;
/* [disabled]background-color: rgba(15,26,155,1); */
margin-right: auto;
position: relative;
}
.topimage {
width: 100%;
max-width: 960px;
height: 100%;
max-height: 175px;
/* [disabled]background-color: rgba(0,102,204,1); */
position: absolute;
/* [disabled]border: thin solid rgba(255,0,0,1); */
}
.topimage img{
max-width: 100%;
max-height: 100%;
/* [disabled]margin-bottom: -9px; */
display: block;
margin-right: auto;
margin-left: auto;
border-radius: 15px 15px 0px 0px;
}
.menu {
background-color: rgba(15,26,155,1);
height: 100%;
max-height: 50px;
max-width: 960px;
position: relative;
top: 175px;
}
.list {
color: rgba(0,0,0,1);
text-decoration: none;
margin-right: auto;
margin-left: auto;
background-color: rgba(255,0,0,1);
padding: 0px;
}
.list li {
display: inline-block;
margin-right: 0px;
margin-left: 0px;
width: auto;
text-align: center;
background-color: rgba(204,255,0,1);
position: relative;
padding: 0px;
}
.content {
width: 100%;
height: 500px;
background-color: rgba(20,35,214,1);
position: relative;
top: 175px;
padding-right: 0.5%;
padding-left: 0.5%;
}
.leftcontent {
background-color: rgba(210,238,252,1);
float: left;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 85%;
top: 0px;
position: relative;
border-left-color: rgba(205,205,205,1);
/* [disabled]margin-left: 0.3%; */
}
.rightcontent {
background-color: rgba(51,177,236,1);
float: right;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 15%;
position: relative;
top: 0px;
/* [disabled]margin-right: 0.3%; */
}
.footer {
}
<div class="wrapper">
<div class="topimage">
</div>
<div class="menu">
<ul class="list">
<li>Home</li>
<li>Products</li>
<li>Services</li>
<li>About Us</li>
<li>Contact</li>
</ul>
</div>
<div class="content">
<div class="leftcontent">
</div>
<div class="rightcontent">
</div>
</div>
</div>

If you have no new lines between the list elements then it works. So you could put all on one line (see snippet) or add font-size: 0; to the <ul>
ul {font-size:0;}
*{
box-sizing:border-box;
}
html,body {
margin: 0px;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
background-color: rgba(173,192,241,1);
}
.wrapper {
height: 725px;
max-width: 960px;
margin-left: auto;
left: 0px;
top: 0px;
/* [disabled]background-color: rgba(15,26,155,1); */
margin-right: auto;
position: relative;
}
.topimage {
width: 100%;
max-width: 960px;
height: 100%;
max-height: 175px;
/* [disabled]background-color: rgba(0,102,204,1); */
position: absolute;
/* [disabled]border: thin solid rgba(255,0,0,1); */
}
.topimage img{
max-width: 100%;
max-height: 100%;
/* [disabled]margin-bottom: -9px; */
display: block;
margin-right: auto;
margin-left: auto;
border-radius: 15px 15px 0px 0px;
}
.menu {
background-color: rgba(15,26,155,1);
height: 100%;
max-height: 50px;
max-width: 960px;
position: relative;
top: 175px;
}
.list {
color: rgba(0,0,0,1);
text-decoration: none;
margin-right: auto;
margin-left: auto;
background-color: rgba(255,0,0,1);
padding: 0px;
}
.list li {
display: inline-block;
margin-right: 0px;
margin-left: 0px;
width: auto;
text-align: center;
background-color: rgba(204,255,0,1);
position: relative;
padding: 0px;
}
.content {
width: 100%;
height: 500px;
background-color: rgba(20,35,214,1);
position: relative;
top: 175px;
padding-right: 0.5%;
padding-left: 0.5%;
}
.leftcontent {
background-color: rgba(210,238,252,1);
float: left;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 85%;
top: 0px;
position: relative;
border-left-color: rgba(205,205,205,1);
/* [disabled]margin-left: 0.3%; */
}
.rightcontent {
background-color: rgba(51,177,236,1);
float: right;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 15%;
position: relative;
top: 0px;
/* [disabled]margin-right: 0.3%; */
}
.footer {
}
<div class="wrapper">
<div class="topimage">
</div>
<div class="menu">
<ul class="list">
<li>Home</li><li>Products</li><li>Services</li><li>About Us</li><li>Contact</li>
</ul>
</div>
<div class="content">
<div class="leftcontent">
</div>
<div class="rightcontent">
</div>
</div>
</div>

Try this -
.list li {
float: left;
list-style: none;
width: auto;
text-align: center;
background-color: rgba(204,255,0,1);
position: relative;
}
Replace with above css with your existing .list li class

You can remove space created by inline-block element as with following trick.
Or you can use float instead of inline-block.
.list {
letter-spacing: -4px;
font-size: 0;
}
.list li {
display: inline-block;
vertical-align: top;
letter-spacing: 0;
font-size: 14px;
}
Or you can use float:
.list {
overflow: hidden;
}
.list li {
float: left;
}

Just add this into your code,
.list{
font-size:0;
}
.list li{
font-size:20px;
padding:0px 0px 0px 10px;
}

Related

How to set margin between fixed header and footer in print page?

in my html, Header and Footer are fixed but body content overlaps with header and footer. I want to display my content between header and footer without overlap. How can it be achieved ?
Please help me regarding this.i'm using this CSS code mentioned below.
You may check it and resolve this problem.
<style type="text/css">
#media print {
#header {
display: table-header-group;
position: fixed;
top: 0;
left: 0;
margin: 0px;
padding: 0px;
width: 100%;
}
#body {
/*position: absolute;
height: 80%;
margin-top: 0em !important;
margin-bottom: 1em !important;
padding: 2em 0 0 0;
margin:0 auto;*/
position: absolute;
margin-top: 10% !important;
margin-bottom: 5% !important;
height: 80%;
overflow: visible;
text-align: justify;
width: 90%;
}
#footer {
display: table-footer-group;
position: fixed;
bottom: -0.6em;
left: 0;
margin: 5em 0px 0px 0px;
padding: 0px;
width: 100%;
/*clear:both;*/
/*padding-top:98%;*/
/*padding-bottom:1em;*/
/*page-break-after: avoid;*/
}
}
#media screen {
#thead {
display: block;
/*padding-right: 5.9em;
padding-left: 6px;*/
width: 100%;
/*height: 5%;*/
}
#tbody {
display: block;
/*height: 80%;
vertical-align: central;
padding-top: 5em;
padding-bottom: 3em;*/
text-align: justify;
width: 100%;
margin-top: -5em;
}
#tfoot {
display: block;
/*padding-right: 6em;
padding-top: 2em;*/
width: 100%;
/*height: 4%;*/
}
}
</style>
This snippet shows an example of fixed header and footer (with variable height) and content that takes the remaining space. The trick here is to use flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
body {
overflow: hidden;
}
.content-container {
display: flex;
flex-direction: column;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#header {
position: relative;
width: 100%;
background-color: green;
}
#body {
position: relative;
width: 100%;
background-color: blue;
overflow: auto;
}
#footer {
position: relative;
width: 100%;
background-color: red;
}
<div class="content-container">
<div id="header">
<div style="height: 40px;"></div>
</div>
<div id="body">
<div style="height: 1000px;"></div>
</div>
<div id="footer">
<div style="height: 20px;"></div>
</div>
</div>

How to move image to center of div using css?

I tried to add margin-left : 25%; to class .four but not work. how can i do ?
.one{
width: 100%;
}
.two{
margin: 0;
padding: 0;
border: 0;
background: transparent;
float: left;
position: relative;
width: 100%;
margin-bottom: 15px;
}
.three{
float: left;
list-style: none;
color: #333;
font-size: 19px;
width: 100%;
background: #fff;
border: 1px solid #ccc;
padding: 10px 0px;
}
.four{
margin-left: 10px;
width: 246px;
height: 138px;
margin-right: 16px;
display: inline-block;
position: relative;
float: left;
}
.five{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100%;
margin-left: auto;
margin-right: auto;
overflow: hidden;
display: block;
cursor: pointer;
}
.six{
transform: translateY(-50%);
top: 50%;
left: 0;
background-color: transparent;
display: block;
position: absolute;
}
.seven{
width: 246px;
display: block;
margin-left: auto;
margin-right: auto;
max-width: unset;
}
<div class="one">
<ul class="two">
<li class="three">
<div class="four">
<a class="five" href="">
<div class="six">
<img class="seven" src="http://kawamono.com/img/cms/Hobby/Coin%20Bank/x100828-sanrio-hellokitty-mini-bank-ylw-SET.jpg.pagespeed.ic.iSCJA9hZBG.jpg">
</div>
</a>
</div>
</li>
</ul>
</div>
Change the css of .four to this:
.four {
width: 246px;
height: 138px;
margin: 0 auto;
position: relative;
}
.one {
width: 100%;
}
.two {
margin: 0;
padding: 0;
border: 0;
background: transparent;
float: left;
position: relative;
width: 100%;
margin-bottom: 15px;
}
.three {
float: left;
list-style: none;
color: #333;
font-size: 19px;
width: 100%;
background: #fff;
border: 1px solid #ccc;
padding: 10px 0px;
}
.four {
width: 246px;
height: 138px;
margin: 0 auto;
position: relative;
}
.five {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100%;
margin-left: auto;
margin-right: auto;
overflow: hidden;
display: block;
cursor: pointer;
}
.six {
transform: translateY(-50%);
top: 50%;
left: 0;
background-color: transparent;
display: block;
position: absolute;
}
.seven {
width: 246px;
display: block;
margin: 0 auto;
max-width: unset;
}
<div class="one">
<ul class="two">
<li class="three">
<div class="four">
<a class="five" href="">
<div class="six">
<img class="seven" src="http://kawamono.com/img/cms/Hobby/Coin%20Bank/x100828-sanrio-hellokitty-mini-bank-ylw-SET.jpg.pagespeed.ic.iSCJA9hZBG.jpg">
</div>
</a>
</div>
</li>
</ul>
</div>
I noticed that you want to center the image yet you are using float:left to .four, it is conflicting to the result you are expecting. And I changed your margins to margin:0 auto; to properly center it horizontally
You can add:
width: 100%;
To style for .four and .six, This will center the image sideways. Those divs does not have a with, so the the image will not center.
Hope this helps 😄
Oel Kristian Ek Hornnes

parent ul color in the background of dropdown child li

In this code (practicing css dropdown menu) the background color of the main parent ul is Red, and background color of the dropdown child li "Sub" is violet. Now when I tried to move the dropdown child li "Sub" 20px right from its position, I see the background color Red still appearing. You can see this if you hover your mouse over li "Hello" I dont want that. It's like something shifting from its placeholder leaving behind the original placeholder color. I want no background color if I shift the dropdown child li "Sub" from its original position to either left/right/up/down
html body {
height: 100%;
width: 100%;
left: 0px;
top: 0px;
margin-top: 0px;
margin-left: 0px;
}
.roundborder {
border-radius:5px
}
.container {
margin-top: 5%;
margin-right: 10%;
margin-bottom: 0px;
margin-left: 10%;
width: auto;
background-color: rgba(153,204,0,1);
height: 25px;
position: relative;
left: 0px;
/* [disabled]top: 0px; */
}
ul {
text-align: center;
/* [disabled]border: thin solid rgba(0,0,0,1); */
width: 100%;
margin-top: 0;
margin-right: auto;
margin-bottom: 0px;
margin-left: 0%;
padding-top: 0;
padding-right: 0px;
padding-bottom: 0;
padding-left: 0px;
background-color: rgba(255,0,0,1);
height: 25px;
position: absolute;
}
li {
display: inline-block;
/* [disabled]border: 1px solid black; */
/* [disabled]padding: 5px; */
height: 25px;
width: 40px;
margin-top: auto;
margin-right: 30px;
margin-bottom: auto;
margin-left: 30px;
position: relative;
background-color: rgba(0,0,255,1);
}
ul > li > ul {
padding-left: 0px;
width: 100%;
height: auto;
visibility: hidden;
position: absolute;
left: 0px;
top: 100%;
text-align: center;
/* [disabled]background-color: rgba(255,255,255,1); */
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
border-style: none;
}
ul > li > ul > li {
display: block;
list-style-type: none;
width: 40px;
margin-left: auto;
margin-right: auto;
position: relative;
border: 1px solid rgba(0,255,102,1);
left: 20px;
background-color: rgba(204,102,153,1);
}
ul li:hover > ul {
visibility:visible;
}
<div class="container">
<ul class="roundborder">
<li>Hello
<ul>
<li>Sub</li>
</ul>
</li>
<li>Cool</li>
<li>World</li>
</ul>
</div>
You need to set your ul > li > ul{ to have background:transparent; because it is currently getting background:red; from ul{
html body {
height: 100%;
width: 100%;
left: 0px;
top: 0px;
margin-top: 0px;
margin-left: 0px;
}
.roundborder {
border-radius:5px
}
.container {
margin-top: 5%;
margin-right: 10%;
margin-bottom: 0px;
margin-left: 10%;
width: auto;
background-color: rgba(153,204,0,1);
height: 25px;
position: relative;
left: 0px;
/* [disabled]top: 0px; */
}
ul {
text-align: center;
/* [disabled]border: thin solid rgba(0,0,0,1); */
width: 100%;
margin-top: 0;
margin-right: auto;
margin-bottom: 0px;
margin-left: 0%;
padding-top: 0;
padding-right: 0px;
padding-bottom: 0;
padding-left: 0px;
background-color: rgba(255,0,0,1);
height: 25px;
position: absolute;
}
li {
display: inline-block;
/* [disabled]border: 1px solid black; */
/* [disabled]padding: 5px; */
height: 25px;
width: 40px;
margin-top: auto;
margin-right: 30px;
margin-bottom: auto;
margin-left: 30px;
position: relative;
background-color: rgba(0,0,255,1);
}
ul > li > ul {
padding-left: 0px;
width: 100%;
height: auto;
visibility: hidden;
position: absolute;
left: 0px;
top: 100%;
text-align: center;
/* [disabled]background-color: rgba(255,255,255,1); */
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
border-style: none;
background:transparent;
}
ul > li > ul > li {
display: block;
list-style-type: none;
width: 40px;
margin-left: auto;
margin-right: auto;
position: relative;
border: 1px solid rgba(0,255,102,1);
left: 20px;
background-color: rgba(204,102,153,1);
}
ul li:hover > ul {
visibility:visible;
}
<div class="container">
<ul class="roundborder">
<li>Hello
<ul>
<li>Sub</li>
</ul>
</li>
<li>Cool</li>
<li>World</li>
</ul>
</div>
Try this. It's because you had left:20px; so when it was hovering it was adding the 20px; which included the red background.
html body {
height: 100%;
width: 100%;
left: 0px;
top: 0px;
margin-top: 0px;
margin-left: 0px;
}
.roundborder {
border-radius:5px
}
.container {
margin-top: 5%;
margin-right: 10%;
margin-bottom: 0px;
margin-left: 10%;
width: auto;
background-color: rgba(153,204,0,1);
height: 25px;
position: relative;
left: 0px;
/* [disabled]top: 0px; */
}
ul {
text-align: center;
/* [disabled]border: thin solid rgba(0,0,0,1); */
width: 100%;
margin-top: 0;
margin-right: auto;
margin-bottom: 0px;
margin-left: 0%;
padding-top: 0;
padding-right: 0px;
padding-bottom: 0;
padding-left: 0px;
background-color: rgba(255,0,0,1);
height: 25px;
position: absolute;
}
li {
display: inline-block;
/* [disabled]border: 1px solid black; */
/* [disabled]padding: 5px; */
height: 25px;
width: 40px;
margin-top: auto;
margin-right: 30px;
margin-bottom: auto;
margin-left: 30px;
position: relative;
background-color: rgba(0,0,255,1);
}
ul > li > ul {
padding-left: 0px;
width: 100%;
height: auto;
visibility: hidden;
position: absolute;
left: 0px;
top: 100%;
text-align: center;
/* [disabled]background-color: rgba(255,255,255,1); */
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
border-style: none;
}
ul > li > ul > li {
display: block;
list-style-type: none;
width: 40px;
margin-left: auto;
margin-right: auto;
position: relative;
border: 1px solid rgba(0,255,102,1);
background-color: rgba(204,102,153,1);
}
ul li:hover > ul {
visibility:visible;
}
<div class="container">
<ul class="roundborder">
<li>Hello
<ul>
<li>Sub</li>
</ul>
</li>
<li>Cool</li>
<li>World</li>
</ul>
</div>

CSS3 - Sticky position not work

i'm try to create navigation menu under header section with sticky positioning but i can't get it work, here my css code :
body{
}
header{
position: relative;
top: 0px;
left: 0px;
height: 100px;
width: 100%;
background-color: whitesmoke;
text-align: left;
float: left;
padding: 5px;
z-index:100;
}
#logo{
position: relative;
height: 85px;
width: 120px;
/*border: 1px solid black;*/
float: left;
background-image: url('logoArdi.svg');
background-position: center;
background-size: contain;
}
#judulWeb{
position: relative;
height: 85px;
width: 200px;
text-align: center;
float: left;
}
#kontak{
position: inherit;
float: right;
height: 100px;
top: 20px;
right: 10px;
}
.imghead{
margin: auto 5px auto auto;
}
.sticky{
position: sticky;
position: -webkit-sticky;
position: -o-sticky;
top: 0px;
float: left;
width: 100%;
height:30px;
background-color: #26282b;
text-align: center;
box-shadow: 0px 2px 10px #26282b;
}
ul li{
position:relative;
bottom:11px;
display: inline-block;
margin: auto 30px auto auto;
}
ul li{
color:whitesmoke;
/*background-color:black;
border-radius: 5px 5px 0px 0px;
padding: 5px;*/
}
ul li:hover{
color:#26282b;
background-color: whitesmoke ;
}
#kontak{
position: inherit;
float: right;
height: 100px;
}
#wrapper{
position: relative;
top: 132px;
width: 80%;
float: left;
margin-right: 10%;
margin-left: 10%;
text-align: center;
background-color: transparent;
}
.imgutama{
width: 200px;
}
i don't now the mistake, but i just intend to create page header and navigation, the navigation is below the page header and will stick on top even i scroll down.
note : navigation class is "sticky".
#sidebar {
position: -webkit-sticky; // required for Safari
position: sticky;
top: 0; // required as well.
}

Elements display in front of their container, but can't be interacted with

I'm currently working on a website and have encountered an interesting problem. I have a container which holds my navbar. For some reason, this container displays behind its contents (Which it should) but mouse interaction acts as if it's in front of its contents.
JSFiddle of my current code
https://jsfiddle.net/qzsxpgrq/
HTML
<div id="navbar">
<div id="bar">
<div id="navLeft">
<div class="navL navbutton">About</div>
<div class="navL navbutton">The Team</div>
</div>
<div id="navLogo">
<div id="logo">
<img src="http://www.epicyoobed.com/res/img/navLogo.png"/>
</div>
</div>
<div id="navRight">
<div class="navR navbutton">Programs</div>
<div class="navR navbutton">Contact</div>
</div>
</div>
</div>
CSS
html {
height: 100%;
font-family: arial;
}
body {
margin: 0;
padding: 0;
background-color: #BBB;
}
#navbar {
margin: 0;
padding: 0;
height: 80px;
width: 100%;
position: fixed;
display: block;
z-index: 1;
}
#bar {
display: block;
position: fixed;
width: 100%;
height: 40px;
background-image: url("http://www.epicyoobed.com/res/img/nav.png");
}
#navRight {
margin: 0;
padding: 0;
height: 40px;
width: 45%;
position: absolute;
display: block;
float: left;
top: 0;
right: 0;
}
#navbar div div a {
display: block;
text-decoration: none;
padding-top: 5px;
color: #000;
width: 100%;
height: 100%;
font-size: 20px;
}
.current {
background-image: url("http://www.epicyoobed.com/res/img/nav_sel.png");
background-repeat: repeat-x;
}
.navL {
float: right;
}
.navR {
float: left;
}
.navbutton {
display: block;
position: relative;
width: 150px;
height: 100%;
text-align: center;
}
.navbutton:hover {
background-image: url("http://www.epicyoobed.com/res/img/nav_sel.png");
background-repeat: repeat-x;
}
#navLogo {
margin: auto;
height: 40px;
width: 100%;
position: absolute;
display: block;
z-index: 2;
}
#logo {
margin: auto;
height: 80px;
width: 80px;
}
#navLeft {
margin: 0;
padding: 0;
height: 40px;
width: 45%;
position: absolute;
display: block;
float: right;
}
Xetanai Try adding a z-index of 3 to the #navLeft and navRight.
#navLeft {
margin: 0;
padding: 0;
height: 40px;
width: 45%;
position: absolute;
display: block;
float: right;
z-index:3;
}
#navRight {
margin: 0;
padding: 0;
height: 40px;
width: 45%;
position: absolute;
display: block;
float: left;
top: 0;
right: 0;
z-index:3;
}
The issue you're experiencing is because the logo is 100% width and has a z-index of 2. Effectively placing it above the left and right containers.