Using two CSS classes on one element - css

What am I doing wrong here?
I have a .social div, but on the first one I want zero padding on the top, and on the second one I want no bottom border.
I have attempted to create classes for this first and last but I think I've got it wrong somewhere:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border-bottom: dotted 1px #6d6d6d;
}
.social .first{padding-top:0;}
.social .last{border:0;}
And the HTML
<div class="social" class="first">
<div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
<div class="socialText">Find me on Facebook</div>
</div>
I'm guessing it's not possible to have two different classes? If so how can I do this?

If you want two classes on one element, do it this way:
<div class="social first"></div>
Reference it in css like so:
.social.first {}
Example:
https://jsfiddle.net/tybro0103/covbtpaq/

You can try this:
HTML
<div class="social">
<div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
<div class="socialText">Find me on Facebook</div>
</div>
CSS CODE
.social {
width:330px;
height:75px;
float:right;
text-align:left;
padding:10px 0;
border-bottom:dotted 1px #6d6d6d;
}
.social .socialIcon{
padding-top:0;
}
.social .socialText{
border:0;
}
To add multiple class in the same element you can use the following format:
<div class="class1 class2 class3"></div>
DEMO

Remember that you can apply multiple classes to an element by separating each class with a space within its class attribute. For example:
<img class="class1 class2">

If you have 2 classes i.e. .indent and .font, class="indent font" works.
You dont have to have a .indent.font{} in css.
You can have the classes separate in css and still call both just using the class="class1 class2" in the html. You just need a space between one or more class names.

If you only have two items, you can do this:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border: none;
}
.social:first-child {
padding-top:0;
border-bottom: dotted 1px #6d6d6d;
}

I know this post is getting outdated, but here's what they asked.
In your style sheet:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border-bottom: dotted 1px #6d6d6d;
}
[class~="first"] {
padding-top:0;
}
[class~="last"] {
border:0;
}
But it may be a bad way to use selectors. Also, if you need multiple "first" extension, you'll have to be sure to set different name, or to refine your selector.
[class="social first"] {...}
I hope this will help someone, it can be pretty handy in some situation.
For exemple, if you have a tiny piece of css that has to be linked to many different components, and you don't want to write a hundred time the same code.
div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}
div.myClass1.red {color:red;}
div.myClass2.red {color:red;}
...
div.myClassN.red {color:red;}
Becomes:
div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}
[class~=red] {color:red;}

If you want to apply styles only to an element which is its parents' first child, is it better to use :first-child pseudo-class
.social:first-child{
border-bottom: dotted 1px #6d6d6d;
padding-top: 0;
}
.social{
border: 0;
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
}
Then, the rule .social has both common styles and the last element's styles.
And .social:first-child overrides them with first element's styles.
You could also use :last-child selector, but :first-childis more supported by old browsers: see
https://developer.mozilla.org/en-US/docs/CSS/:first-child#Browser_compatibility and https://developer.mozilla.org/es/docs/CSS/:last-child#Browser_compatibility.

Another option is to use Descendant selectors
HTML:
<div class="social">
<p class="first">burrito</p>
<p class="last">chimichanga</p>
</div>
Reference first one in CSS: .social .first { color: blue; }
Reference last one in CSS: .social .last { color: green; }
Jsfiddle: https://jsfiddle.net/covbtpaq/153/

Instead of using multiple CSS classes, to address your underlying problem you can use the :focus pseudo-selector:
input[type="text"] {
border: 1px solid grey;
width: 40%;
height: 30px;
border-radius: 0;
}
input[type="text"]:focus {
border: 1px solid #5acdff;
}

Related

How to create double borders through CSS

How to use CSS doing like this? especially double borders (NOT using border:double;)
PS: The HTML code of the Demo: ....1801180218031804 ...
You basically use nesting, meaning the parent container (.container) has a border and the child element (.childdiv) has a border. The html (for a single cell) would look like this:
.container,
.childdiv {
padding: 20px;
border: 1px solid black;
}
.container {
display: inline-block;
width: 70px;
}
.childdiv {
display: inline-block;
width: 30px;
}
<div class="container">
<div class="childdiv">
</div>
</div>
Check out the fiddle for a working demo.
Try This:
div {
border:1px solid #ccc;
padding: 10px;
display: inline-block;
}
div:before {
content: attr(data);
display: inline-block;
border:1px solid #ccc;
padding: 10px;
}
<div data="1898"></div>

CSS padding not working properly?

I think i have a issue with the padding.
I made a simple box with only just a padding: 20px; and for me it looks like the padding-bottom is more than 20px.
Here are some screenshots:
Here is my simple css:
.panel {
width: auto;
height: auto;
border-radius: 2px;
margin-bottom: 20px;
position: relative;
}
.panel-default {
padding: 20px;
border: 1px solid #e0e0e0;
background: #fff;
}
.panel-default > .panel-body {
font-size: 14px;
}
HTML:
<div class="grid_4">
<div class="panel panel-default">
<div class="panel-body">Lorem ipsum dolor sit amet.</div>
</div>
</div>
I use 960 Grid System, but i dont think this happend because of this.
And i have in my html, body {} the box-sizing set up to border-box and the line-height to 24px.
Hope someone can help me :)
The padding is alright, the problem is with your margin-bottom: 20px;.
This should be your .css:
.panel {
width: auto;
height: auto;
border-radius: 2px;
position: relative;
}
.panel-default {
padding: 20px;
border: 1px solid #e0e0e0;
background: #fff;
}
.panel-default > .panel-body {
font-size: 14px;
}
This code will have the effect you are looking for.
https://jsfiddle.net/r67nxyL5/
Either you have a margin somewhere.You can check this by inspecting the element using your internet browser (right click).
or you can try floating your .panel-default and giving it a display block like so:
.panel-default {
float: left;
display: block;
}
if this doesn't work it's definitely a margin-bottom somewhere :)
Perhaps you didn't remove the default margin from the paragraph tag, which adds up to the bottom.
Try to set up paragraph margins to 0.

CSS :last-child unexpected behaviour [duplicate]

This question already has answers here:
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 7 years ago.
Hi I am trying to use the last-child operator to hide the last div which applies a border. My issue is that the las div still has the border on the bottom
Below is the HTML for my Test
<div class="car-search">
<!--other divs removed for example-->
<div class="border-bottom"></div>
</div>
<div class="car-search">
<!--other divs removed for example-->
<div class="border-bottom"></div>
</div>
The CSS
.car-search .border-bottom{
border-bottom: 1px solid $lighterGrey;
padding-top: 15px;
margin-bottom: 15px;
width: 770px;
margin-left: 15px;
}
.car-search:last-child .border-bottom{
display: none;
}
I have no idea why the border is being displayed on the last .car-search
Current Output:
.car-search .border-bottom {
border-bottom: 1px solid #000;
padding-top: 15px;
margin-bottom: 15px;
width: calc(100% - 30px);
margin-left: 15px;
}
.car-search:last-child .border-bottom {
display: none;
}
Attempts
I have also tried using the !important tag with no success
I have tried :last-of-type with no success
Unfortunately there is no last-of-class selector inside of CSS. You'll either need to remove the .border-bottom element from your last .car-search element, or fall back to Javascript / jQuery to remove the element from the DOM.
For example, you could run the following jQuery code to remove the last .car-search element's .border-bottom:
$('.car-search').last().children('.border-bottom').remove();
jsFiddle Demo
Alternatively, if you wish to use pure CSS for this, you could wrap your .car-search elements inside of a container <div> and use the :last-of-type selector.
For example, your new structure might look like:
<div class="car-search-wrapper">
<div class="car-search">
<!--other divs removed for example-->
<div class="border-bottom"></div>
</div>
<div class="car-search">
<!--other divs removed for example-->
<div class="border-bottom"></div>
</div>
</div>
Then you may use:
.car-search:last-of-type .border-bottom {
display: none;
}
jsFiddle Demo
As said above there is no :last-of-class selector for now so the only way to do it is javascript. With jQuery you can run the following code:
jQuery('div.car-search:last .border-bottom').css({display:'none'});
Moreover with scss you can write
.car-search {
.border-bottom {
border-bottom: 1px solid $lighterGrey;
padding-top: 15px;
margin-bottom: 15px;
width: calc(100% - 30px);
margin-left: 15px;
}
&:last-of-type {
.border-bottom {
display: none;
}
}
}
Which is a bit less of scss lines.
In compiled CSS:
.car-search .border-bottom {
border-bottom: 1px solid $lighterGrey;
padding-top: 15px;
margin-bottom: 15px;
width: calc(100% - 30px);
margin-left: 15px;
}
.car-search:last-of-type .border-bottom {
display: none;
}

How to remove space between Div Tables

As you can see in this fiddle there is a space between the two bottom divs. How do I go about fixing this issue?
The HTML
<div id="textbox"></div>
<div id="textboxSquare"></div>
<div id="textboxSquare"></div>
The CSS
* {
margin: 0; padding: 0;
}
#textbox {
border: 1px solid #848484;
-webkit-border-top-left-radius: 30px;
-moz-border-radius-topleft: 30px;
border-top-left-radius: 30px;
-webkit-border-top-right-radius: 30px;
-moz-border-radius-topright: 30px;
border-top-right-radius: 30px;
outline:0;
height:25px;
display: block;
box-sizing: border-box;
width: 300px;
padding-left:20px;
padding-right:20px;
}
#textboxSquare {
display:inline-block;
box-sizing: border-box;
height:25px;
width: 150px;
border: 1px solid #848484;
}
Put all of those <div>s on the same line and it'll go away.
It's a problem with display: inline-block;
Here's a reference for ya:
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
Fixed your fiddle
Look at that didn't even notice the other error: Use an #ID only once. The document will only be searched until the very first #ID is found, then it stops.
<div id="textbox"></div>
<div id="textboxSquare"></div> <!-- This id="" must be unique to all others in the document -->
<div id="textboxSquare"></div> <!-- This id="" must be unique to all others in the document -->
Use a class instead: http://jsfiddle.net/8B875/4/
That fiddle also implements a different approach that uses a float: left; property but you'll have to adjust other things potentially if you go that route.
Add
float:left;
clear:none;
In the #textboxSquare:
#textboxSquare {
display:inline-block;
box-sizing: border-box;
height:25px;
width: 150px;
border: 1px solid #848484;
float:left;
clear:none;
}
Fiddle
Don't use negative margins.
A good way to fight is the letter-spacing trick, where you set the letter-spacing of it's parent div to 0, and then reset it inside the inline-block div to normal
Like said already don't use duplicate ID's. But that's not the problem. Use have assigned display: inline-block; to your DIV's. Simply remove this declaration and add a float:left; to these items.
just remove space between div's in your HTML like this:
<div id="textbox"></div>
<div id="textboxSquare"></div><div id="textboxSquare"></div>
or
<div id="textbox"></div>
<div id="textboxSquare"></div><!--
remove this space
--><div id="textboxSquare"></div>

Clearing DIV with float

I have a div (navigation) that is "float:left;".
After this div main content comes. And second divs comes over the first one.
If I add style="clear:both;" after the first dif, then it works.
However, i wonder if this is the right way to do this, this is my only question.
<div class="nav">
<ul>
<li>text</li>...
</ul>
</div>
<div style="clear:both;"></div>
<div id="content-wrapper"></div>
.nav{
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
}
.nav li{
float: left;
margin: 0 2px;
}
.nav li a{
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #fff;
border-right: 1px solid #ccc;
background-color: #3b3d49;
-webkit-border-radius: 7px 7px 0px 0px;
border-radius: 7px 7px 0px 0px;
}
Yes, that works fine. However, you don't need another element to clear the content, you can add the style to the content wrapper.
In your style sheet:
#content-wrapper { clear: both; }
Another approach is to add a container around the floating element, and make it contain its children using the overflow style:
<div class="nav-container">
<div class="nav">
<ul>
<li>text</li>...
</ul>
</div>
</div>
<div id="content-wrapper">
</div>
Then in your style sheet add:
.nav-container { overflow: hidden; }
The main use of the above mentioned "clear:both"
The clear property specifies which sides of an element where other floating elements are not allowed.
"clear:both"
means No floating elements allowed on either the left or the right side.
In answer to your question, cleaning floats with clear:both; is a pretty standard way of doing this, yes.
this is the best way, just add class .group to your container
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
.group {
zoom: 1; /* For IE 6/7 (trigger hasLayout) */
}
yes it is correct to see this example from the creators of html
http://www.w3schools.com/cssref/pr_class_clear.asp
You could use <br clear="all" /> for short hand. And another way is that you could use clearfix method, you could search it on google for the best. this is the method that #conner explained it.

Resources