change styles when using two css classes together - html

What I am trying to do is create default css styles that when I combine multiple classes together I want to override some of the default styles from another class. Here is an example of what I am trying to do.
style.css
.navBar {
position:absolute;
left:0px;
right:0px;
width:100%;
height:50px;
background-color: #333333;
}
.fixed {
position: fixed;
}
.top {
top:0px;
}
.left {
float:left;
left:0px;
}
.navBar .left {
width: 50px;
height:100%;
}
In my html if I have <div class="navBar"> then it will just use the styles in .navBar but when I try to do <div class="navBar left"> I want it to override the width and height of .navBar but keep everything else from .navBar and .left
I dont want to put the height and width inside .left because I want to keep it as universal as possible.

Use the CSS selector .navBar.left. This checks for any elements with a class of navBar that also have a class of left. Currently you are using .navBar .left. Instead of checking if an element has both classes, it checks for all elements with class left that are children of navBar

You probably shouldn't have specific classes like .fixed and .top like that. I'd suggest you to imagine a base class for something you want to style (ie.: your navBar) and than add other classes to modify them (like left). Read a bit about BEM (block, element, modifier) or any other method to make your CSS more modular and try to slowly apply that to your CSS.
You can always stack classes to make them more specific (take priority), like .navBar.left or use !important on super specific classes like those that only set one property, but use any of these techniques too much and your CSS will soon become unstable and super hard to maintain.
Learn how to better structure and organize your CSS. It will pay off!

First of all, Do NOT write classes names in camelCase style, it's not meant for css ,see why.
This is an example of applying and extending the default style by adding multiple classes, click on Run Code snipped to see it in action
html, body{
margin: 0;
}
.nav-bar {
position:absolute;
top: 100px;
width:100%;
height:50px;
background-color: #AC92EC;
text-align: center;
}
.fixed {
position: fixed;
background-color: #A0D467;
}
.top {
top:0;
}
.left {
left:0;
width: 200px;
background: #48cfad;
}
<div class="nav-bar">
defualt navbar
</div>
<div class="nav-bar left">
left navbar
</div>
<div class="nav-bar fixed top">
fixed and top navbar
</div>

Related

How do I delete padding in CSS

I have used CSS below to remove the title and some padding but there is still padding that I can't seem to remove.
This is my current coding:
.site-info { display: none; }
header.entry-header {
display: none;
}
.page .post-header {
display: none;
}
On Inspect it states
<div id="content" class="site-content" style="padding-top: 2.5em;
Can anyone help me please?
The padding is being inherited from somewhere else. Either default browser settings, or one of your other divs/elements. You can use the id of the div, or the class, in CSS to manually change it like so:
#content, .site-content {
padding-top: 0px;
}
You can try just using the id tag or the class tag to see which one specifically is causing the padding inheritance. Would have to see more code/the site to be sure.
The padding is being set somewhere else content is a common id tag in a stylesheet- you can override it.
<style>
body #content{
padding:0px;
}
</style>
if that doesn't work, this will
<style>
body #content{
padding:0px !important;
}
</style>
<div id="content" class="site-content" style="padding-top: 2.5em;
Aren't you getting paddimg from here? The inline style. Inline elements have higher order than internal or external css

How to add CSS styles to element via other CSS [duplicate]

This question already has answers here:
Including another class in SCSS
(6 answers)
Closed 8 years ago.
I have a certain question about applying styles to an element through another CSS class. To be more specific, lets have a look at following. I have div:
<div class="main"></div>
with some styles:
.main {
background: red;
display: inline;
/* some other styles */
}
and I want to apply .another class to the div, but via its .main CSS.
.main {
background: red;
display: inline;
.another
}
.another {
width: 50px;
height: 100px;
}
I assume that a preprocessor (SASS, Compass, etc.) is needed, but can someone advice if this is possible and what to keep in mind?
Thanks
You can assign multiple class to that div. so you can write like this and can apply class.
<div class="main another"></div>
No preprocessor is needed, you can group classes with .class.another, that's the same thing that css preprocessors does.
You can just add multiple classes in html, like <div class="main another and-other">...</div>. In css, you can just group the selectors, the inline order doesn't matter, but it's recommended to use most used class (main) first, and add more specific classes lower. But the order from top to bottom matters, lower in file the selector is, more important it is.
I've created a jsfiddle from your code, take a look. I've added background color so you see the difference, because width and height does not apply to inline elements.
You can merge the two styles like:
.main.another {
background: red;
display: inline;
width: 50px;
height: 100px;
}

Part of list item's text aligned left and rest aligned right

I would like to have part of <li> content aligned to the left ("Title") and rest of it ("[button]") to the right. For each item.
I'm using following HTML code:
<ul class="dual-align-list">
<li><div>Title</div><div>[button]</div></li>
<li><div>Title</div><div>[button]</div></li>
</ul>
and styles:
ul.dual-align-list li
{
display: block;
height: 25px;
}
ul.dual-align-list li div:first-child {float: left}
ul.dual-align-list li div:nth-child(2) {float: right}
But I have a bad feeling, that I'm doing something really wrong.
Is there a better approach/solution to this problem?
But I have a bad feeling, that I'm doing something really wrong.
Is there a better approach/solution to this problem?
The only problem is your classes and use of pseudo-elements aren't very semantic. A better approach would be to give classes to your divs that describe what their content is, and style them that way.
<ul class="title-content-list">
<li><div class="title">Title</div><div class="content">[button]</div></li>
</ul>
And CSS
ul.title-content-list > li { display: block; height: 25px; }
ul.title-content-list > li > div.title { float: left }
ul.title-content-list > li > div.content { float: right }
Or something along those lines.
It's very bad practice to use "left" or "right" as class names - what if you later decide you want your title on the right and button on the left? You'd have to change all your HTML, or have weird CSS where .right positions elements on the left and .left on the right.
What you are doing seems to be working (at least per how you describe what you are looking for here). I'm assuming that your issue is the complexity of your selectors? If so, one thing you could try is moving the selector to the individual element. I know for bootstrap they call this pull-right so I went ahead and did that:
<ul class="dual-align-list">
<!-- Title really only needs to be in a div if you
plan on styling it further -->
<li> Title <div class="pull-right">[button]</div></li>
<li> Title <div class="pull-right">[button]</div></li>
</ul>
See this JSFiddle for a working example with that in it. Hopefully this addresses the actual question!
Edit
By the way, if the issue is just how far the button goes to the right you can put everything in a fixed width container or you can add a margin-right to the "pull-right" class. For the fixed width container, just wrap your ul in:
<div class="container"> <!-- "ul" here --> </div>
You will also need the following style rule as well:
/* edited to use percents for a responsive layout */
.container { margin-left: 5%; margin-right: 5% }
I put this in an update to the previous fiddle you can find here. Hopefully that helps some as well. Good luck!
EDIT (2)
Changed fixed width layout to responsive layout with 5% margins. These could be adjusted per the desired result or even styled with the #media element to vary based on screen size!
Try this:
HTML
<ul class="dual-align-list">
<li>
<div class="left">Title</div>
<div class="right">[button]</div>
</li>
<li>
<div class="left">Title</div>
<div class="right">[button]</div>
</li>
</ul>
CSS
ul.dual-align-list li {
display: block;
height: 25px;
position: relative;
}
ul.dual-align-list li .left {
text-align: left;
position: absolute;
left:0;
}
ul.dual-align-list li .right {
text-align: right;
position: absolute;
right:0;
}
Hopefully this helps :)

How can I display social icons on my website with HTML and CSS?

I am desperately trying to add some icons to my website but can't get them appear properly. What I want is just a container that contains images and display them in line. Then I would want to add some padding to that container and in between the images and that's it.
See here my approach. If anyone can help me out and correct my code so that it actually works, I would be more than happy.
In my HTML file:
<div class="icons">
<div class="email"></div>
<div class="twitter">twitter
</div>
In my CSS:
.icons {
margin-left:30px;
}
.icons .email { background: url(../images/social/email.png) left top no-repeat; }
.icons .twitter { background: url(../images/social/twitter.png) left top no-repeat; }
NOTE: This code did not work for me.
I would create an image sprite for my icons. And then for the markup I would use a list and set the icon images as background images to their respective anchor tags.
Something like this:
HTML
<ul>
<li>Facebook</li>
<li>Twitter</li>
<li>Google Plus</li>
</ul>
CSS
ul li {
float:left;
display:block;
}
.icon {
width:25px;
height:25px;
display:block;
text-indent:-9999px;
background-image:url(http://tridentdesign.com/wp-content/uploads/2012/12/gemicon.jpg);
background-repeat:no-repeat;
}
.facebook {
background-position:-140px -115px;
}
.twitter {
background-position:-185px -115px;
}
.googleplus {
background-position:-140px -265px;
}
HERE IS A FIDDLE TO DEMONSTRATE:
http://jsfiddle.net/Z8zkK/2/
When you are adding an image in CSS you should add atleast Height or width, it's good to add both, you can ignore(not necessarily) when you are adding images using img tag.
To get in inline, as you are using div, add float:left to .icons class
.icons {
margin-left:30px;
float:left;
width:50px;
height:50px;
}
You can also use the answer by Kris Hollenbeck, in that case you have to rewrite you HTML code too, which is highly preferred.

Making CSS only apply when a class is not present?

I have this line of CSS from the Twitter Bootstrap,
.icon-white,.nav-pills>.active>a>[class^="icon-"],.nav-pills>.active>a>[class*=" icon-"],.nav-list>.active>a>[class^="icon-"],.nav-list>.active>a>[class*=" icon-"],.navbar-inverse .nav>.active>a>[class^="icon-"],.navbar-inverse .nav>.active>a>[class*=" icon-"],.dropdown-menu>li>a:hover>[class^="icon-"],.dropdown-menu>li>a:hover>[class*=" icon-"],.dropdown-menu>.active>a>[class^="icon-"],.dropdown-menu>.active>a>[class*=" icon-"],.dropdown-submenu:hover>a>[class^="icon-"],.dropdown-submenu:hover>a>[class*=" icon-"]
{
background-image:url("http://cdn.mydomain.com/static/img/glyphicons-halflings-white.png");
}
How would I go about making that not apply to .dropdown-menu>li.disabled>a, if the disabled class is present on the li I want it to ignore/not apply that CSS. Is this easily possible?
You could apply the style that you want for the general case to everything, and then, lower down in your script, you could override that with another style for the "disabled" class, that mimics your default behavior.
Something like this: http://jsfiddle.net/8cQvz/1/
div{
display:block;
height:25px;
width: 25px;
background-image:url("image.png");
float:left;
margin:5px;
}
div.no
{
background-image:none;
}