how to add if-else logic in CSS file? - css

I have the following .css.scss file
.book-list li {
display: inline-block;
margin-bottom:5px;
margin-left:5px;
width:175px;
color: #c4c5c7;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
&:first-child {
width: 535px;
margin: 0 0 5px 5px;
padding: 0 0 5px 5px;
border-bottom: 1px dotted #6b6a6c;
}
}
Now I wish to style a very similar list called .todo-list.
The only differences are:
.todo-list li element has width of 125px instead of 175px.
.todo-list li element does NOT have the special styling for the first-child
I would style my html in the following way:
<ul class="book-list">
<li>...
</li>
<li>...
</li>
</ul>
or
<ul class="todo-list">
<li>...
</li>
<li>...
</li>
</ul>
How can I achieve the above without duplicating the whole block?
(Essentially I'm looking for ways to add if-else condition in the css file.)
Thank you

You could use Mixins to re-use common code.
#mixin common-code {
display: inline-block;
margin-bottom:5px;
margin-left:5px;
color: #c4c5c7;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
}
.book-list li {
width:175px;
#include common-code;
&:first-child {
width: 535px;
margin: 0 0 5px 5px;
padding: 0 0 5px 5px;
border-bottom: 1px dotted #6b6a6c;
}
}
.todo-list li {
width:125px;
#include common-code;
}
As far as I know, there are no If-Else clauses in SASS or pure CSS.

You can achieve this using the CSS attribute selector and the $= operator (which means 'ends with'):
[class$="list"] li {
display: inline-block;
margin-bottom:5px;
margin-left:5px;
color: #c4c5c7;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
}
.book-list li:first-child {
width: 535px;
margin: 0 0 5px 5px;
padding: 0 0 5px 5px;
border-bottom: 1px dotted #6b6a6c;
}
.book-list li {
width:175px;
}
.todo-list li {
width: 125px
}

I do not believe that there is a way to add logic css files. You could do it using javascript, however a pure css solution is possible.
.book-list li, .todo-list li {
display: inline-block;
margin-bottom:5px;
margin-left:5px;
color: #c4c5c7;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
}
.book-list li {
width: 175px;
&:first-child {
width: 535px;
margin: 0 0 5px 5px;
padding: 0 0 5px 5px;
border-bottom: 1px dotted #6b6a6c;
}
}
.todo-list li {
width: 125px;
}
Note: In css the last property given to an element will be the one used. This means that you can style .todo-list li to overwrite the previous assignment of width and &:first child should you wish to do so.

You can use inheritance in Sass like:
.list {
display: inline-block;
margin-bottom:5px;
margin-left:5px;
color: #c4c5c7;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
}
.book-list {
#extend .list
li {
width: 125px;
&:first-child {
width: 535px;
margin: 0 0 5px 5px;
padding: 0 0 5px 5px;
border-bottom: 1px dotted #6b6a6c;
}
}
}
.todo-list {
#extend .list;
li {
width: 175px;
}
}

Related

How to do Capsule shape div with delimiter like below attachment?

Is it possible to make a capsule shape using border-radius without a set width or height and include between delimeter?
Yes, You can try this way
.container {
width: 90%;
padding:20px;
margin: 10px auto;
background:#000;
}
ul {
list-style: none;
padding: 7px;
background: #333;
border-radius: 12px;
display: inline-flex;
}
li {
padding: 0 10px;
border-right: 1px solid #999;
line-height: 1;
color: #999;
font-size: 12px;
display: inline-block;
vertical-align: middle;
}
li:last-child {
border-right: none;
}
<div class="container">
<ul>
<li>abc</li>
<li>def</li>
<li>ghi</li>
</ul>
</div>

How to selectively style certain li and ul elements?

I have designed a navigation bar for my website using the following CSS:
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #ff9933;
font-size:90%;
}
li {
float: left;
border-right: 1px solid #ffb366;
}
li a {
display: block;
color: white;
text-align: center;
padding-left: 10px;
padding-right: 10px;
padding-top: 3px;
padding-bottom: 3px;
text-decoration: none;
}
li a:hover {
background-color: #e67300;
}
</style>
This is a version of the horizontal navigation bar example documented at w3schools.com: http://www.w3schools.com/css/tryit.asp?filename=trycss_navbar_horizontal_black
My problem is that it affects other <li> and <ul> elements used in my website as well, not just the navbar. How do I ensure the navbar ones stay separate from other <li> and <ul> elements, using solely CSS? I've started learning CSS quite recently, so I'm certain I'm missing something pretty fundamental here.
Use may want to use css classes
http://www.w3schools.com/cssref/sel_class.asp
ul.mylist {
.....
}
ul.mylist li {
.....
}
ul.mylist li a {
.....
}
ul.mylist li a:hover {
.....
}
Also make sure to add the class to the html
<ul class='mylist'>
<li>......
Similar to man's answer, enclose the ul elements in a div and set the class of the div to navbar, for example. Then change your CSS code to this:
ul.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #ff9933;
font-size:90%;
}
ul.navbar li {
float: left;
border-right: 1px solid #ffb366;
}
ul.navbar li a {
display: block;
color: white;
text-align: center;
padding-left: 10px;
padding-right: 10px;
padding-top: 3px;
padding-bottom: 3px;
text-decoration: none;
}
ul.navbar li a:hover {
background-color: #e67300;
}
I'll modify your code to demonstrate how you can use classes to specify which ul tag you wish to style
<style>
ul.myNav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #ff9933;
font-size:90%;
}
ul.myNav > li {
float: left;
border-right: 1px solid #ffb366;
}
ul.myNav > li a {
display: block;
color: white;
text-align: center;
padding-left: 10px;
padding-right: 10px;
padding-top: 3px;
padding-bottom: 3px;
text-decoration: none;
}
ul.myNav > li a:hover {
background-color: #e67300;
}
</style>
And all you have to do is add the class to your preferred ul element in your html.
<ul class="myNav"> .... </ul>
li:not(:first-child){
code...
}
li:not(:last-child){
code...

How do I name a navbar structure with BEM?

I have the following SASS code.
How would I do the exact same but using the BEM methodolgy?
nav.primary ul {
border-top: 2px solid $darkgreen;
display:block;
margin:10px 0 0 0;
padding: 25px 0 0 0;
li {
background-color:#004f5a;
display: inline-block;
float: none;
margin-left:0;
padding: 5px 0;
text-align: center;
width: 100%;
a {
color:#fff;
letter-spacing: 0.5px;
&:hover {
color:$darkgreen;
}
}
}
}
For example, make a block .primary-nav with three elements .primary-nav__ul, .primary-nav__li, .primary-nav__a.
.primary-nav {
&__ul {
border-top: 2px solid $darkgreen;
display: block;
margin:10px 0 0 0;
padding: 25px 0 0 0;
}
&__li {
background-color: #004f5a;
display: inline-block;
float: none;
margin-left: 0;
padding: 5px 0;
text-align: center;
width: 100%;
}
&__a {
color:#fff;
letter-spacing: 0.5px;
&:hover {
color: $darkgreen;
}
}
}
BEM is a naming convention for classes (distinguishing identifiers and modifiers); the concept is to create ,very strict, single class selector.
The way this is achieved is through SCSS ampersand selector;
In your scenario , since you're markup doesn't combine classes to customize elements, it doesn't make much sense.

CSS Dropdown Menu Shift

I'm curious why my 'homepage' link keeps shifting over. I've made a fiddle of the problem:
jsfiddle.net/nbf8fwdv/
Thanks for the help. I'm still getting the hang of semantics and proper usage in CSS, so if you see any glaring problems with my code that only a beginner would make, please let me know. Thanks for the help in advance.
In order to prevent the homepage from shifting on hover, you'll want to remove this property:
max-width: 75px;
from this class:
nav ul>li:hover {
background-color: rgba(253,235,193,.6);
max-width: 75px;
text-align:center;
}
Because the homepage list item is naturally greater than 75px, the max-width property is actually reducing it's width on hover.
You can write a class like bootstrap
body {
background-color: white;
font-family: PT Sans, sans-serif;
text-shadow: 1px 1px rgba(166,166,166,.2);
}
header {
background: white;
width: 100%
padding: 40px 0;
color: black;
text-align: center;
}
a {
text-decoration: none;
color: black;
font-size: 1.0em;
letter-spacing: 2px;
}
nav {
box-shadow: 1px 1px 10px rgba(166,166,166,.2);
}
nav ul {
background-color: rgba(253,235,193,.3);
overflow: visible;
color: white;
padding: 0;
text-align: center;
margin: 0;
position: relative;
}
nav ul li {
display: inline-block;
padding: 20px 40px;
position: relative;
}
nav ul ul {
display: none;
}
nav ul>li:hover {
background-color: rgba(253,235,193,.6);
text-align:center;
}
nav ul li:hover ul{
display: block;
margin-top: 20px;
}
nav ul li:hover li{
margin-left: -40px;
margin-top:-15px;
text-align: center;
float: left;
clear: left;
}
.portfolio_menu{position:absolute;top:100%;left:0;z-index:1000;display:none;float:left;min-width:160px;padding:5px 0;margin:2px 0 0;font-size:14px;text-align:left;list-style:none;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #ccc;border:1px solid rgba(0,0,0,.15);border-radius:4px;-webkit-box-shadow:0 6px 12px rgba(0,0,0,.175);box-shadow:0 6px 12px rgba(0,0,0,.175)}
To actually save your other links by shifting over when hover over the "portfolio", here is my 2 cents. http://jsfiddle.net/nbf8fwdv/5/
nav ul ul {
display: none;
position:absolute;
left:0;
}

Vertically Align <a> tag inside of a list

I am trying to vertically align an anchor tag within a list item, i have managed to horizontally align it so far. See code below and check jsfiddle for demo.
HTML
<div id="header-top">
<div id="header-top-middle">
<ul>
<li><a data-icon="" a href="#">1</a></li>
<li><a data-icon="" a href="#">222222222</a></li>
<li><a data-icon="" a href="#">3</a></li>
<li><a data-icon="" a href="#">4</a></li>
</ul>
</div>
CSS
#header-top {
height:30px;
background-color:#303030;
width: auto;
border-bottom: 2px solid #DDDDDD;
}
#header-top-middle {
width:1024px;
margin: 0 auto;
height:30px;
}
#header-top-middle ul {
list-style: none;
margin: 0;
padding: 0;
}
#header-top-middle ul li {
border-right: 1px solid #DDDDDD;
display: inline;
float: left;
overflow-x: hidden;
overflow-y: hidden;
}
#header-top-middle ul li a {
color: #FFFFFF;
display: block;
font-size: 15px;
height: 30px;
text-align: center;
width: 30px;
text-decoration:none;
}
See jsfiddle
You can use display: table for your list:
#header-top-middle ul {
list-style: none;
margin: 0;
padding: 0;
display: table;
}
as well as display:table-cell; and vertical-align: middle; for your link inside list item:
#header-top-middle ul li a {
color: #FFFFFF;
display: block;
font-size: 15px;
height: 30px;
text-align: center;
width: 30px;
text-decoration:none;
display:table-cell;
vertical-align: middle;
}
Updated Fiddle
Add line-height:30px to a:
http://jsfiddle.net/hRadK/1/
#header-top-middle ul li a {
color: #FFFFFF;
display: block;
font-size: 15px;
height: 30px;
text-align: center;
width: 30px;
text-decoration:none;
line-height:30px;
}
This might help: http://css-tricks.com/centering-in-the-unknown/
In the link, Chris deals with centering an element whose width and height are unknown.
#header-top-middle ul li a {
color: #FFFFFF;
//display: block;
font-size: 15px;
height: 30px;
text-align: center;
width: 30px;
text-decoration:none;
display:table-cell;
vertical-align:middle;
}
http://jsfiddle.net/hRadK/8/
Try the approach outlined in this answer of wrapping your hyperlinks in a div and applying the vertical alignment on the div. The only other amendment you will need to make is then to remove the block display of hyperlinks you currently have defined.
#header-top-middle ul li a {
color: #FFFFFF;
/* display: block; - remove this rule */
font-size: 15px;
height: 30px;
text-align: center;
width: 30px;
text-decoration:none;
}
Updated fiddle

Resources