How to avoid repeat the same lines on CSS? - css

I do not want to repeat the same property all the time on my CSS.
In my case, I have three media queries in which I change the property of padding of a specific element and after I need to put it as the beginning:
#media (max-width: 1000px){
ul > a{
padding-top: 20px;
padding-bottom: 20px;
}
}
#media (max-width: 750px){
ul > a{
padding-top: 16px;
padding-bottom: 16px;
}
}
#media (max-width: 500px){
ul > a{
padding-top: 20px;
padding-bottom: 20px;
}
}
As you can see, I have to put the same code on the first and on the third one media query and I would like to reduce the amount of lines of my CSS.
I would like to wrap these lines into a variable or something similar:
ul > a{
padding-top: 20px;
padding-bottom: 20px;
}
and use them in the whole CSS each time I need it.
I found that there is an experimental technology to create variables on CSS but that it has not been stabilized yet and it does not have a full browser support.
Thus, is there a method to use more than one line on CSS without repeating them?
Thanks in advance!

Just define the common rule without a media query and then use media queries to override it as necessary, like so:
ul a{
padding:20px 0;
}
#media (max-width:750px){
ul a{
padding:16px 0;
}
}
Alternatively, add min-width to your media queries and create a new one to define all the common rules in, like so:
#media (max-width:500px),(min-width:751px) and (max-width:1000px){
ul a{
padding:20px 0;
}
}
#media (min-width:501px) and (max-width:750px){
ul a{
padding:16px 0;
}
}

You could use one query with min and max width Fiddle, also only li element can be direct child of ul element (i used color instead of margin form demo)
ul a {
color: blue;
}
#media screen and (min-width: 500px) and (max-width: 750px) {
ul a {
color: red;
}
}
<ul>
<li>Lorem</li>
<li>Lorem</li>
</ul>

Related

Removing a class in a CSS stylesheet

I am trying to make my site mobile accessible. The problem I have is with the nav. I believe it has to do with the class being used on the nav. So what I want to do is remove the class when a mobile device is being used.
I have it in a media query and have changed a number of things to make it look correct on mobile. I read that it is possible to remove a class using
$( "p" ).removeClass( "myClass yourClass" );
I believe mine would be
$( "nav" ).removeClass( "navigation" );
but when put in the stylesheet and put through a css validator, I get
Lexical error at line 104, column 4.
Encountered: "(" (40), after : "$" ( "nav" ).removeClass( "navigation" );
I have no idea how to, or even if I can, fix this. Other pages I have read say this is impossible to do so I am getting conflicting information.
Here is me trying to post code.
This everything in my CSS affecting my nav.
nav ul { list-style-type: none;
margin: 0;
padding: 0; }
header nav a { text-decoration: none;
padding: 10px;
display: block;
background-color: #a8e6a8;
border-bottom: 1px solid #228B22; }
nav a:link { color: #228B22; }
nav a:visited { color: #568b22; }
nav a:hover { color: #869DC7;
background-color: #EAEAEA; }
.navigation { float: right;
width: 200px;
font-weight: bold;
letter-spacing: 0.1em; }
Here is my unfinished media query.
#media only screen and (max-width: 1024px) {
body { margin: 0; padding: 0; background-color: #fff; }
#wrapper { width: auto; min-width: 0; margin: 0; }
h1 { margin: 0; text-align: center; font-size: 2em; line-height: 200%; }
nav { float: none; width: auto; }
nav li { display: inline-block; }
nav a { padding: 1em; border-style: none; font-size: 1.2em; }
nav ul { text-align: center; padding: 0; margin: 0; }
main { margin: 0; padding: 0 1em; font-size: 90%; }
dd { margin-bottom: 1em; }
footer { margin: 0; }
.navigation { float: none;
width: auto;
letter-spacing: 0.1em; };
}
Actually, you could add a class with jQuery but I don't believe this is the best way to achieve what you need.
What I have seen many designers doing is having two navs. One for big screens and another for small screens. It can be very handy, as you can work with different html codes too. For example, you can add FontAwesome icons to your mobile nav, add or remove links, etc. So, your html would look like this:
<nav class="nav-big">
<ul>
<li>Link 1</li>
<li>Link 2</li>
...
</ul>
</nav>
<nav class="nav-small">
<ul>
<li><i class="fa fa-plus"></i>Link 1</li>
<li><i class="fa fa-minus"></i>Link 2</li>
...
</ul>
</nav>
Then, your CSS would look like this:
.nav-big {
...your styles
}
.nav-small {
...your styles
}
#media screen and (min-width: 1024px) {
.nav-small {
display: none;
}
}
#media screen and (max-width: 1023px) {
.nav-big {
display: none;
}
}
You can see something like this working in my Blog and also in another website I developed.
Hope to have helped. If I did, please mark the answer as useful or correct! Thanks!
Welcome on board! Please try to be more concise on your questions :-)
My answer:
The code you're posting is JavaScript. jQuery, to be precise. This certainly doesn't work in CSS.
But the better solution is to use the media query not to remove a class, but to alter it. Example:
.foo { width:200px; }
#media print
{
.foo { width:100px; }
}
EDIT after the CSS was added to the question:
The trailing ; looks like a syntax error to me: Change letter-spacing: 0.1em; }; to letter-spacing: 0.1em; }
Other than that the media query looks good in principle. Try adding background:red to see if it works at all :-)
$("nav").removeClass("navigation"); is JavaScript, not CSS. Conceptually, you can not add/remove a class in CSS - it is only a style description language.
What you can do in CSS, however, is show/hide content for a specific device width ("breakpoint"):
.navigation {
display: block;
/* your style definitions for nav */
}
#media only screen and (max-device-width: 800px) {
.navigation {
display: none;
}
}

Unwanted text size change on scroll on dropdown menu

I have a dropdown menu on which links get bigger when the page is scrolled. This unfortunately then threatens to drop the bottom-most ones off the foot of the page as a result.
The site is http://www.jswhite.co.uk/
Does anyone know how to prevent this from happening?
your javascript code adding class "et-fixed-header" at header tag and in your css you using below code.
#media only screen and (min-width: 981px)
.et-fixed-header #top-menu li a {
font-size: 22px;
}
So after adding et-fixed-header class it changing menu text. change your css.
.et-fixed-header #top-menu li a {
font-size: 16px;
}
here is the problem
#media only screen and (min-width: 981px)
.et-fixed-header #top-menu li a {
font-size: 22px;
}
you used #media .. Try deleting this statement
you can also just paste this css code
.et-fixed-header #top-menu li a {
font-size: 16px !important;
}
set font size important to maintain it while scrolling
#top-menu li li a {
font-size: 16px!important;
line-height: 1.1em;
}
add overflow as scroll
#main-header .nav li ul {
background-color: #f1f1f1;
overflow: scroll;
}

Bootstrap Dropdown Menu too big

I cannot figure out why my bootstrap dropdown menu seems to have an extra line on top and line on the bottom--with nothing in them. Granted, I have made my navbar 150px for all resolutions but that doesn't explain the extra padding on the bottom. This is my first bootstrap project and I have searched high and low for an answer to this. You have to narrow the window to less than 767 pixels for the dropdown option to appear.
This is the page: http://www.ashlandlockandsafe.com/index3.html.
In the CSS, I have tried to address it with the following:
#media screen and (max-width: 767px) {
.navbar-header{
height:150px;
}
.navbar-brand, .navbar-nav > li > a {
line-height: 30px;
height: 45px;
padding: 0px;
margin-top:0px;
margin-left:10px;
}
.well {
font-size:2.5em;
text-align:center;
text:#000;
padding:0;
border:#000 solid 4px;
background-image:url(../images/metal.png);
background-image:no-repeat;
background-size:contain;
background-position:center;
}
.carhelp {
margin-top:15px;
}
h1 {
text-align: center;
font-size: 30px;
}
.phonetxt {
font-size:36px;
text-align:center;
}
But it still has what appears to be an extra row on the top and bottom. Any ideas how to address the dropdown itself?
In website, fixed header has a padding .navbar-fixed-top .nav{60px 0px} causing the problem.
so solution is media queries
#media (max-width: 480px) {
.navbar-fixed-top .nav {
padding: 0px 0px;
}
}
so on devices like mobile, it will over-ride the default .navbar-fixed-top .nav selector property.
More information about media-queries

How to target content through css

I have this html that I cannot edit:
<div id="menu">
Home |
Meet Our Physicians |
Services |
</div>
I have to make it responsive and when when on mobile I want to delete the |
How can I target it? Something like:
#media screen and (max-width:480px){
element{
display:none;
}
}
Try this:
#media screen and (max-width:480px) {
#menu > a{
margin-right: -10px; /* getting the elements closer */
}
#menu {
color: transparent; /* making the open text's color transparent */
}
}
Working Fiddle
The only way to emulate this (CSS cannot target text that is not part of an element, unfortunately), is to target the containing element and then override the styling in the descendant <a> elements:
#media screen and (max-width:480px){
#menu {
font-size: 0;
}
#menu a {
font-size: 16px; /* or whatever */
margin: 0 0.5em; /* or whatever, to restore some spacing between elements */
}
}
Edit:
Maybe something like that:
#media screen and (max-width:480px){
#menu {
text-indent: -9999px;
font-size: 0px;
}
#menu a {
text-indent: 0px;
font-size: 14px;
}
}

Trying to understand #media queries in CSS file

In the process of trying to figure out how to stretch the navbar to fill the entire width of the screen I ran into something posted on here by another user. I am just starting out with bootstrap/css stuff. I was trying to figure out what was going on in this specific css file but couldn't for the life of me. I had a few questions if anyone can answer them. The CSS file contains this code:
#media (min-width: 640px) {
/* 768px */
.navbar {
border-radius: 0px;
/* 4px */
;
}
}
#media (min-width: 640px) {
/* 768px */
.navbar-collapse {
width: auto;
border-top: 0;
box-shadow: none;
}
.navbar-collapse.collapse {
display: block !important;
height: auto !important;
padding-bottom: 0;
overflow: visible !important;
}
.navbar-collapse.in {
overflow-y: visible;
}
.navbar-fixed-top .navbar-collapse, .navbar-static-top .navbar-collapse, .navbar-fixed-bottom .navbar-collapse {
padding-right: 0;
padding-left: 0;
}
}
#media (min-width: 640px) {
/* 768px */
.navbar-nav {
float: left;
margin: 0;
}
.navbar-nav > li {
float: left;
}
.navbar-nav > li > a {
padding-top: 15px;
padding-bottom: 15px;
}
.navbar-nav.navbar-right:last-child {
margin-right: -15px;
}
}
#media (min-width: 640px) {
/* 768px */
.navbar-toggle {
display: none;
}
}
.container > .navbar-header, .container-fluid > .navbar-header, .container > .navbar-collapse, .container-fluid > .navbar-collapse {
margin-left: 0px;
/* -15px */
margin-right: 0px;
/* -15px */
;
}
.container-fluid {
padding-left: 0px;
/* 15px */
padding-right: 0px;
/* 15px */
;
}
.nav > li > a {
padding-left: 5px;
/* 15px */
padding-right: 5px;
/* 15px */
;
}
.navbar {
border: none;
/* 1px solid transparent */
margin-bottom: 0px;
/* 20px */
;
}
.navbar-collapse {
max-height: none;
/* 340px; */
padding-left: 0px;
/* 15px */
padding-right: 0px;
/* 15px */
;
}
.navbar-default .navbar-collapse, .navbar-default .navbar-form {
border-color: #fff;
/* #e7e7e7 */
;
}
.navbar-default .navbar-nav > li > a {
color: #fff;
/* #777 */
;
}
.navbar-default .navbar-nav > li > a:focus, .navbar-default .navbar-nav > li > a:hover {
background-color: #00752c;
/* transparent */
color: #fff;
/* #333 */
;
}
.navbar-default .navbar-nav > .active > a, .navbar-default .navbar-nav > .active > a:focus, .navbar-default .navbar-nav > .active > a:hover {
background-color: #00752c;
/* #e7e7e7 */
color: #fff;
/* #555 */
;
}
.navbar-default .navbar-toggle:focus {
background-color: transparent;
/* #DDD */
;
}
.navbar-default .navbar-toggle:hover {
background-color: #DDD;
}
.navbar-default .navbar-toggle .icon-bar {
background-color: #000;
/* #888 */
;
}
.navbar-nav {
margin: auto;
/* 7.5px -15px */
;
}
.navbar-toggle {
margin-left: 15px;
/* 0px */
margin-right: 0px;
/* 15px */
float: left;
/* right */
;
}
body, html {
height: 100%;
width: 100%;
}
#media (max-width: 639px) {
.navbar-collapse {
border-bottom: 1px solid;
border-top: 1px solid;
}
}
.clear {
clear: both;
}
.navbar {
z-index: 1;
}
.navbar-default .navbar-nav > li > a:focus, .navbar-default .navbar-nav > .active > a:focus {
outline-style: none;
}
.navbar-nav {
background-color: #009b3a;
}
#header nav {
background-color: #009b3a;
color: #fff;
font-size: 13px;
height: 50px;
text-transform: uppercase;
}
#page {
margin-left: auto;
margin-right: auto;
max-width: 620px;
}
My questions are:
1) Why are there 4 #media (min-width: 640px) queries? I tried combining those 4 into one #media query but it broke the code. I don't understand why.
2) What exactly is going on in something like this?
.container > .navbar-header, .container-fluid > .navbar-header, .container > .navbar-collapse, .container-fluid > .navbar-collapse
3) In this CSS file, you have some things referenced multiple times in different
#media (min-width:640px)
queries. For example:
You have
.navbar {
border-radius: 0px; /* 4px */
}
At the start and again in a different #media 640px query
.navbar {
border: none; /* 1px solid transparent */
margin-bottom: 0px; /* 20px */
}
Why is it referenced twice here? Couldn't we just combine it into .navbar class?
Help a noob understand CSS :)
Thanks
It depends where you put your media query. Notice that they appear after the style they are overriding. This is for clarity. Because CSS is evaluated from top to bottom, if the media is above the style - it will not be overridden.
is direct child of parent. Without this css will look anywhere inside parent element
Examples you have shown are not conflicting - Second one is not inside media. If there was a conflict the latter one would override one higher in the file. I would say multiple media here are for readability
Hope that helps. Play with this one if you want to know more about how CSS classes are overridden http://josh.github.io/css-explain/
yes and you can combine all 4 media queries....they are included...because that is sloppy work? basically its a judgement call here: probably each was added at separate times, and they are meant to be minified # some point. or that is in production and its not the best of style sheets. you can add them all into one media query, the border:none is going to override the entire border declaration(s), but border-radius is not included (asfaik, i don't see it on dev.moz in the short border style declaration. if it is and i'm wrong, the same applies, i would think, that everything property under the short form of border is reset to a style of none.
last question, the > is the child combinator selector, which only selects elements that are child descendants of the element(s)/classes/ids/, etc., named prior to the selector. so ul > li will only select list items that are direct descendants of unordered lists, where as ul li will select all list items that fall under an unordered list.
Some of your questions are difficult to answer without hearing form the developers themselves, but here are my best guesses:
1) Why are there 4 #media (min-width: 640px) queries?
The bootstrap css file isn't developed in css, but is the compiled result of many LESS files. These queries and css rules might have originally been split over several files, and this is just how the compiler has decided to squish it all together.
As to why this didn't work when you tried it, that's hard to say without seeing your code, but as TreeTree suggested, you may have accidentally introduced syntax errors (maybe you missed a curly brace somewhere?). But you're right, all things being correct, these rules can be wrapped in a single #media query.
2) What exactly is going on in something like this?
.container > .navbar-header, .container-fluid > .navbar-header, .container > .navbar-collapse, .container-fluid > .navbar-collapse
The greater-than > symbol is a child selector. .container > .navbar-header means apply these rules to .navbar-header only when it is a direct child of .container
3) In this CSS file, you have some things referenced multiple times in different [#media] queries .. Why is it referenced twice here?
Again, this may just be an artifact of the compiled LESS files into a single css file. If you were writing your css by hand, you are correct in thinking that they could be combined into a single rule. Our compilers aren't always that smart, but they probably don't need to be - these types of files usually aren't read by people, and the impact on performance is minimal, if any.
1) You probably introduced some syntax errors when you merged them, especially if there is no indentation at all. Having 4 separate queries could just be a means of organizing code.
2) It's just 4 separate selectors that make use of the > selector which means immediate child/descendant. It will only search the children but not the children's children. More about it here.
.container > .navbar-header,
.container-fluid > .navbar-header,
.container > .navbar-collapse,
.container-fluid > .navbar-collapse
3) The lack of indentation has thwarted you. The first block of CSS is located within a query but the second block is not. I suggest adding some indentation so you can see for yourself.
In response to comment:
Given this HTML:
<div class = "foo">
<div class = "bar">
<div class = "bar"></div>
</div>
<div class = "bar">
<div class = "bar"></div>
</div>
</div>
.foo .bar will match every single .bar whereas .foo > .bar will only match the .bar whose parent is .foo. It will not match the inner .bar because their parent is .bar.
Thanks everyone. I definitely understand CSS a bit more now. I guess I just got unlucky with a case where the formatting made it hard to understand what was going on. That sort of thing certainly doesn't help when you're first starting out.

Resources