No jQuery : div onclick toggle div functions - html

I am jQuery-phobic and new to css. So, please do not recommend to use jQuery.
I have two questions with my code.
[solved]When I click any one of .active divs, others go down. How to fix it on top?
How to change display from none to active as I click the div? (the code I wrote only show the div as long as I 'click' it)
.wrap{
text-align: center;
}
.hidden{
background: grey;
display : none;
}
.active{
background : lightcoral;
display: inline-block;
vertical-align: top;
}
.active:active > .hidden{
display: block;
margin : 0 0;
}
<div class = 'wrap'>
<div class = 'active'>
<h2>1</h2>
<div class ='hidden' onclick='layer_toggle()'>a</div>
</div>
<div class = 'active'>
<h2>2</h2>
<div class ='hidden'>b</div>
</div>
<div class = 'active'>
<h2>3</h2>
<div class ='hidden'>c</div>
</div>
</div>

If the usual javascript function suits you.
function switch_active(e) {
if (e.querySelector('.hidden').style.display == 'none') {
e.querySelector('.hidden').style.display = 'block';
} else {
e.querySelector('.hidden').style.display = 'none';
}
}
.wrap{
text-align: center;
}
.hidden{
background: grey;
display : none;
}
.active{
background : lightcoral;
display: inline-block;
vertical-align: top;
}
<div class = 'wrap'>
<div class = 'active' onclick='switch_active(this)'>
<h2>1</h2>
<div class ='hidden'>a</div>
</div>
<div class = 'active' onclick='switch_active(this)'>
<h2>2</h2>
<div class ='hidden'>b</div>
</div>
<div class = 'active' onclick='switch_active(this)'>
<h2>3</h2>
<div class ='hidden'>c</div>
</div>
</div>

You can easily fix the vertical alignment by adding
"vertical-align: top;" to your ".active" class in your CSS.
You say you don't want to use jQuery. I don't know if you are also plain javascript-phobic but if not, I would suggest use simple javascript since that is the easiest way. If you want to keep focussing on CSS, one way to do it is to make them checkboxes or radio buttons (depending on the effect you are looking for), making the checkbox/radio button invisible and styling them as your divs.
Also, there is no reason to be jQuery-phobic. It can look a bit overwhelming but the basics are really not that difficult! :)

Related

Flex wrap and flex basis not working using CSS?

I am making this application, and I am using React. Here, I have many items, and I am dynamically adding them using React. Here's my code to help you understand better -
CSS:
.flexContainer {
display: flex;
flex-wrap: wrap;
}
.flexContainer .item {
padding: 5px;
border: 1px solid aqua;
flex: 1 1 50%;
}
.flexContainer .item {
display: block;
width: 500px;
text-align: center;
}
* {
box-sizing: border-box;
}
React:
<div class="flexContainer">
<div key={p.id} class="item">
<div class="item">
<p className="itemTitle">{p.title}</p>
<button onClick={() => addItem(p)}>
{alreadyAdded ? "Add again" : "Add to Cart"}
</button>
</div>
</div>
</div>
Here is a snippet of my items:
{
id: 7,
price: 50,
button: (
<button
onClick={() => {
alert("This function is not ready yet!!!!");
}}
>
Click!
</button>
),
title: "Apple",
},
So, when I run my program, I get this-
My Pic
I had taken another similar code from here
As you can see, the code I had taken from the other stackoverflow is working, but mine is not working. I've used the same technique to do for mine as well but it's not working.
What I want is, 2 of the items to come in a row, with 50% of width, and so on.
Currently, it's not working.
Please help me out, it would help out a ton!
Thanks.
in terms of your question for that row removing some of the the code will allow this to work.
.flexContainer .item {
display: block;
width: 500px;
}
This code is not really needed unless you have other reasons for it to be in your code but doesnt really play part of your question
As you will see from this link your row works
https://jsfiddle.net/ct96dL2o/1/
so from the original code you supplied this answer will work if you need a more elaborate answer you would need to show more code.
change your item style width
.flexContainer .item {
width:50% !important
}
If you are using bootstrap then use row and inside of that and two col-6 class and also checkout link of bootstrap grid
https://getbootstrap.com/docs/5.2/layout/grid/
<div class="container">
<div class="row">
<div class="col-6">
first
</div>
<div class="col-6">
second
</div>
</div>
</div>

Hover on ID show another Div

I have a navbar that shows a sub navbar on hover. I need to have another sub navbar (so a sub sub navbar) show on a button hover in the sub navbar.
All I need to do is change the Display from "None" to "Block" on hover of the ID= preStart.
This is what I have:
.subsubnavbar-content {
display: none;
position: absolute;
left: 300px;
background-color: #eee;
color: white;
width: 50%;
z-index: 1;
}
#preStart:hover .subsubnavbar-content{
display: block;
}
You can first have a look at this answer to understand the class selectors better.
Now, coming to understanding WHAT is working and WHY is it working. Your subnav-content appears as block on hover of subnav because your subnav-content is a descendent of subnav in the html.
.subnav:hover .subnav-content {
display: block;
}
<div class="subnav">
<div class="subnav-content">
...
</div>
</div>
Whereas your .subsubnavbar-content is NOT a descendent of #preStart in the HTML. For your display: block to work, you need to place your .subsubnavbar-content inside #preStart
<a id="preStart">
Pre-Start Checklists
<i class="fa fa-caret-down"></i>
<div class="subsubnavbar">
<div class="subsubnavbar-content">
...
</div>
</div>
</a>
I can't solve this with CSS, but I know how to with JavaScript. Use the code below.
const preStart = document.querySelector("#preStart");
const subsubnavbar = document.querySelector(".subsubnavbar-content");
preStart.onmouseover = function () {
subsubnavbar.style.display = "block";
}

Controling icons/text shown using CSS classes

I have a div with contents that can be styles as info or warning as shown below. (The underscore is for deactivating the impact of the class for this particular example case and needs not to be addressed in this scope.)
<div className="content info _warning">
...
<div className="footer">
<i className="fas fa-exclamation-triangle"></i>
</div>
</div>
By switching over to
<div className="content _info warning">
...
</div>
I'm controlling the color of the icon at the bottom, as set up by the following styling.
div.content.warning div.footer { ...
color: burlywood;
}
div.content.info div.footer { ...
background-color: fuchsia;
}
The problem is that I'd like the icon to change as well. If I'd be using images, I'd hide all by the needed one but that against using the vectorizable FontAwesome goodies. I considered a set of divs with different contents as shown below but it seems clunky.
...
Is there a CSS based approach that can be used to alter the icon shown in the pseudo-code below?
div.content.warning div.footer { ...
color: burlywood;
image: "fas fa-warning";
}
div.content.info div.footer { ...
background-color: fuchsia;
image: "fas fa-info";
}
You can use the :after pseudo selector in CSS for example:
div.content.warning div.footer:after { ...
content: "(Icon with .warning styles)"
}
div.content.info div.footer:after { ...
content: "(Icon without .warning styles)"
}
if you're using Font Awesome you can dynamically add the content property through Javascript
Since you are using FontAwesome icons, you should keep using fa-* classes on different elements and hide them accordingly with your custom classes. This is what I mean:
<div class="footer">
<div class="info-message">
<i class="fa-info-icon"></i>
</div>
<div class="warning-message">
<i class="fa-warning-icon"></i>
</div>
</div>
And in CSS:
.footer .info-message,
.footer .warning-message {
display: none;
}
.footer.info .info-message {
display: block;
}
.footer.warning .warning-message {
display: block;
}
So the proper icon element will be shown just giving .info or .warning on footer.

Bootstrap. Removing the border around the thumbnail images

I used bootstrap thumbnail class to create a grid of clickable images. For some reason, they have a border and its annoying me.
HTML:
<div class = "the-grid">
<div class = "container">
<div class = "row">
<div class = "col-md-4">
<div class = "thumbnail">
<image src = "Images/data.png"/>
</div>
<div class = "thumbnail">
<image src = "Images/cloud.png"/>
</div>
</div>
</div>
</div>
CSS:
<style>
.the-grid{
background-color: #efefef;
border-bottom: 1px solid #DBDBDB;
min-height: 100%;
}
.the-grid .row .thumbnail{
border: 0px;
box-shadow: none;
border-radius:0px;
background-color: #000;
}
.the-grid .row .thumbnail img:hover {
background-color: #000 !importantl
</style>
You can override the default bootstrap thumbnail border setting by adding this to your CSS file:
.thumbnail {
border: 0;
}
You can also simulate this by inspecting the properties of a thumbnail in Chrome. Find .thumbnail properties and uncheck border. You will see it disappear from your image.
Screenshot of .thumbnail
Once you add the code to your CSS file. Re-inspect and you will see that the border property with a strikethrough.
.thumbnail with no border
Just remove all border property's from .thumbnail and all borders will be removed
.the-grid .row .thumbnail{
box-shadow: none;
}
Go into your bootstrap.min.css (if you have it local, otherwise you have to download it) and search for: ".thumbnail" and then delete the part where it says "border" or so.. Don't have any bootstrap version aviable otherwise I will say you where that is, sry.
After inspecting the element, the padding was what i needed to adjust on mine as well as the border to remove that box just in case anyone else runs into this.
thumbnail {
padding: 0;
border: 0;
}
If you using latest Bootstrap,
use class "img-thumbnail" on your img tag !
Example:
<img class ="img-thumbnail" src="Your_Path_Image" alt="">
& Add class "img-thumbnail" on your file custom CSS Bootstrap!
Example:
.img-thumbnail{
background: none;
border: none;
}
why dont you just add the border class like this...
<img class="img-thumbnail border border-0" src="photo_location..." alt="photo">

css to hide the near div when this div is clicked (working for hover)

My code :
<div>
<div class='top-class'>
Header Name
</div>
<div class='body-class'>
This is body a
</div>
</div>
<div>
<div class='top-class'>
Another Header Name
</div>
<div class='body-class'>
Another body
</div>
</div>
css code I tried:
.top-class:hover + .body-class { display: block; } /* This is working */
But, I want that to happen when header is clicked. So, i tried this:
.top-class:visited + .body-class { display: block; } /* DIDNT work */
The pseudo class "active" seems to do the job
.top-class:active + .body-class { display: block; background-color: red; }
You can check my jsfiddle
You can use tabindex in you first div then it can have focus event on.
<div class='top-class' tabindex=1>Header Name</div>
Then in css you test focus pseudo class
.top-class:focus + .body-class { display: block; background-color: red; }
Check this jsfiddle