grouping CSS elements - html

I'm having a real hard time wrapping my brain around grouping different selectors and styles into one coherent setup.
I found this one set of CSS/HTML code that is exactly what I need (http://jsfiddle.net/joshnh/daFDn/) but I'm at a loss how I can utilize it with my setup.
body {
padding: 50px;
}
ul {
border: 1px solid #444;
display: inline-block;
height: 301px;
position: relative;
width: 400px;
}
li {
font: bold 16px/100px sans-serif;
height: 100px;
}
a {
border-right: 1px solid #444;
border-top: 1px solid #444;
color: red;
display: block;
text-align: center;
text-decoration: none;
width: 99px;
}
li:first-child a {
border-top: none;
}
li:nth-child(2) a {
color: blue;
}
li:nth-child(3) a {
color: green;
}
a:hover {
background: red;
color: #fff;
}
li:nth-child(2) a:hover {
background: blue;
color: #fff;
}
li:nth-child(3) a:hover {
background: green;
color: #fff;
}
img {
background: red;
display: none;
height: 301px;
position: absolute;
right: 0;
top: 0;
width: 300px;
}
li:nth-child(2) img {
background: blue;
}
li:nth-child(3) img {
background: green;
}
a:hover + img,
img:hover {
display: block;
}
I have a WordPress site, using Headway Themes (GUI theme creator). I want to create a section on one page that does exactly what the Fiddle does. I also don't want any other similar HTML elements to be affected by the CSS in that fiddle. (i.e. I use LI's elsewhere on the site and don't want them "font: bold 16px/100px sans-serif;" like this example).
For me, this truly baffles my mind and would seriously appreciate some guidance on how to structure this correctly.
I'm sure I need to create an ID (because this page will be unique to the rest of the site) but I'm not sure that this is correct syntax for an ID:
#switch ul {border: 1px solid #444;display: inline-block;height: 301px; position: relative;width: 400px;}, li {font: bold 16px/100px sans-serif;height: 100px;}
(as an example but is essentially what I want to do)

Just had a quick stab at this - As you say, you'll need to add something to limit those selectors to the scope you're interested in. An id is one way of doing this - in that case you would need to update your selectors to look something like: http://jsfiddle.net/2osg7a31/
Two things to look out for:
- ul#switch instead of #switch ul (since you're applying the styles to the ul that has an id of #switch, rather than a ul with a descendant of #switch)
- Make sure all styles for the descendants are limited to the #switch id too, not just those applied directly to the ul tag.
I'd suggest using a class instead of an id might be a better idea, since you're only adding the identifier to allow styling, rather than to try and identify the element uniquely: http://jsfiddle.net/0h54wseL/

Yes, you'll need to use and id or preferably a class which is better if you want to repeat that specific fiddle in other pages of your website. The way to do it is to add a .switch in every css entry for the list style. Then in your HTML put the whole ul inside a div with class="switch".
HTML:
<div class="switch">
<ul>
<li>
Item 1
<img src="" alt=""/>
</li>
<li>
Item 2
<img src="" alt=""/>
</li>
<li>
Item 3
<img src="" alt=""/>
</li>
</ul>
</div>
Here is the fiddle to explain : http://jsfiddle.net/zyd1822w/1/
Also check this link for more info about essential css selectors.
I hope this helps.

Related

Changing color of link on hover of a div

I'm trying to change the color of a link on hover of a <div>. Is that possible using just CSS? If not, how would I achieve this?
div {
border: 1px solid black;
padding: 15px;
}
div:hover {
color: red;
}
<div>
<a href='www.google.com'> www.google.com </a>
</div>
You need to style the anchor, not the div. Try this:
div {
border: 1px solid black;
padding: 15px;
}
div:hover a {
color: red;
}
<div>
<a href='www.google.com'> www.google.com </a>
</div>
The div itself has no text, so there's no place to apply the color property. So when you hover a div with nothing to color, nothing happens.
As mentioned in another answer, apply the hover to the anchor element, which contains text.
But your original code would work if instead of color you used background-color or border.
div {
border: 1px solid black;
padding: 15px;
}
div:hover {
color: red; /* won't work; nothing to color */
background-color: aqua; /* this will work */
border: 2px dashed #777; /* this will work */
}
<div>
<a href = 'www.google.com'> www.google.com </a>
</div>
rjdown's answer is correct, but the question is if you still need the div at all.
All a div does is provide a block for you to style. If you style the anchor as block, you have just that. Code bloat is bad for your SEO and headache-freeness. ;-)
Try this:
a:link {
display: block;
/* make it act as the div would */
overflow: auto;
/* or what you want, but good practice to have it */
border: solid 1px black;
}
a:hover,
a:focus,
a:active {
border: solid 1px red;
}
<a href='www.google.com'> www.google.com </a>
Remember to use more than a color change on your hover or the 1 in 12 males with color blindness won't see a thing, potentially, happening. The focus and active additions are for accessibility too. Especially focus is very important for keyboard users.
Good luck.
We can simply assign inherit value to all the CSS properties of anchor tag ,
Thus when you hover above its container DIV element , it will inherit all the new properties defined inside DIV:hover.
div {
display: flex;
align-items: center;
justify-content: center;
text-decoration: none;
height: 100px;
width: 100%;
color: white;
background: blue;
}
a {
text-decoration: inherit;
color: inherit;
}
div:hover {
color: orange;
}
<div>
www.google.com
</div>

Can i enter tag in a tag with css

i need to change a element a, while i'm in element b. Can i manage to do so using only CSS/HTML is my question ?
li:active{
img{
border:1px solid black;
}
}
You'll be wanting...
li:active img {
border:1px solid black;
}
this will style all img elements within li:active. If you only want to style immediate descendants of the latter, you'll need:
li:active > img {
border:1px solid black;
}
I suggest you read up on Chris Coyier's article "Child and Sibling Selectors" to learn about more complex CSS. Additionally, there are more complex CSS selectors based on positional relationships, such as li:active + img and li:active ~ img, but that's beyond the scope of this answer.
However, what you suggest is actually possible using SASS, which is a CSS preprocessor.
No, What you can do however is be a little more specific with your selector - try the following:
li:active img{
border:1px solid black;
}
Bon Voyage!
You can do it this way:
li:active img{
border:1px solid black;
}
you can use a descendant selector: li:active img {boder:1px solid black;} or a child selector li:active > img {boder:1px solid black;} if the img is a direct child of the li
CSS3 allows for embeded selectors where there is a condition. An example of valid nested CSS using CSS3 specifications is :
#media screen and (max-width:800px) {
ul {
float: none;
max-width: auto;
width: auto;
}
div:first-of-type {
display: none;
}
}
The condition set here is to set those styles nested when the media type is screen (aka desktop), and is no larger than 800px ( max-width: 800px ).
li:active img{
border: 1px solid black;
}
This is the correct answer, if you want to use 2 classes or whatever in 1 clause you put them at the beginning.
NOTE: to concatenate 2 element you do so with a space.
Hope this helped.
You can do the following instead:
li:active img{
border:1px solid black;
}
The syntax you are referring to is similar to nested rules in CSS pre-processors such as LESS. This syntax is not supported natively by browsers and must instead be compiled into valid CSS syntax.
#header {
color: black;
.navigation {
font-size: 12px;
}
.logo {
width: 300px;
}
}
Becomes:
#header {
color: black;
}
#header .navigation {
font-size: 12px;
}
#header .logo {
width: 300px;
}
http://lesscss.org/features/#features-overview-feature-nested-rules

How to create menu like on the picture? CSS style

How to create menu like on the picture?
For now I have:
<div id="nav">
<nav class="top-nav">
<div class="shell">
HOMEPAGE<span></span>
<span class="top-nav-shadow"></span>
<ul>
<li class="active"><span>home</span></li>
<li><span>services</span></li>
<li><span>projects</span></li>
<li><span>solutions</span></li>
<li><span>jobs</span></li>
<li><span>blog</span></li>
<li><span>contacts</span></li>
</ul>
</div>
</nav>
</div>
And my css for div nav:
#nav{
background-color: transparent;
padding: 0em 0em 0em 2em;
white-space: nowrap;
list-style: none outside none;
margin: 0px;
height: auto;
line-height: normal;
}
How to style this? I'm newbie
From your picture i think you are looking for a way to make Tabs not a menu..
The best thing you can use i think is jQuery Tabs
From that link you can check how to make it using simple html and the jQueryUI css.
If you want to use only css and html kindly check this
How to make a simple tabbed menu with CSS and HTML
I created an example.
Please have a look at it.
Working Fiddle
The main concept this is positioning the li tags on ul.
Giving bottom: -1px to the li tags to let them come on the border of the ul.
If you give li border-bottom then it looks like border of ul.
I hope you can create your own styling by referring this (but not copying the code).
Here's a jsFiddle showing the styles you want.
The LI tags are floated left, and overflow: hidden; is set on the parent UL tag to contain the floats.
The entire UL tag is shifted down position: relative; margin-bottom: -1px; over the top of the element below it. The 1 pixel borders under the A tags give the illusion that the tab is part of the element below. The border-bottom-color on the active tab is set to match the background color of the content area.
The A tags are set as display: block; so that they can be given a min-width, text-align: center; and vertical padding
I also put a small border-radius on the A tags for flair :-p This can be removed if you don't want it.
I'm assuming the class .active will be changed by code running on your server. If not, the JS listed in several of the above answers will change the class when a tab is clicked. Some of the answers only change the class on the tab (which isn't really what you want…), so look at the libraries instead (however, the CSS will need to be modified accordingly as most JS tab libraries require a specific HTML structure). If you need further details, please leave a comment below.
You'll need a few js to do it.
Here is an example which doesn't require any framework, and fit to your needs (using jQuery) :
$('#tab-nav > li').click(function(e) {
// get new active pane id
var idPane = $(this).find('a:first').attr('href').substring(1);
// change active nav
$('#tab-nav > li').removeClass('active');
$(this).addClass('active');
// change active pane
$('.tab-pane').removeClass('active');
$('#'+idPane).addClass('active');
});
.clear { display: block; clear: both; height: 0px; }
#container {
position: relative;
color: #497188;
}
#tab-nav {
position: relative;
z-index: 20;
}
#tab-nav > li {
display: block;
border: 1px solid #497188;
border-bottom: 1px solid #497188;
padding: 1px 3px;
margin: 0 2px -1px;
float: left;
}
#tab-nav > li a {
color: #497188;
text-decoration: none;
}
#tab-nav > li.active,
#tab-content {
background: #DEE7EC;
border-bottom: 1px solid #DEE7EC;
}
#tab-content {
position: relative;
clear: both;
border: 1px solid #497188;
z-index: 10;
}
.tab-pane {
display: none;
position: absolute;
top: 0;
left: 0;
padding: 3px;
}
.tab-pane.active {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="container">
<ul id="tab-nav">
<li class="active"><span>home</span></li>
<li><span>services</span></li>
<li><span>projects</span></li>
<li><span>solutions</span></li>
<li><span>jobs</span></li>
<li><span>blog</span></li>
<li><span>contacts</span></li>
</ul>
<div id="tab-content">
<div id="home" class="tab-pane active">home</div>
<div id="services" class="tab-pane">services</div>
<div id="projects" class="tab-pane">projects</div>
<div id="solutions" class="tab-pane">solutions</div>
<div id="jobs" class="tab-pane">jobs</div>
<div id="blog" class="tab-pane">blog</div>
<div id="contacts" class="tab-pane">contacts</div>
<p class="clear"> </p>
</div>
</div>

Image Change with Mouse Hover

I have some links to my facebook and twitter, these links are images. I want these links to turn lighter when I hover over them. I was thinking I could do this by making two images and making the images change when I hover over the image link. Is this the best way to do it and if it is how do i do it? I couldn't find any help on how to do it this way.
Here is my HTML:
<div class="social">
<a href="https://www.facebook.com/seth.urquhart?sk=wall&v=wall">
<img src="../img/facebook_logo_extended.jpg"/>
</a>
</div>
<br>
<div class="social">
<a href="https://twitter.com/SethUrquhart">
<img src="../img/twitter_logo_extended.jpg"/>
</a>
</div>
Here is my CSS:
p {
color: #232323;
text-indent:0px;
margin-left:30px;
padding-right: 30px;
}
ul {
text-align: center;
color: gray;
}
ul a {
text-decoration: none;
color: black;
}
ul a:hover {
text-decoration: underline;
margin-right: auto;
margin-left: auto;
}
html {
background: #e8e9e1;
}
h1 {
text-align: center;
}
body {
font-family: sans-serif;
color: #232323;
}
.wrap {
min-width: 600px;
width: 1200px;
margin: auto;
height: 100px;
text-align: center;
background-color: none;
}
.content {
background: #ffffff;
width: 900px;
margin-left: auto;
margin-right:auto;
height: auto;
text-indent: 50px;
}
.footer {
text-align: center;
background-color: #383838;
width: 900px;
margin-left: auto;
margin-right: auto;
color: #e8e9e1;
}
.social {
width: 900px;
margin: auto;
height: 100px;
text-align: center;
background-color: none;
}
.social:hover {
margin-right: auto;
margin-left: auto;
background:#cccccc;
color:#000;
}
ul#list-nav {
padding: 0;
list-style: none;
width: 605px;
margin: 0 auto;
}
ul#list-nav li {
display:inline;
}
ul#list-nav li a {
text-decoration:none;
padding:5px 0;
width:150px;
background:#383838;
color:#eee;
float:left;
border-left:1px solid #fff;
}
ul#list-nav li a:hover {
margin-right: auto;
margin-left: auto;
background:#cccccc;
color:#000;
}
Assuming you're willing to use CSS3, I created an example showing one way to get a brief widening effect for the icons (I suppose that is what "dense" means in the question). Reduced code here:
.icon {
-webkit-transition: 0.25s;
transition: 0.25s;
}
.icon:hover {
position: relative;
z-index: 1;
transform: scale(1.7);
-ms-transform: scale(1.7); /* IE 9 */
-webkit-transform: scale(1.7); /* Safari and Chrome */
}
The transform property has good support. The effect with transition isn't so well supported (no IE9 support), but if you are thinking on graceful degration, I think it's quite valid to use that.
EDIT
I'm updating this answer because it could help other people in future. The accepted answer isn't the right approach, since it's using obtrusive JavaScript to do things about styling, where CSS is the right tool. I really hope the OP will take a look here and change their code.
Based on the OP's feedback, I updated the example showing how to get a brightness effect simulated by changing the opacity property with a fallback using filter for IE6-8. In short, here's the code:
.icon {
opacity: 1;
filter: Alpha(Opacity=100);
}
.icon:hover {
opacity: .6;
filter: Alpha(Opacity=60);
}
It's easy and works very well when the parent's background-color is lighter than the element. If you need something more elaborated (if you really want changing between two images), I really suggests you to use CSS sprites.
I don't know what you mean by dense, but you can alter any image property via the onmouseover and restore it with onmouseout. Here's a code snippet to show how to do it. This code simply makes an image dimmer when the mouse is over it, then restores it when the mouse leaves:
<img
src = "test.jpg"
style = "width:50%;"
id = "test"
onmouseover = "document.getElementById('test').style.opacity=0.5"
onmouseout = "document.getElementById('test').style.opacity=1" />
If you wanted to make the images bigger on the hover, you'd change any of the size attributes. For instance, here's a particularly dramatic size jump:
<img
src = "test.jpg"
style = "width:50%;"
id = "test"
onmouseover = "document.getElementById('test').style.width='75%'"
onmouseout = "document.getElementById('test').style.width='50%'" />
Please note that the above is for illustrative purposes only. There are other ways of doing this, and I am not saying the way I presented is the best or even a good one. However, it's clear and I just want you to clearly see how this can be done.
The simpliest solution would probably for you to use background-images rather than images so you can just switch between them. You can even go as far as creating 3 states this way.. inactive, hover, and selected..
Consider cascades and specificity.. If you define your inactive state first, hover state is defined second overwriting the same definitions, selected state is defined last, again with the same definitions and level of specificity. Now each will overwrite the other in the appropriate or they will happen.
An image
div { background:url('http://www.placehold.it/200x200/f2f2f2') no-repeat; }
On hover display a different image
div:hover { background:url('http://www.placehold.it/200x200/666666') no-repeat; }
If the element is an anchor or has some onclick function defined with it.. display a different image on select with a new class
div.selected { background:url('http://www.placehold.it/200x200/000000') no-repeat; }

Displaying a "close" icon upon hover

I have a newsfeed which is obviously organized by an . When the user hovers over each of the items, the background is highlighted. I'd also like to have a small "x" in the top right hand corner of each item, only shown when hovered. This "x" would be a delete button to remove that post.
Right now I just have some basic html stating: <div class="hide-button">x</div>
I know that I don't want the "x" displayed in the html, but rather have it in the CSS. So I have the <li> css below for hovering, as well as the CSS for the hide button. I'd like to know the best method to integrate the hide button div into the <li>
.hide-button {
float: right;
margin-top: -13px;
font-size: 11px;
font-family: helvetica;
color: gray;
}
.hide-button a{
text-decoration: none;
color:gray;
}
.hide-button a:hover {
text-decoration: underline;
color:gray;
}
and the list:
.newsfeedlist li {
background: white;
border-bottom: 1px solid #E4E4E4;
padding: 12px 0px 12px 0px;
overflow: hidden;
}
.newsfeedlist li:hover {
background-color: #F3F3F3;
}
Thank you so much!!!!!
Presuming your delete buttons are inside another container you could do something like
.hide-button {
float: right;
margin-top: -13px;
font-size: 11px;
font-family: helvetica;
color: tray;
display: none;
}
... the other bits of CSS ...
.newsfeedlist li:hover .hide-button {
display: block;
}
Modifying the close button to be hidden by default and then when hovering on a list item you set the display back again on the close button.
Hope this makes sense
Tim
You might really be in need of this:
Demo at jsFiddle.net
I modified an example and tushed it up for multiple content areas or images.
But hide-button element in the li and do
.newsfeedlist li:hover .hide-button {
display: inline-block;
}
and add display: none; to .hide-button
Otherwise, there's always javascript.