How to fix different browsers changing my div width? - html

This is normally a question I don't need answered, however, I am baffled. I have tried everything I can think of.
This picture includes a gray piece at the right. That is the scroll bar, figured I would include it. This is on Google Chrome and how I want it.
And this is how Microsoft Edge does it.
I narrowed the problem down to my list items <li>. On Google Chrome (according to my resolutions), the width of <li> is 226.141px and on Microsoft Edge, using the same exact CSS, the width of <li> is 226.35px.
Here is my style.css:
div.menuContainer {
width:100%;
height:48px;
}
ul.menuItems {
/*margin:-12px;*/
}
li.menuItem {
display:inline-block;
box-sizing:border-box;
width:calc(100% / 7);
float:left;
text-align:center;
height:48px;
font-weight:bold;
background-color:#DD4400;
color:#454545;
padding:12px 0;
overflow:hidden !important;
}
li.menuItem:hover {
background-color:#454545;
color:#DD4400;
}
li.menuItem a {
color:#454545;
}
li.menuItem a:hover {
color:#DD4400;
}
Here is my menu:
<div class="menuContainer">
<ul class="menuItems">
<a href="/videos.php">
<li class="menuItem">
Videos
</li>
</a>
<a href="/playlists.php">
<li class="menuItem">
Playlists
</li>
</a>
<a href="/categories.php">
<li class="menuItem">
Categories
</li>
</a>
<a href="/actors.php">
<li class="menuItem">
Actors
</li>
</a>
<a href="/photos.php">
<li class="menuItem">
Photos
</li>
</a>
<?php if($logged_in == 0) { ?>
<a href="/login.php">
<li class="menuItem">
Login
</li>
</a>
<a href="/register.php">
<li class="menuItem">
Register
</li>
</a>
<?php }
else { ?>
<a href="/logout.php">
<li class="menuItem">
Logout
</li>
</a>
<a href="/account.php">
<li class="menuItem">
My Account
</li>
</a>
<?php }
?>
</ul>
</div>
Any help you can provide would be greatly appreciated. Thank You.

Here's a different way of approaching the same problem using flexbox. I also made some slight modifications to your HTML. The key to this approach is using display: flex on the container and flex-grow: 1 on the children of that container. Note: I also use display: flex to achieve vertical and horizontal centering on the <a> elements, rather than having to use top and bottom padding.
.menu {
height: 48px;
display: flex;
}
.menu a {
flex-grow: 1;
display: flex;
font-weight: bold;
background-color: #DD4400;
color: #454545;
align-items: center;
justify-content: center;
text-decoration: none;
}
.menu a:hover {
text-decoration: underline;
}
<div class="menu">
Videos
Playlists
Categories
Actors
Photos
Logout
My Account
</div>
Here's a quick article explaining what flexbox can do for you - css-tricks.com/snippets/css/a-guide-to-flexbox. I'd highly recommend adding this to your toolkit - it's relatively modern, but has very good browser support caniuse.com/#feat=flexbox. I use it very regularly.

Do a reset in your css, as a first thing:
* {
/* So 100% means 100% */
box-sizing: border-box;
}
See link for details.

Related

CSS spacing between li elements inside a ul list

I have a ul list like below. I want to make equal space between each li elements, in all viewports - desktops/ mobile/ table.
I had give padding-right for the icons inside the li, but this does break when seen in other resolutions (like mobile).
How to give equal ideal spacing between the li elements in all the resolutions?
I want the horizontal list of li elements to be center-aligned and equally spaced on whichever screen (desktop/ phone/ tablet)
<ul class="navbar-nav navbar-right">
<li class="dropdown">
<a href="javascript:void(0);">
<img src="1.svg" class="imgicon">
<span class="username">li 1</span>
</a>
</li>
<li class="dropdown" id="li2" >
<a href="javascript:WindowLocation('/123');">
<img src="2.svg" class="imgicon">
<span class="hidden-xs">li 2</span>
</a>
</li>
<li class="dropdown" id="li3" >
<a href="javascript:WindowLocation('/123');">
<img src="3.svg" class="imgicon">
<span class="hidden-xs">li 3</span>
</a>
</li>
<li class="dropdown" id="li4" >
<a href="javascript:WindowLocation('/123');">
<img src="4.svg" class="imgicon">
<span class="hidden-xs">li 4</span>
</a>
</li>
</ul>
as #Laif suggested you can try the flexbox. Flex can does exactly what you are asking for.
Notice the below code. it has 4 li inside ul and we have space between all li.
on mobile it will look like
ul {
display:flex;
list-style:none;
padding: 0px;
justify-content: space-between;
}
li{
border:1px solid grey;
padding:5px 10px;
}
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>
`
Solution 1. Try viewport meta tag to tell the browsers on whichever device to use the same measurements. It maintains the same aspect ratio on all devices.
Solution 2. If solution 1 does not work for you, then try css media queries to make the right spacing on mobile and other screens.
try this:
in css you can apply
*{
margin: 0;
padding: 0;
}
ul
{
text-align: center;
}
ul li
{
list-style: none;
display: inline-block;
padding: 20px;
}
#media(max-width:340px)
{
ul li
{
padding: 15px;
}
}
you will get your li in center of the screen in all the resolution

Adaptative width with flexbox and flex-grow

I'm trying to set up a menu bar with the
following template
I'm trying this with FlexBox but I can't figure out what's wrong. Here's the HTML :
<nav>
<ul>
<li id="navleft">
<img src="images/logo.png" alt="logo wikihow">
</li>
<li class="navothers">
<img src="images/contribuye.png" alt="contribuye">
<p>CONTRIBUYE</p>
</li>
<li class="navothers">
<img src="images/explora.png" alt="explora">
<p>EXPLORA</p>
</li>
<li class="navothers">
<img src="images/entrar.png" alt="entrar">
<p>ENTRAR</p>
</li>
<li class="navothers"><img src="images/mensajes.png" alt="mensajes">
<p>MENSAJES</p>
</li>
</ul>
</nav>
And I apply the following styles in CSS :
nav {
padding: 0 30% 0 30%;
}
nav ul {
display:flex;
justify-content:center;
align-items:center;
background-color: #93B874;
}
#navleft{
flex-grow:32;
flex-shrink:1;
}
#navleft img{
width: 144px;
vertical-align:center
}
.navothers{
flex-grow:1;
flex-shrink:4;
}
I get the following result
My problem is that all the elements in the right part (with the "navothers" class) dont have the same width ! They just adapt depending on the size of the text they have.
I may have mixed a lot of things, what have I done wrong ?
Your flex-grow value for #navleft is too big, and your container is too small. Your items can't grow they don't have enough room. Try this :
CSS :
nav {
padding: 0 10% 0 10%; // imho it's a strange way to center your nav. Reduced to show you that you're lacking room.
}
nav ul {
display: flex;
justify-content: center;
align-items: center;
background-color: #93B874;
}
#navleft {
flex: 2; // You may tune this one but keep room for your other items !
}
#navleft img {
width: 144px;
vertical-align: center
}
.navothers {
flex: 1;
border: solid 1px #000;
}
HTML :
<nav>
<ul>
<li id="navleft">
<img src="images/logo.png" alt="logo wikihow">
</li>
<li class="navothers">
<img src="images/contribuye.png" alt="contribuye">
<p>CONTRIBUYE</p>
</li>
<li class="navothers">
<img src="images/explora.png" alt="explora">
<p>EXPLORA</p>
</li>
<li class="navothers">
<img src="images/entrar.png" alt="entrar">
<p>ENTRAR</p>
</li>
<li class="navothers">
<img src="images/mensajes.png" alt="mensajes">
<p>MENSAJES</p>
</li>
</ul>
</nav>
https://jsfiddle.net/me7t2hv3/

ul list gets hidden under a div

I've been having this issue in different places where I have a ul list and Im trying to add it on top of a div (image, or a background) but the list does not appear on top. I wonder if it gets sent in the back..i even added z-index
JSFiddle
CSS
.toolbar p{ text-align: right; padding: 10px 180px 0 0; color: #fff; font-size: 26px; z-index: 1; }
.toolbar { width: auto; height: 50px; background-color: #cc1e2c; z-index: 1;}
.social-icons { z-index: 2;}
HTML
<div class="col_full toolbar">
<p>CALL NOW: +1 555.555.1234</p>
<ul class="social-icons">
<li> </li>
<li> </li>
<li> </li>
</ul>
</div>
The reason you aren't seeing anything is because you posted links, without any content. If you added words, the list would appear:
<div class="col_full toolbar">
<p>CALL NOW: +1 555.555.1234</p>
<ul class="social-icons">
<li> Twitter </li>
<li> Instagram</li>
<li> Facebook</li>
</ul>
</div>
On this example I made the height bigger so you could see that the links are in fact on top.
If you want to see the images, you need to use the image tag like so:
<li> <img src="path/to/your/twitter_image.jpg"/> </li>
JSFiddle

Extra padding is added in mozilla for content

Below is my code:
#sam_ul li {
height: 41px;
border-bottom: 1pt solid #DEDEDE;
background-color: #F8F8F8;
}
#sam_ul li {
list-style-type: none;
}
#u_l_add:before {
content: '\0FBF';
}
<ul id="sam_ul" style="margin:0px;">
<li>
<a href="#">
<span id="u_l_add" style="font-size:36px;line-height:20px;"></span>
<div style="width:130px;position:relative;top:-20px;left:40px;">Add</div>
</a>
</li>
<li>
<a href="#">
<span id="u_l_sear" style="font-size:36px;line-height:20px;"></span>
<div style="width:130px;position:relative;top:-20px;left:40px;">Search Artifact</div>
</a>
</li>
</ul>
The content pseudo element is displayed differently in both IE and mozilla. By different I mean in IE it is displaying correctly while in mozilla it is adding some extra padding and displaying the content.
check the difference between the first li element and the second li element.
Can anyone help me with this?
Add padding:0 to unordered list
#sam_ul{
padding:0
}
#sam_ul li {
height: 41px;
border-bottom: 1pt solid #DEDEDE;
background-color: #F8F8F8;
list-style-type: none;
}
#u_l_add:before {
content: '\0FBF'; }
#u_l_sear:before {
content: '\0FBF'; }
<body>
<ul id="sam_ul" style="margin:0px;">
<li>
<a href="#">
<span id="u_l_add" style="font-size:36px;line-height:20px;"></span>
<div style="width:130px;position:relative;top:-20px;left:40px;">Add</div>
</a>
</li>
<li>
<a href="#">
<span id="u_l_sear" style="font-size:36px;line-height:20px;"></span>
<div style="width:130px;position:relative;top:-20px;left:40px;">Search Artifact</div>
</a>
</li>
</ul>
</body>
Try to normalize everything. HTML and body has default margin and padding for every browser that could ruin your design. Almost all block elements has that.
Try:
html,body{
margin: 0;
padding:0;
}
Or download and add normalize.css

CSS Menu does not appear in correct place in IE and Firefox

The following is a screen capture of the issue that i'm faced with. The drop down menu is supposed to appear under the second menu item in the top menu.
The HTML is,
<nav class="nav">
<ul>
<li class="menu-item">Hi Alexander!</li>
<li class="menu-item"><a>My Account</a>
<div class="my-sub-menu">
<ul class="sub-list">
<li class="list-item"><a>History</a></li>
<li class="list-item"><a>Personal Details</a></li>
<li class="list-item"><a>Preferences</a></li>
<li class="list-item"><a>Bonuses</a></li>
<li class="list-item"><a>Wishlist</a></li>
<li class="list-item"><a>Newsletter</a></li>
<li class="list-item"><a>Invite Friends</a></li>
<li class="list-item"><a>FAQ</a></li>
<li class="list-item"><a>Sign out</a></li>
</ul>
</div>
</li>
<li class="menu-item"><a>Contact Us</a></li>
<li class="menu-item"><a>Chat</a></li>
<li class="menu-item"><a>Chat</a></li>
</ul>
</nav>
The CSS is as follows,
.nav {
margin-top: 2px;
position: relative;
float: right;
}
.nav > ul {
padding: 0;
margin: 0;
}
.menu-item{
list-style: none;
float: left;
}
.menu-item .my-sub-menu {
visibility: hidden;
position: absolute;
padding: 0;
margin: 0;
}
.menu-item:hover .my-sub-menu {
visibility: visible;
}
.list-item {
list-style: none;
}
I need the sub menu to appear under the second item in the top menu. This is only in firefox and IE but chrome renders it perfectly. I cant figure out what the issue is. Is there at least e fix that i could use for these two browsers? or another alternative to get around this issue.
Tahnk you in advance.
If you add position:relative to .menu-item it will make the absolute positioning work from the list item itself. The only draw back is if you are using a percentage based width on your drop down it will take the width of the parent li as 100% so a pixel width may have to be specified.
try doing
.sub-list{
padding:0px !important;
}
and if by second menu u want it to come under contact us
then change the position of the div
<div class="my-sub-menu">
<ul class="sub-list">
<li class="list-item"><a>History</a></li>
<li class="list-item"><a>Personal Details</a></li>
<li class="list-item"><a>Preferences</a></li>
<li class="list-item"><a>Bonuses</a></li>
<li class="list-item"><a>Wishlist</a></li>
<li class="list-item"><a>Newsletter</a></li>
<li class="list-item"><a>Invite Friends</a></li>
<li class="list-item"><a>FAQ</a></li>
<li class="list-item"><a>Sign out</a></li>
</ul>
</div>
into the next li element ie cntact us
kind of a fiddle
fiddle ex