Box Sizing irregularity in Safari - css

I'm having difficulty getting an <ul> divided evenly in safari so that the inline <li> elements make up 100% of the width.
The html code is basically:
<ul>
<li>red</li>
<li>blue</li>
<li>green</li>
<li>orange</li>
<li>purple</li>
</ul>
with css:
ul {
list-style: none;
margin: 0;
padding: 0;
width: 500px;
background-color: #9999ff;
}
li {
text-align: center;
text-decoration: none;
display: inline-block;
border-left: 1px solid #000;
width: 20%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
li:last-child {
border-right: 1px solid #000;
}
It seems like this would make 5 evenly spaces list elements which take up 20% (including borders) of the total width each. Works perfectly in Firefox and Chrome but Safari leaves an extra 6 pixels or so at the end. When I remove the box-sizing property then the list becomes too long. I can't seem to make this simple thing work for the life of me.
Here's JSFiddle: http://jsfiddle.net/z2Xdf/7/

It was rendering the wrong way for me in Chrome...at least using jsFiddle.
Inline-block puts some white space on the sides. Remove from li display:inline block and add
display:block;
float:left;
Also, move your background color to li from ul.
If you wanted to keep display:inline-block, you can apparently do this...
<ul><!-
-><li>stuff</li><!-
-><li>stuff</li><!-
-></ul>
but that seems like a hassle to type?
More "hacks" here (check comments)...

Related

Sometimes small gap appears between child and parent in an underlined menu

I am working on a navigation with a bottom border. Active links are displayed with a second bottom border.
When I press refresh, sometimes there is a small gap between the two borders like on this screenshot. Why?
When I resize the window the gap is gone.
I have this issue in Chrome 97 on a mac. But I'm not sure if it doesn't occur in other browsers as well.
Does somebody can point me in the right direction?
* {
box-sizing: border-box;
}
a {
text-decoration: none;
}
li,
ul {
list-style-type: none;
padding: 0;
}
li,
ul {
margin: 0;
}
img{
width:100%;
height: auto;
}
.projektmenu-wrap {
border-bottom: 1px solid #000;
list-style: none;
overflow-x: auto;
padding-left: 2rem;
padding-right: 1rem;
padding-top: 0.75rem;
position: -webkit-sticky;
position: sticky;
top: 0;
white-space: nowrap;
z-index: 98;
}
.projektmenu {
align-items: flex-start;
display: flex;
flex-grow: 1;
flex-wrap: nowrap;
justify-content: flex-start;
white-space: nowrap;
width: 100%;
}
.projektmenu__item {
border-bottom: 3px solid transparent;
margin-right: 2em;
margin-top: 0;
}
.projektmenu__item--selected,
.projektmenu__item:hover {
border-bottom: 3px solid #000;
}
<img src="https://picsum.photos/200/300">
<nav class="projektmenu-wrap">
<div class="scrollable-menu">
<ul class="projektmenu">
<li class="projektmenu__item projektmenu__item--selected">
Lorem Ipsum
</li>
<li class="projektmenu__item ">
Dolor est
</li>
</ul>
</div>
</nav>
After some debugging i think the problem could be position: sticky.
Edit:
After some more debugging I realize that I forget something here, before the menu element there is an image with 100%width and auto height, maybe that could be the problem. I added it to the code above
After trying a lot of things, I have added some vw to the border, for the moment that is looking good. But it is hard to tell if it is the solution:
border-bottom: calc(3px + 0.1vw) solid #000
In general, having such a double border solution is always prone to rendering issues, because at different zoom levels the browser renders the borders a bit different.
See also: Google Chrome - Rendering differences when zooming in/out
A solution / workaround for your example would be to define the border width in relative units (e.g. rem), which isn't linked to pixels but based on font sizes (e.g. the root element).
Try this out:
.projektmenu__item--selected,
.projektmenu__item:hover {
border-bottom: 0.2rem solid #000; // alternative unit: em
// or: using a mixture of fixed and relative units
// border-bottom: calc(0.2rem + 3px) solid #000;
}
Also, although not required in this case, I would recommend you to use pseudo-elements like ::after for such markers in lists, it's basically what those are intended for.

Why do 2 elements of 50% not fit in the same row

Question
If both <li> are 50%, why won't they be side by side?
Code
Demo on jsfiddle.
HTML
<ul>
<li>left</li>
<li>right</li>
</ul>
CSS
ul {
padding: 0;
}
li {
list-style: none;
display: inline-block;
width: 50%;
font-size: 12px;
color: gray;
background-color: rgb(216, 216, 216);
padding: 3px;
text-align: center;
border: 1px solid rgba(179, 179, 179, 0.83);
}
Because by default, margin padding and border are counted outside the element and not inside. So padding of 3px + border of 1px will be added to 50% on all the sides.
Also am not sure whether you are resetting browser default margin and padding of the elements, if you aren't than do it. You can either use universal selector which some oppose as it hits performance by few bits, like
* {
margin: 0;
padding: 0;
}
Or if you want to reset in a lenient way than you can use CSS Reset Stylesheet
So inorder to count the padding and border inside, you will have to use box-sizing property with a value of border-box.
Also you are using inline-block which will leave a margin / whitespace whatever you call of merely 4px so make all the li in one line. OR what you can do is, you can float your elements to the left, so that you don't have to make all the li in one line in your source document.
Demo
Just make sure you clear your floating elements if you are going with the float solution.
First problem: With the display:inline-block property, there is a white-space betwen elements. This make the li elments 100% + white-space is more than 100% so they can't fit on same line better use float:left for your issue
Second problem: You give a 3px border which makes your li elements more than 50% wide you could use the property box-sizing:border-box; which would make your borders inside the li elements
CSS:
ul {
padding: 0;
padding:0;
}
li {
list-style: none;
float:left;
width: 50%;
font-size: 12px;
color: gray;
margin:0;
padding:0;
background-color: rgb(216, 216, 216);
text-align: center;
border: 1px solid rgba(179, 179, 179, 0.83);
box-sizing:border-box;
}
JSFIDDLE
It is because the <li> elements have a padding. You have to remove the padding or decrease the width.
For example,
width: 48%;
padding: 2%
It's because of the padding. Wash element is 50% + 3px wide.
As others have mentioned display: inline-block; adds space between blocks.
ul {
padding: 0;
font-size: 0; /*This is a hack for inline-block to not add space between blocks*/
}
The above code is a hack to make the "space" between <li> elements become zero. Source: CSS Tricks - https://css-tricks.com/fighting-the-space-between-inline-block-elements/
Demo
Looks like it is a simple problem
looks like you want a padding of
3px per <li> you have 2 then you need 6px extras
and you have broder of 1px ( you have 2 borders per li) that means that you have 4px extras.
That means that you need 10 px extras
You just need to change this line
width: calc(50% - 10px);
Example
http://jsfiddle.net/L5DbR/14/

Problems with rounded horizontal CSS navigation menu

I'm designing a navigation menu for a website.
The menu must have rounded corners, I've done this using 'border-radius'.
I've set the width as 800px as that's the rough width the menu needs to be, if I remove the width or put width: auto the width goes to 100%.
There is a gap before the first button and after the last button in my navigation menu and what I need to get rid of this gap without losing the curved edges.
How can I make the first and last buttons maintain rounded outside corners and remove the gap between each side of the navigation.
CSS:
/* CSS MENU */
#menu {
/* DISPLAY SETTINGS */
text-align: center;
height: 40px;
width: 800px;
margin: 0;
padding: 0;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
/* APPEARANCE SETTINGS */
border-top: 2px solid #356AA0;
border-left: 2px solid #356AA0;
border-bottom: 2px solid #204061;
border-right: 2px solid #204061;
background: #628ddb;
/* FONT SETTINGS */
color: #15387a;
font-family: Arial, sans-serif;
font-weight: bold;
text-transform: uppercase;
font-size: 12px;
}
/* LIST SETTINGS */
#menu li {
display: inline-block;
}
/* HYPERLINK SETTINGS */
#menu li a {
text-decoration: none;
display: block;
padding: 0 15px;
line-height: 40px;
}
/* HOVER AND ACTIVE BUTTON SETTINGS */
#menu li a:hover, #menu li.active a {
color: #15387a; background: #3D7BBB; border-bottom: 2px solid #204061
}
HTML
<ul id="menu">
<li class="active end">Home</li>
<li>Our Services</li>
<li>Testimonials</li>
<li>Get A Quote</li>
<li>Drive For Us</li>
<li>Terms & Conditions</li>
<li class="end">Contact Us</li>
</ul>
So there are several things that need to happen in order to maintain your design:
1.) The UL tag needs to have display: table
2.) Like what #Netsurfer You'll need to set the LI to have display: table-cell so that the list items flush to the edges
3.) Now that UL has rounded corners, any child elements with squared corners will stick out. You can either:
a.) Resolve this by applying overflow: hidden to both the UL and LI or
b.) Apply the rounded corners to the LI and A tags.
4.) Your :hover & active state applies a bottom border -- the table-cell will cause this to look strange. It might be better to remove it altogether.
You can check out the code here: http://jsfiddle.net/vuAVV/
Remove text-align: center; from #menu.
You might also want to include padding-left: 10px; to make sure when the first link is highlighted it does not overlap with the rounded corner of the menu.
See this working jsFiddle.
Change the display setting for the LIs to display: table-cell.
By doing so you are also not "trapped" by the white-space issues when using display: inline-block.
See jsFiddle
PS: Forgot the rounded corners ..., now also included. ;-)

Why are statically positioned children of a fixed position element gaining width?

UPDATE:
This seem to only be an issue with ul/li if i replace the ul with a div and remove the li and apply the relevant style to the a's instead its fine. ID' still liek to know why the ul/li structure presents a problem since margin/padding have been reset explicitly.
Im having soem trouble with the children of a fixed position element in IE7. They seem to be gaining width/margin/padding from somewhere but I cant discern where or how to fix it.
You can take a look at it in jsFiddle here. Ive added the bg colors just for debugging. The image/li tags should be flush with they yellow, and are in IE8 as well as mozilla and webkit. But in IE7 there is an extra ~20px of space to the left pushing them over, as if the li, a, or img tags had a margin. However, if i look through the properties in IEDevToolbar there is no margin or padding being applied. Futhermore, this happens even if i assign widths to everything and zero out margin/padding directly on each element with IEDevToolbar.
I'm totally lost on this one.
Below is the relevent code... There is a XHTML 1.0 Transitional doctype on the layout in question:
<style type="text/css">
.social-widgets {
position: fixed;
top: 125px;
left: 0px;
background: #f00;
width: 34px;
}
.social-widgets-content {
list-style: none inside none;
margin: 0;
padding: 0;
text-align: left;
background: #ff0;
}
.social-widgets-content li {
margin: 10px 0 !important;
padding:0;
width: 34px;
background: #0f0;
}
.social-widgets-content img {
display:block;
border-top: 2px solid #e9e8e8;
border-bottom: 2px solid #e9e8e8;
border-right: 2px solid #e9e8e8;
padding: 0px; margin:0px;
background: #00f;
}
</style>
<div class="social-widgets">
<ul class="social-widgets-content">
<li><img src="/images/button/button.facebook.png"></li>
<li><img src="/images/button/button.twitter.png"></li>
<li><img src="/images/button/button.feedback.png"></li>
</ul>
</div> <!-- /.social-widgets -->
This has nothing to do with position:fixed;. It was an issue with the list styling. When using list-style: none inside none; IE7 still adds the spacing for the list-marker despite the marker being set to none. The solution was to set list-style-type: none; instead of using the shorthand.

CSS Creating a menu-div-box?

I am trying to create some simple menu links. I tried something like this:
div.menulinkboxaround
{
height: 25px;
}
a.menulinkbox
{
font-family: Verdana, Helvetica;
padding-left: 50px;
padding-left: 50px;
padding-bottom: 5px;
padding-top: 5px;
background-color: Green;
}
a.menulinkbox:hover
{
background-color: Red;
}
a.menulinkbox:visited
{
background-color: Yellow;
}
<div class="menulinkboxaround">Link 1</div>
<div class="menulinkboxaround">Link 2</div>
<div class="menulinkboxaround">Link 3</div>
<div class="menulinkboxaround">Link 4</div>
What i am trying to accomplish is to create menu elements that has a touch of style to em, so each link should be inside a div box with a padding 50 px on each side.
When i run this, they get clumped up on top of each other. I don't want to specify a width since the text inside the menu box should determine the size of it automatically.
Ex. (50px+text size+50px)
50px space (just green area) | Sample Text | 50px space (just green area)
Maybe this will help (since divs are block displayed elements by default):
div.menulinkboxaround { height: 25px; float: left; }
Try adding this:
a.menulinkbox
{
display: block;
}
Depending on whether you want this menu vertical or horizontal you may also want to add float: left; to div.menulinkboxaround.
As the previous answers suggest, you could put float:left on the menulinkboxaround.
It is difficult to tell from your description the desired effect, I am assuming you want the menu to be horizontal with 50px either side of the links.
With the code you currently have, the hover state only stretches in one direction, also as you are only specifying :hover it is not really as keyboard friendly as it would be if you specified :focus as well.
Also because you are setting the height in px as you increase the font size the text becomes clipped at the bottom. Not specifying the pseudo selectors on the link may also cause you later problems in Internet Explorer.
You could also tidy up the code a little to reduce the unnecessary classes and improve the semantics of the menu.
For example:
<style type="text/css">
ul.menu {
/* removing the browser defaults for margin padding and bullets */
margin: 0;
padding: 0;
list-style-type: none;
/* Now you have a sensible parent it is a good idea to put the font
family here, I have also added a fallback of sans-serif in the rare
case Helvetica and Verdana are not available on the users computer,
it might be best to set this on the body if you are using this font
site-wide
*/
font-family: Verdana, Helvetica, sans-serif;
/* To create symetry I am adding 25px to the right and left of the menu,
this will stay green even if the items inside are not
*/
padding: 0 25px;
background-color: green;
/* increacing the lineheight so the background color of the links does
not overflow the green of the menu behind it, for a simple menu like
this it is fine, a more complex or longer links that need to wrap I
suggest changing the method of implementation from display inline to
floating which is a bit more complex
*/
line-height:1.95;
}
/* because all the list items are inside this parent list you can use
the descendant selector to target them rather than adding a separate
class, you are saying all list items inside the unordered list that
has a class of menu
*/
ul.menu li {
/* telling the list items to behave like inline elements so they are
naturally on one line also removint the browser default margin and
padding
*/
display: inline;
margin: 0;
padding: 0;
}
ul.menu a:link,
ul.menu a:visited,
ul.menu a:hover,
ul.menu a:focus,
ul.menu a:active {
/* you can combine all your padding rules together in the order
Top Right Bottom Left, I remember this like it kinda spells TRouBLe :)
*/
padding: 5px 25px 5px 25px;
background-color: green;
/* setting the color to white because the default link color of blue
is not that visible against green
*/
color: white;
}
/* adding the :focus selector to make this more keyboard accessible */
ul.menu a:hover,
ul.menu a:focus {
background-color: red;
color: black;
}
ul.menu a:visited {
background-color: yellow;
color: black;
}
</style>
</pre>
<ul class="menu">
<!-- Putting these all on one line because we are making
them display:inline so the spaces get counted and there will
be a gap otherwise -->
<li>Link 1</li><li>Link 2</li><li>Link 3</li>
</ul>
I have tested this in recent versions of FF, Opera and Safari, and IE6 IE7 and IE8
<style type="text/css">
ul.menu {
margin: 0;
padding: 0;
list-style-type: none;
font-family: Verdana, Helvetica, sans-serif;
padding: 0 25px;
background-color: green;
/* overflow hidden clears the internal floated links and zoom 1
kicks IE into doing the same, I suggest you move the zoom: 1
into an IE stylesheet using conditional comments
*/
overflow: hidden;
zoom: 1;
}
ul.menu li {
display: inline;
margin: 0;
padding: 0;
}
ul.menu a:link,
ul.menu a:visited,
ul.menu a:hover,
ul.menu a:focus,
ul.menu a:active {
padding: 5px 25px 5px 25px;
background-color: green;
color: white;
/* setting the links to float left and giving them display block as
well explicitly, this is so that the vertical padding of 5px gets
applied, inline elements can only have horizontal margin and padding,
and since we are floating them they now take up 0 vertical height in
the document which is why we needed to clear the float on the
surrounding menu
*/
display: block;
float: left;
}
ul.menu a:hover,
ul.menu a:focus {
background-color: red;
color: black;
}
ul.menu a:visited {
background-color: yellow;
color: black;
}
</style>
<ul class="menu">
<li>Link 1</li><li>Link 2</li><li>Link 3</li>
</ul>
This second method is much more reliable, deals with wrapping links nicer and is generally a better solution but a bit harder to explain.
If you didn't want the menu to fill the full width of the screen just as long as the text takes up, regardless of which method you are using above, I suggest you put float: left and clear: both on the ul.menu which should shrink to the width it needs to take up
I hope this helps
sample code below (credit to other answers)
div.menulinkboxaround
{
height: 25px;
float: left;
}
a.menulinkbox
{
font-family: Verdana, Helvetica;
padding-left: 50px;
padding-right: 50px;
padding-bottom: 5px;
padding-top: 5px;
background-color: Green;
}
a.menulinkbox:hover
{
background-color: Red;
}
a.menulinkbox:visited
{
background-color: Yellow;
}
<div class="menulinkboxaround">Link 1</div>
<div class="menulinkboxaround">Link 2</div>
<div class="menulinkboxaround">Link 3</div>
<div class="menulinkboxaround">Link 4</div>

Resources