CSS: apply style only on <a> without inner <img> - css

I would like to apply style to hovered links in a list, but only if there is not image inside <a> element.
The markup is like this:
<div id="leftcolumn">
<ul>
<li>google</li>
<li><img src="http://cso.cz/wpimages/cz2.gif"></li>
</ul>
</div>
and my css:
div#leftcolumn ul a:hover{
background-color: #F8F8F8;
color: Black;
border-bottom: 1px solid Black;
}
I have tried this css, but to no avail:
div#leftcolumn ul a:hover < img{
background-color: #F8F8F8;
color: Black;
border-bottom: 1px solid Black;
}
Here is the jsfiddle

You cannot style an element based on it's children in CSS, what you can do is assign a special class for <a> tags that hold the image and prevent styling it:
<div id="leftcolumn">
<ul>
<li>google</li>
<li><img src="http://cso.cz/wpimages/cz2.gif"></li>
</ul>
</div>
div#leftcolumn ul a:not(.withImage):hover{
background-color: #F8F8F8;
color: Black;
border-bottom: 1px solid Black;
border-top: 1px solid black;
}

I doubt this is possible in pure CSS.
However, you could wrap text in a <span> and only apply rules there, i.e. something like:
<div id="leftcolumn">
<ul>
<li><span>google</span></li>
<li><img src="http://cso.cz/wpimages/cz2.gif"></li>
</ul>
</div>
CSS:
div#leftcolumn ul a:hover > span {
background-color: #F8F8F8;
color: Black;
border-bottom: 1px solid Black;
}
Updated JSFiddle

add .non-img to li which is without img
n do css with using .non-img
div#leftcolumn ul li.non-img a:hover{
background-color: #F8F8F8;
color: Black;
border-bottom: 1px solid Black;
border-top: 1px solid black;
}
<div id="leftcolumn">
<ul>
<li class="non-img">google</li>
<li><img src="http://cso.cz/wpimages/cz2.gif"></li>
</ul>
</div>

Related

Using triangle shape css for dropdown arrow with after before

How can I use triangle shape css for drop-down arrow with before after.
I have created this css
<span class="arrow-top"></span>
.arrow-top {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 35px solid #943c3c;;
}
i have also created demo.
http://trianglecss.blogspot.com/2018/06/triangle-shape-with-css.html
:before and :after are CSS pseudo classes. Think of them as new tags rendered before or after the class you are targeting. So if you have a div called .dropdown you can make an arrow before or after that div without any extra markup.
HTML:
<div class="dropdown">
My Dropdown
</div>
CSS:
.dropdown::after {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 35px solid #943c3c;;
}
You'll end up needing to add additional styling to this triangle to position it, but that's the start.
I'd suggest using icon library such as fontawesome,glyphicons.
It looks better for UI design.
$('.dropdown').on('click', function(){
$(this).toggleClass('show');
})
.dropdown{
position:relative;
}
.dropdown:after{
position:absolute;
content:'';
left:80px;
top:8px;
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 8px solid red;
}
.submenu{display:none;}
.show .submenu{
display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="dropdown">Dropdown
<ul class="submenu">
<li>submenu 1</li>
<li>submenu 2</li>
</ul>
</li>
</ul>

What is the purpose of nav a:focus

Sometimes I see the following code in css.
CSS:
nav a:focus
HTML:
<body>
<nav>
<ul>
<li>Ex1</li>
<li>Ex2</li>
<li>Ex3</li>
<li>Ex4</li>
</ul>
</nav>
</body>
Example: https://jsfiddle.net/RE006/5ezprru0/
Does that css code serve any purpose?
I usually use the :focus selector with input[type] tags.
For example
#text1{
width: 100px;
height: 60px;
border: 1px solid gray;
}
#text1:focus{
border: 1px solid blue;
}
<input type="text" id="text1" />
:focus means is clicked and focused.

Nav bar border issue using css

In my case I am having a nav bar(two tabs) with green border. Below this there is a container with green border. For active tab border-bottom should be white and another tab should be green. So I changed border-bottom:1px solid #fff for active tab alone. This case is working fine is large and medium device. But in small device green line is still displaying under active tab which is the border of the container present under the nav.
HTML Code:
.tab-links:after {
display:block;
clear:both;
content:'';
}
.tab-links li {
margin:3px;
float:left;
list-style:none;
}
.tab-links a {
background:none repeat scroll 0 0 #dfdfdf;
border:1px solid #c3c3c3;
color:#484e2a;
display:inline-block;
font-family:open_sansbold;
font-size:11px;
min-width:166px;
padding:8px 4px;
text-decoration:none;
transition:all .15s linear 0s;
}
.tab-links a:hover {
background:#a7cce5;
text-decoration:none;
}
li.active a, li.active a:hover {
background: #fff;
border:1px solid #dddfb0;
border-bottom:1px solid #fff;
color:#484e2a;
}
.tab-content {
padding:15px;
background:#fff;
border:1px solid #dddfb0;
margin-top:-20px;
}
.tab {
display:none;
}
.tab.active {
display:block;
}
<div class="tabs">
<ul class="tab-links">
<li class="active">Tab #1</li>
<li>Tab #2</li>
</ul>
</div>
<div class="tab-content">
<div id="tab1" class="tab active">
<p>Test Content 1</p>
</div>
<div id="tab2" class="tab">
<p>Test Content 2</p>
</div>
</div>
JS Fiddler link:
https://jsfiddle.net/ktncf454/
#padamapriya :
I did few modifications for you..:
.tab-links li {
margin: 3px;
float: left;
list-style: none;
position: relative;
z-index: 1;
margin-bottom: 2px;
}
.tab-content {
padding: 15px;
background: #fff;
border: 1px solid #dddfb0;
margin-top: -20px;
position: relative;
z-index: 0;
}
Hope this helps!!!!
You will have to write the media query for the this scenario.Generally what happens on different devices the navbar adjust himself automatically.so writing the media query you can change the border for the container and for the tab.
It will good if you share your source code or create a jsfiddle so that we can test and give you the proper solution.

Problems with positioning

When I zoom in and out, the gray in my background color goes over the red border. How do I stop this from happening?
My CSS:
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
color:#fff;
padding-top:30px;
margin: auto;
position:relative;
height:2055px;
border: 1px solid red;
width:1050px;
background-color: #090909;
margin-top:15px;
}
.backgroound {
position:relative;
top:-50px;
/* this gray goes over red border */
background-color: gray;
height:1950px;
border: 1px solid gray;
width:985px;
}
.paa {
background-color:black;
font-size:40px;
position:relative;
border: 1px solid gray;
padding: 0px;
text-align:center;
height:80px;
margin-top:-30px;
}
And my HTML:
<header class="paa"> <a href="#">
<img src="" alt="loggo">
</a>
</header>
<nav>
<ul>
<li>
<a class="vari" href="" id="home"><span >Home</span></a>
</li>
<li>
<a class="vari" href="" id="m">llls</a>
</li>
</ul>
</nav>
<div class="backgroound"></div>
You need to shorten the height of .backgroound.
Either:
height: 1830px;
or
height: 87%;
Fiddle: http://jsfiddle.net/ryanpcmcquen/1q52w0jf/
So you need the grey div to be in the center of the red border?
if that's the case, add this line:
.background
margin:auto;
}
Check this fiddle: https://jsfiddle.net/x3uc9qm5

How to add some style only for main parrent in SASS

I have this markup:
<ul> // w style: border-top: 2px solid #aaa; border-bottom: 1px solid #ccc;
<li>
<ul> // w style: border-bottom: 2px solid #aaa;
<li>
<li>
</ul>
</li>
</ul>
I need to add some style to the main parent and a touch of style that will be inherited for the same item within - as above. So i do this, but it is't work...
ul {
border-top: 2px solid #aaa;
& > {
border-bottom: 1px solid #ccc;
}
}
I know that I can duplicate tag and specify a different style for him inside, but in general is the possibility of such record/write? Thx for help.
I believe your use of the nested selector is wrong, it must be throwing errors from Sass.
You have used the direct descendent selector > after the alias for the containing selector in this case the ul, so your compiled selector would look like ul > which is invalid.
The selector should follow the pattern parent > child. You may be attempting ul > ul but the nested ul is not a direct descendent of the main ul rather it is a child of li.
Your amended code:
ul {
border-top: 2px solid #aaa;
& > li > & {
border-bottom: 1px solid #ccc;
}
}
But note this rule will also apply in the following situation too, so you should probably go for a class on the nested ul.
HTML
<ul> // w style: border-top: 2px solid #aaa; border-bottom: 1px solid #ccc;
<li>
<ul> // w style: border-bottom: 2px solid #aaa;
<li>
<ul> // Another list
<li></li>
<li></li>
</ul>
<li>
</ul>
</li>
</ul>
So this would be better
<ul> // w style: border-top: 2px solid #aaa; border-bottom: 1px solid #ccc;
<li>
<ul class='sub-list'> // w style: border-bottom: 2px solid #aaa;
<li>
<li>
</ul>
</li>
</ul>
with the following css/scss
ul {
border-top: 2px solid #aaa;
}
.sub-list { /* Will inherit the above if used on a ul */
border-bottom: 1px solid #ccc;
}

Resources