I have below piece of code and want to hide img tag using css pseudo elements.
<body>
<div>
<img src="hi.png">
<div class="container1">
<p>somevalue </p>
</div>
</div>
<div>
<img src="hi.png">
<div class="container2">
<p>somevalue </p>
</div>
</div>
</body>
<style>
.container :: before{
display: none;
}
</style>
unfortunately above code is not working and my image is not being hidden. Please help me out how to get this done by using CSS.
In above code i want to hide only the image tag which is present before container2 class. I will have a cycle of classes (i just added only 2 for example) please help me out.
From MDN:
In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an
element with the content property.
By using .container::before, you aren't targeting the element before .container, you're simply creating a new element there.
You can see this when you try this code:
.container::before {
content: 'lol';
}
One way to hide your img element using pseudo-classes would be to use for example
img:first-child {
display: none;
}
I'm working on a project where I need to hide a div when the a x is clicked. I'm wanting to target a specific div to be closed, the div that is holding the href nice. I'm getting a bit stuck with targeting the div once the X is clicked. Could you give any pointers for simple designer?
HTML
Div name = footer and the link class is "linky"
CSS
.linky:link {
footer:display: none;
}
Any help would be appreciated.
Regards,
Put the link target directly before the footer:
<body>
hide footer
... other content ...
<div id="linky"></div>
<footer>footer</footer>
</body>
the use the + operator:
#linky:target + footer {
display: none;
}
/* to prevent scrolling to the bottom */
#linky {
position: fixed;
top: 0;
}
see this fiddle
a + b will target b if it is directly after a.
Note the two additional differences from your version:
Use :target instead of :link (:link will target links)
Use id instead of class
I'd like to add that the footer will be visible again as soon as the user clicks on any other #anchor link.
Update: Now preventing scrolling; removed simple version because scrolling can not be prevented there
Try this:
<div id="div1">
Hello, i will be hidden on click the below link
</div>
<b>Click here</b> to hide
## this is the link code
<a href="" id="link" onClick="hide()"><b>Click here</b> to hide
<script>
function hide(){
document.getElementById('div1').style.display="none";
}
</script>
so I'm having a bit of a tough time figuring this out.
I want to edit some links within a specific div, seems simple enough right?
Just put
#mydiv a:link {color:#B40404}
However it does not seem to be working for me! Below is my code:
<div id="leftcontent"><div id="MYDIV">why this is no work</div>
CSS:
#MYDIV {
background-image: url(http://mypicture.com/mypic.jpg);
width:290px;
height:280px;
font-family:Tahoma,Geneva,sans-serif;
padding:25px;
background-repeat:no-repeat;
}
#MYDIV a:link {color:#B40404; }
I have no idea why this isn't working. Any help appreciated!
Thanks
HTML should be like below (a tag inside the div)
<div id="leftcontent">
<div id="MYDIV">why this is no work</div>
</div>
CSS
#MYDIV a{color:#B40404; text-decoration:none}
DEMO
In your case (div inside the a tag) you need not to write id name in css directly write style for a tag
a{color:#B40404; text-decoration:none }
or
#leftcontent a{color:#B40404; text-decoration:none }
DEMO 2
Your CSS is selecting an tag INSIDE a tag. You either need to do the following:
a #MYDIV { /* css code */ }
or
<div id="MYDIV"><a>My link here</a></div>
EDIT:
I just read your comment on the other page. In that case, you need to add either a class or an ID to the and then reference that.
<div id="leftcontent"><div id="MYDIV">why this is no work</div>
CSS
#myLink { /* add style here */ }
<div id="leftcontent"><div id="MYDIV">why this is no work</div>
in css
#MYDIV a:link {color:#B40404; }
i think you need not style the hyperlink in your case.
Since the styling would ultimately would be applied to your div is enclosed within the anchor tag. So give the color: #your_hex_code to the #MYDIV which would suffice your need. That is what you need.
If you made a mistake in the html, Sowmya answer is perfect.
Optional:
Moreover you can use jquery to style that, $("#your anchor").parent().css or use closest.
Thanks
this way is not a correct formating in coding html this will not validate in html validator
<div id="leftcontent">
<a href="http://google.de">
<div id="MYDIV">why this is no work</div>
</a>
</div>
instead of that do this
<div id="leftcontent">
<div id="MYDIV">
sample
</div>
</div>
if you want the whole div link add basic style in your div a
css
#MYDIV {
float:left;
}
#MYDIV a {
float:left;
width:100px;
height:100px;
}
Suppose my web page has a struture like this:
<body>
<div id="fee">
<div id="fi">
<div id="actual_content">
<p>Content</p>
<div id="some_important_stuff">Blah</div>
<p>More content</p>
<span class="and_another_thing">Meh</span>
...
</div>
<div id="fo>
...
</div>
...
</div>
<div id="fum">
...
</div>
...
</div>
<div id="fazz">
...
</div>
...
</body>
I want to create a print CSS style that hides everything except for the contents of actual_content.
My first attempt was like this:
body * {
display: none; /* Hide everything first */
}
/* Show content div and all of its ancestors */
body > #fee {
display: block;
}
body > #fee > #fi {
display: block;
}
body > #fee > #fi > #actual_content {
display: block;
}
/* Unhide contents of #actual_content */
#actual_content * {
display: block; /* Not ideal */
}
However, since there's no "display: auto" or "display: default", I mess up the styles of actual_content's items when I try to unhide them. I also don't like hard coding the path to actual_content since it might change.
You probably want to use the visibility property. W3Schools describes the difference between the display and visibility properties: the visibility property affects the visibility of an element without affecting its structure, i.e. the space it would normally occupy on the page.
on the top level div set visibility:hidden, then on the div you want set visibility:visible
You'll need to go in and target everything that you don't want to show up individually by setting display:none. If you can change class names, etc, you should un-nest the actual_content <div>, then add classes like hide_div to the other <div>s so they're easy to turn off.
I know your tags show you want a CSS solution, but the PrintArea jQuery plugin is perfect for your needs:
PrintArea sends a specified dom element to the printer, maintaining
the original css styles applied to the element being printed.
http://plugins.jquery.com/project/PrintArea
I have a menu and three hidden divs that show up depending on what option the user selects. I would like to show / hide them on click using only CSS. I have it working with jquery right now but I want it to be accessible with js disabled. Somebody here provided this code for someone else but it only works with div:hover or div:active, when I change it to div:visited it doesn't work. Would I need to add something or perhaps this isn't the right way to do it? I appreciate any help :)
The thing is my client wants this particular divs to slide/fade when the menu is selected, but I still want them to display correctly with javascript turned off. Maybe z-index could do the trick...?
For a CSS-only solution, try using the checkbox hack. Basically, the idea is to use a checkbox and assign different styles based on whether the box is checked or not used the :checked pseudo selector. The checkbox can be hidden, if need be, by attaching it to a label.
link to dabblet (not mine): http://dabblet.com/gist/1506530
link to CSS Tricks article: http://css-tricks.com/the-checkbox-hack/
This can be achieved by attaching a "tabindex" to an element. This will make that element "clickable". You can then use :focus to select your hidden div as follows...
.clicker {
width:100px;
height:100px;
background-color:blue;
outline:none;
cursor:pointer;
}
.hiddendiv{
display:none;
height:200px;
background-color:green;
}
.clicker:focus + .hiddendiv{
display:block;
}
<html>
<head>
</head>
<body>
<div>
<div class="clicker" tabindex="1">Click me</div>
<div class="hiddendiv"></div>
</div>
</body>
</html>
The + selector will select the nearest element AFTER the "clicker" div. You can use other selectors but I believe there is no current way to select an element that is not a sibling or child.
Although a bit unstandard, a possible solution is to contain the content you want to show/hide inside the <a> so it can be reachable through CSS:
http://jsfiddle.net/Jdrdh/2/
a .hidden {
visibility: hidden;
}
a:visited .hidden {
visibility: visible;
}
<div id="container">
<a href="#">
A
<div class="hidden">hidden content</div>
</a>
</div>
Fiddle to your heart's content
HTML
<div>
<a tabindex="1" class="testA">Test A</a> | <a tabindex="2" class="testB">Test B</a>
<div class="hiddendiv" id="testA">1</div>
<div class="hiddendiv" id="testB">2</div>
</div>
CSS
.hiddendiv {display: none; }
.testA:focus ~ #testA {display: block; }
.testB:focus ~ #testB {display: block; }
Benefits
You can put your menu links horizontally = one after the other in HTML code, and then you can put all the content one after another in the HTML code, after the menu.
In other words - other solutions offer an accordion approach where you click a link and the content appears immediately after the link. The next link then appears after that content.
With this approach you don't get the accordion effect. Rather, all links remain in a fixed position and clicking any link simply updates the displayed content. There is also no limitation on content height.
How it works
In your HTML, you first have a DIV. Everything else sits inside this DIV. This is important - it means every element in your solution (in this case, A for links, and DIV for content), is a sibling to every other element.
Secondly, the anchor tags (A) have a tabindex property. This makes them clickable and therefore they can get focus. We need that for the CSS to work. These could equally be DIVs but I like using A for links - and they'll be styled like my other anchors.
Third, each menu item has a unique class name. This is so that in the CSS we can identify each menu item individually.
Fourth, every content item is a DIV, and has the class="hiddendiv". However each each content item has a unique id.
In your CSS, we set all .hiddendiv elements to display:none; - that is, we hide them all.
Secondly, for each menu item we have a line of CSS. This means if you add more menu items (ie. and more hidden content), you will have to update your CSS, yes.
Third, the CSS is saying that when .testA gets focus (.testA:focus) then the corresponding DIV, identified by ID (#testA) should be displayed.
Last, when I just said "the corresponding DIV", the trick here is the tilde character (~) - this selector will select a sibling element, and it does not have to be the very next element, that matches the selector which, in this case, is the unique ID value (#testA).
It is this tilde that makes this solution different than others offered and this lets you simply update some "display panel" with different content, based on clicking one of those links, and you are not as constrained when it comes to where/how you organise your HTML. All you need, though, is to ensure your hidden DIVs are contained within the same parent element as your clickable links.
Season to taste.
In 2022 you can do this with just HTML by using the details element. A summary or label must be provided using the summary element. details is supported by all major browsers.
<details>
<summary>Click Here for more info</summary>
Here is the extra info you were looking for.
</details>
HTML
<input type="text" value="CLICK TO SHOW CONTENT">
<div id="content">
and the content will show.
</div>
CSS
#content {
display: none;
}
input[type="text"]{
color: transparent;
text-shadow: 0 0 0 #000;
padding: 6px 12px;
width: 150px;
cursor: pointer;
}
input[type="text"]:focus{
outline: none;
}
input:focus + div#content {
display: block;
}
<input type="text" value="CLICK TO SHOW CONTENT">
<div id="content">
and the content will show.
</div>
A little hack-ish but it works. Note that the label tag can be placed any where. The key parts are:
The css input:checked+div selects the div immediately next to/after the input
The label for said checkbox (or hey leave out the label and just have the checkbox)
display:none hides stuff
Code:
<head>
<style>
#sidebar {height:100%; background:blue; width:200px; clear:none; float:left;}
#content {height:100%; background:green; width:400px; clear:none; float:left;}
label {background:yellow;float:left;}
input{display:none;}
input:checked+#sidebar{display:none;}
</style>
</head>
<body>
<div>
<label for="hider">Hide</label>
<input type="checkbox" id="hider">
<div id="sidebar">foo</div>
<div id="content">hello</div>
</div>
</body>
EDIT: Sorry could have read the question better.
One could also use css3 elements to create the slide/fade effect. I am not familiar enough with them to be much help with that aspect but they do exist. Browser support is iffy though.
You could combine the above effect with javascript to use fancy transitions and still have a fall back. jquery has a css method to override the above and slide and fade for transitions.
Tilda(~) mean some sibling after; not next sibling like plus(+).
[key="value"] is an attribute selector.
Radio buttons must have same name
To string tabs together one could use:
<html>
<head>
<style>
input[value="1"]:checked ~ div[id="1"]{
display:none;
}
input[value="2"]:checked ~ div[id="2"]{
display:none;
}
</style>
</head>
<body>
<input type="radio" name="hider" value="1">
<input type="radio" name="hider" value="2">
<div id="1">div 1</div>
<div id="2">div 2</div>
</body>
</html>
You could do this with the CSS3 :target selector.
menu:hover block {
visibility: visible;
}
block:target {
visibility:hidden;
}
You're going to have to either use JS or write a function/method in whatever non-markup language you're using to do this. For instance you could write something that will save the status to a cookie or session variable then check for it on page load. If you want to do it without reloading the page then JS is going to be your only option.
if 'focus' works for you (i.e. stay visible while element has focus after click) then see this existing SO answer:
Hide Show content-list with only CSS, no javascript used
You can find <div> by id, look at it's style.display property and toggle it from none to block and vice versa.
function showDiv(Div) {
var x = document.getElementById(Div);
if(x.style.display=="none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
<div id="welcomeDiv" style="display:none;" class="answer_list">WELCOME</div>
<input type="button" name="answer" value="Show Div" onclick="showDiv('welcomeDiv')" />
With this method, when you click on Nav Dropdown elements it will NOT disappear, unlike plain :focus solution.
key is:
tabindex in parent element
parentDiv:focus-within hiddenDiv { display: block;}
it will work with both: display and visibility css;
HTML code:
<div className="DevNavBar dbb">
{/* MAKE SURE TO ADD TABINDEX TO PARENT ELEMENT, OTHERWISE FAILS */}
<div className="DevNavBar_Item1 drr" tabIndex="0">
item1
<div className="DevNavBar_Item1_HiddenMenu dgg">
<ul>
<li>blah1</li>
<li>blah2</li>
</ul>
</div>
</div>
</div>
CSS code:
.DevNavBar {
padding: 40px;
}
.DevNavBar_Item1 {
padding: 20px;
width: fit-content;
cursor: pointer;
position: relative;
}
.DevNavBar_Item1:hover {
color: red;
}
.DevNavBar_Item1_HiddenMenu {
display: none;
position: absolute;
padding: 10px;
background-color: white;
z-index: 10;
left: 0;
top: 70px;
}
.DevNavBar_Item1:focus {
color: red; // this is so that when Nav Item is opened, color stays red
}
.DevNavBar_Item1:focus-within .DevNavBar_Item1_HiddenMenu {
display: block;
color: black; // this is to remove Bubbling, otherwise it will be RED, like the hover effect
}
Here is Video Demo I created on my youtube channel (note: this is my youtube channel, so I am affiliated to that channel), the link is for 'show and tell' purposes: https://youtu.be/QMqcZjmghf4
CSS does not have an onlclick event handler. You have to use Javascript.
See more info here on CSS Pseudo-classes: http://www.w3schools.com/css/css_pseudo_classes.asp
a:link {color:#FF0000;} /* unvisited link - link is untouched */
a:visited {color:#00FF00;} /* visited link - user has already been to this page */
a:hover {color:#FF00FF;} /* mouse over link - user is hovering over the link with the mouse or has selected it with the keyboard */
a:active {color:#0000FF;} /* selected link - the user has clicked the link and the browser is loading the new page */