Am I messing with specificity ? What's going on? - html

I will short and clear. I have this html code snippet.
<nav>
<span class="heading">CodingHero</span>
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
</ul>
</nav>
Now I have this CSS part
nav ul li a {
font-family:'Poppins', sans-serif;
font-size:20px;
color:white;
opacity:0.8;
margin:0px 20px;
}
This is also okay. But I found this weird behavior while dealing inside media queries.
nav ul a {
font-size:44px;
background-color:yellow;
margin:0px;
}
My background color works yellow fine.
But my font-size and margin doesn't work.
As soon as I provide specificity as I originally used in CSS part,
nav ul li a {
font-size:44px;
background-color:yellow;
margin:0px;
}
This works, my font-size and margin also comes into effect.
Can someone explain why I have to use the original selector that I first used to get all properties to act?
Why my font-size and margin were not applied when background color in same part is applied.
What's going on here? Any resources to clear my head.
Any response is appreciated.
Thanks !

background-color:yellow; was applied because the other selector with higher specificity (nav ul li a) doesn't contain a background-color property, so nothing overrides it.
If you were to add one like
nav ul li a {
font-family:'Poppins', sans-serif;
font-size:20px;
color:white;
opacity:0.8;
margin:0px 20px;
background-color: red; /* added */
}
then it'll override background-color:yellow, just like it override margin and font-size

Related

Full browser width list, but text has max-width

I have a full browser width list with a background color (which changes color on hover). However I want the li text to be text-align:left, have a max-width and the left and right margins to be equal – but the background color to still be full browser width. How do I do this?
I have made a JSfiddle here.
As soon as I put a max-width on the li, the background color will obviously shrink to the max-width. Is there a way to just target the text within the list?
<div class="case_study_links">
<ul>
<li>Abbey Meadow Flowers<br>Helping to grow a sustainable florists</li>
<li>Collins Environmental<br>Differentiating ecologists from competitors</li>
<li>University of Oxford<br>Branding for research project on young migrants</li>
<li>Small Woods<br>New brand brings credibility to organisation</li>
<li>Good Energy<br>Rebranding helps double customer numbers</li>
</ul>
</div>
.case_study_links li {
list-style: none;
font-size:1.8rem;
text-align:left;
border-top:1px solid white;
}
.case_study_links a:link { color:white; display:block; padding:4.8rem 0; background-color:rgb(149,199,201);}
.case_study_links a:visited { color:white; display:block; padding:4.8rem 0; background-color:rgb(149,199,201);}
.case_study_links a:hover { color:white; display:block; padding:4.8rem 0; background-color:rgb(134,179,181);}
.case_study_links a:active { color:white; display:block; padding:4.8rem 0; background-color:rgb(134,179,181);}
wrap Your text in a <span class="myTexts"> and add css properties to it:
.myTexts
{
max-width:100px; // or anything you want
margin:auto
}
U have your CSS on wrong levels:
Define background-color on the ul (maybe width: 100%; too, didn't test)
Define borders and width: 100%; on the li
Define max-width: ; on the a, or the elements within a
As suggested, you could wrap a part of the text in a span element.
I would refrain from using "br", you could do this:
<li><p>Abbey Meadow Flowers</p><p>Helping to grow a sustainable florists</p></li>
Change the P elements accordingly for semantic HTML to H1,H2,H3,span,p, etc.
Note that span is an inline element, and will not automatically take up full width. Use display: block; in your CSS to fix this

Navigation items padding: Firefox vs. Chrome&IE11

I got strange problem with different rendering of one simple nav/menu. Here is code:
HTML:
<nav id="navigation">
<ul>
<li class="active">HOME<br><span>What we do</span></li>
<li>ABOUT<br> <span>Our expertise</span></li>
<li>SERVICES<br><span>Our products</span></li>
<li>WHY CHOOSE US<br><span>Our promise</span></li>
<li>NEWS<br><span>Updates</span></li>
<li>OUR CHOSEN CHARITY<br><span>Great Ormond Street</span> </li>
<li>CONTACT US<br><span>Get in touch</span> </li>
</ul>
</nav>
Relevant CSS:
#navigation {
width:100%;
clear:both;
background-color:#a9218e;
height:72px;
}
#navigation ul {
width:894px;
height:72px;
list-style-type:none;
margin:0 auto;
padding:0;
box-sizing:border-box;
}
#navigation ul li {
display:block;
float:left;
text-align:center;
padding:10px 22px 0 22px;
margin:0;
height:72px;
box-sizing:border-box;
}
#navigation ul li:last-child {
padding:10px 20px 0 19px;
}
#navigation ul li a {
color:#fff;
text-decoration:none;
width:100%;
height:72px;
font-size:15px;
font-weight:400;
}
#navigation ul li.active {
background-color:#00837e;
}
Test link: http://bybyweb.com/hygeineco/
Well... In Firefox, menu is perfect, it fits container's size (894px). However, in Chrome and IE11 (and i guess lower IE versions), one item is pushed down. So, i guess that there is no space for that item (contact) because of paddings. I've tried to resize container, or to reduce padding, and rendering difference is ~2px, between Chrome and Firefox... With container width of 896px - all is working fine in Chrome, in IE11, anyway, container width should be at least 903px?! What causes these strange differences? I guess that chosen google font rendering is different in every browser. How to solve this issue?
Add This Code Where Ever You Used box-sizing:border-box; :
box-sizing:border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-ms-box-sizing:border-box;
-o-box-sizing:border-box;
-webkit- is for Safari And Chrome
-moz- is for Mozila (but in your case , the browser could understand it without -moz- )
-o- for Opera and -ms- for IE ;
Almost solved it by using display:table for ul, and display:table-cell for li elements... However, now i just have to remove margin/padding at the bottom.... :)
EDIT: background image position was in question... so, now is perfect. Good old tables - still the best solution for some things. :)
EDIT2: I would remove this question, but i am sure it will help to someone who have similar situation. Conclusion -> when you have menu with different widths of/for every menu item, BUT if you want to place that menu inside fixed width container, setting of right/left padding to li elements (menu items) is obviously not the best solution. Display: table, and display:table-cell is the best (and maybe only?) possible solution.

How to deal with default padding and margins

I am fairly comfortable with html5/css3 now, so I am trying to make a site using same and make it responsive.
So far things are going smoothly except for these two problems:
the use of em i dont understand the calculations at all, especially why i have to put this font: .81em/150% i am following a guide from a tutorial online.
i am having some imaginary padding on my div, you can see it here http://jsfiddle.net/NhZ2A/
e.g. I have on the body:
body{padding:0px; margin:0px;}
Then I have a div with an image like this:
<div id="slider">
<img src="images/slider.jpg"/>
</div>
Then in my css I have:
#slider{
width:100%;
height:auto;
}
#slider img{
width:60%;
height:auto;
}
With the above css I still have padding on the slider div below or maybe it's a margin on the image below.
I don't understand why and its killing me.
For the second issue :
The space is not padding, it is created because the <img> tag is an inline element and therefore has a whitespace use display:block; on the <img> tag to remove it.
Use css resets , To get consistent cross-browser experience,it should be included,any one among these.
Eric Meyer’s Reset CSS
HTML5 Doctor CSS Reset
Yahoo! (YUI 3) Reset CSS
Normalize.css
Get it from here --> http://www.cssreset.com/
Yes, CSS reset is important to set default initial value for each element.
reset.css Source - Reset5
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,audio,canvas,details,figcaption,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,summary,time,video
{
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
margin:0;
padding:0
}
body
{
line-height:1
}
article,aside,dialog,figure,footer,header,hgroup,nav,section,blockquote
{
display:block
}
nav ul
{
list-style:none
}
ol
{
list-style:decimal
}
ul
{
list-style:disc
}
ul ul
{
list-style:circle
}
blockquote,q
{
quotes:none
}
blockquote:before,blockquote:after,q:before,q:after
{
content:none
}
ins
{
text-decoration:underline
}
del
{
text-decoration:line-through
}
mark
{
background:none
}
abbr[title],dfn[title]
{
border-bottom:1px dotted #000;
cursor:help
}
table
{
border-collapse:collapse;
border-spacing:0
}
hr
{
display:block;
height:1px;
border:0;
border-top:1px solid #ccc;
margin:1em 0;
padding:0
}
input[type=submit],input[type=button],button
{
margin:0!important;
padding:0!important
}
input,select,a img
{
vertical-align:middle
}
em - Unit measurement values (1em is equal to the current font-size,same as 2em = 2*font-size)
Font Syntax:
font: font-style font-variant font-weight font-size/line-height font-family;
In your question value .81em/150%
.81em/150% - font-size/line-height
Every browser has a default behaviour and configuration
If you want a clean start from all of them, you must set it with a "reset.css" style sheet, to avoid undesirable behaviours and have all homogeneous.
Check this SO answer to get a proper reset CSS stylesheet:
https://stackoverflow.com/questions/167531/best-practice-for-css-reset-style-sheet
The first choice will be
Css Resets
Most Used Css Reset
JUSR USE CSS RESET
* {
margin:0;
padding:0;
box-sizing:border-box;
}

How to create a box with header with css styling?

How can I create a box with a coloured "heading area" that has a shadow on the bottom just like this example on the right sidebar of this website: Box with heading on this website
This website uses images to create this effect. I would like to use pure CSS3 and HTML5 to achieve this.
I have tried looking for this on Google, but I can't find it anywhere. I'm assuming that I am calling it the wrong thing, that is why I cannot find it. Also, I've tried using a table with two cells, but it does not work out right and I cannot create or place the shadow correctly.
I would like my box to be exactly the same as the example except that I would change the colours.
I would really appreciate any help I can get.
My approach is to use borders for a triangular mask, and underneath use a background gradient.
Check out my code on codepen
The updated version. For pseudo elements (:before and :after) you had to make some space for the border (1px on left and right).
Remember that the tricky part is with adjusting the width with this sidebar. First thing to change is the sidebar class, then you should match the borders for the pseudo element :after.
try this code
<!DOCTYPE html>
<html>
<head>
<style>
#nav{
width:950px;
height:50px;
float:left;
position:relative;
background:#F4F4F4;
background:linear-gradient(top,#FFF,#F4F4F4);
background:-moz-linear-gradient(top,#FFF,#F4F4F4);
background:-o-linear-gradient(#FFF,#F4F4F4);
background:-webkit-gradient(linear,left top,left bottom,from(#FFF),to(#F4F4F4));
box-shadow: 0px 1px 7px rgba(0, 0, 0, 0.21);
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#444444')";
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#CCCCCC');
}
#nav ul{
list-style-type:none;
margin:0;
width:910px;
padding:0px;
padding-left:30px;
float:left;
}
#nav ul li{
display:inline;
}
#nav ul li a{
text-decoration:none;
color:#06C;
padding:8px;
padding-left:25px;
padding-right:25px;
font-family: 'Lucida Sans Unicode','Lucida Grande',Helvetica,Arial,sans-serif;
font-size:16px;
text-align:center;
font-weight:bold;
float:left;
margin:5px;
}
#nav ul li a:hover{
color:#FFF;
background:#666;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a >Home</a></li>
<li><a >Profile</a></li>
<li><a >About</a></li>
<li><a>Contact US</a></li>
</ul>
</div>
let me know if it work for you.
If you want to make this effect with CSS3 use box-shadow.
For example, check fiddle:
http://jsfiddle.net/PwerX/
More documentation check this link, it helped me!
http://css-tricks.com/snippets/css/css-box-shadow/
Have fun!

Hover Over a Menu Item Working in Firefox but not in Internet Explorer

Here is the coding in which hover is working inf firefox but not in IE
.menu
{
margin-top:1px;
display:inline-block;
background-color:#FCFAB4;
color:#000000;
height:30px;
width:121px;
padding-top:10px;
font-size:13px;
font-weight:bold;
font-family:Geneva, Arial, Helvetica, sans-serif;
text-align:center;
}
.menu:hover
{
background-color:#990000;
color:#FFFFFF;
border-bottom:#CC0000;
text-decoration:none;
cursor:pointer;
}
Help me
I don't believe the ':hover' pseudo-class is implemented for anything other than 'a' tags in IE. Try another approach (use 'onmouseover' event).
When you say IE it's better to let us know which (IE6/7/8). IE6 does not support :hover on anything but , IE7/8 do.
If you need this to work in IE6 you have a few options.
Refactor your code to only use 's in your menu.
Use a nice script from Dean Edwards to "upgrade" IE6 and IE7 for several issues such as this one. http://www.charlescooke.me.uk/web/lab_notes/ie7_script.html
You can use a bit of CSS and JS to give IE6 a way to recognise the :hover on other elements
/* Nice work around for IE6 issues with hover only being used on , con is that it needs javascript */
* html li {
behavior: expression(
this.onmouseover = new Function("this.className += ' hover'"),
this.onmouseout = new Function("this.className = this.className.replace(' hover','')"),
this.style.behavior = null
);
}
IE6 aside looking at your code I'm not really seeing how it works now. What you need is to have a menu element with what evers in it set to display:none, also you should position in using position:absolute/relative and top: XXpx/left: XXpx. Then on :hover you change to display: block.
Hope this helps,
Denis
Assuming your menu item is a link a user might select from a horizontal "menu" and looks close to this:
<ul class="menu">
<li>stackoverflow</li>
<li>google</li>
</ul>
Try this CSS:
.menu li{
display:inline;
list-style-type:none
}
.menu li a
{
margin-top:1px;
display:inline-block;
background-color:#FCFAB4;
color:#000000;
height:30px;
width:121px;
padding-top:10px;
font-size:13px;
font-weight:bold;
font-family:Geneva, Arial, Helvetica, sans-serif;
text-align:center;
}
.menu li a:hover
{
background-color:#990000;
color:#FFFFFF;
border-bottom:#CC0000;
text-decoration:none;
cursor:pointer;
}
If you are using an unordered list as a menu, the list items are not the "menu", the unordered list is the "menu", so apply the class there. And apply the CSS to the anchors to achieve hover functions.
If your menu is vertical, ignore display:inline on the .menu li styles.
IE7 supports :hover on anchors, but IE8 will support it on li's.