How to select multiple circular DIV elements? - html

I am trying to create a webpage that displays a circle broken up into quadrants and allows the user to select one or more of the quadrants by clicking on them. After a user clicks a quadrant to select it, the quadrant would change color.
Below is a snippet of HTML that displays the upper two quadrants.
Using HTML, CSS, and/or JavaScript, how I can update my code to actually do this?
I've tried various solutions, but the closest I got was being able to select a single quadrant at a time and changing the background color of the enclosing square DIV, not the circular quadrant's color.
<div style="display:inline-block;
position:relative;
height:500px;
width:500px;">
<div style="position:absolute;
top:0;
left:0;">
F1
</div>
<div style="background:#CCC;
border:2px solid #000;
border-radius:500px 0 0 0;
color:#000;
height:500px;
width:500px;">
</div>
</div>
<div style="display:inline-block;
position:relative;
height:500px;
width:500px;">
<div style="position:absolute;
top:0;
right:0;">
F2
</div>
<div style="background:#CCC;
border:2px solid #000;
border-radius:0 500px 0 0;
color:#000;
height:500px;
width:500px;">
</div>
</div>

You can create a simple 2x2 grid using flex, grid, inline-block, etc and use border radius on the container instead of the elements.
Then you can easily add JS code to change background-color of element on click.
$('.box > div').click(function() {
$(this).toggleClass('select');
})
.box {
width: 300px;
height: 300px;
border-radius: 50%;
overflow: hidden;
border: 1px solid;
display: flex;
flex-wrap: wrap;
}
.box>div {
height: 50%;
width: 50%;
border: 1px solid #000;
box-sizing: border-box;
cursor: pointer;
display: inline-flex;
justify-content: center;
align-items: center;
color: #fff;
}
.box>div.f1 {
border-left: 0;
border-top: 0;
background: blue;
}
.box>div.f2 {
border-right: 0;
border-top: 0;
background: red;
}
.box>div.f4 {
border-right: 0;
border-bottom: 0;
background: green;
}
.box>div.f3 {
border-left: 0;
border-bottom: 0;
background: orange;
}
.box>div.select {
background: #111;
}
.box>div:hover {
opacity: 0.5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
<div class="f1">F1</div>
<div class="f2">F2</div>
<div class="f3">F3</div>
<div class="f4">F4</div>
</div>

Related

CSS Triangle bottom right of parent div

I have a parent div that contains a child that is aligned to the bottom right corner of the parent. The child has text within it that I am trying to get to display correctly.
As it is currently set up, the contents of the child has placed the text to the right side instead of within.
CSS:
.container {
background-color: red;
height: 200px;
width: 400px;
position:relative;
}
.gradeTriangle{
width: 0px;
height:0px;
border-bottom: 50px solid #000;
border-left: 50px solid transparent;
bottom: 0;
right: 0;
position: absolute;
color: green
}
HTML:
<div class="container">
<div class="gradeTriangle">
$25
</div>
</div>
Fiddle:
http://jsfiddle.net/vh7m8gey/1/
Output:
I am trying to get the $25 to be centered in the black triangle that is on the bottom right of the child.
How should i approach this?
I created a container for the amount with absolute position right 3px and bottom -45px.
.container {
background-color: red;
height: 200px;
width: 400px;
position:relative;
}
.gradeTriangle{
width: 0px;
height:0px;
border-bottom: 50px solid #000;
border-left: 50px solid transparent;
bottom: 0;
right: 0;
position: absolute;
color: green
}
.amountContainer{
position:absolute;
padding:1%;
bottom:-45px;
right:3px;
}
<div class="container">
<div class="gradeTriangle">
<div class="amountContainer">$25</div>
</div>
</div>
Look at this. You need to insert for example span inside gradeTriangle, and position It using css.
.gradeTriangle span {
position: absolute;
bottom: -40px;
right: 0px;
}
HTML:
<div class="container">
<div class="gradeTriangle">
<span>$25</span>
</div>
</div>
You can easily create the triangle as background of the main container:
.container {
background:
linear-gradient(to bottom right,transparent 49.8%,#000 50%) bottom right/50px 50px no-repeat,
red;
height: 200px;
width: 400px;
position: relative;
}
.gradeTriangle {
bottom: 5px;
right: 5px;
position: absolute;
color: green
}
<div class="container">
<div class="gradeTriangle">
$25
</div>
</div>
Need to add some style to get the design, added the following style for gradeTriangle
.gradeTriangle{
position:absolute;
right:0;
bottom:0;
width: 60px;
height: 60px;
border-bottom: solid 30px black;
border-right: solid 30px black;
box-sizing: border-box;
color:#fff;
border-left: solid 30px transparent;
border-top: solid 30px transparent;
}
please check the sample code.

2 separate css classes applied to 1 element

I have a wordpress page, and I would like to add a bottom border to the post, according to the post category.
If post has only 1 category, then I use:
.category-daily {
border-bottom: red solid 3px;
}
But there are posts who have 2 categories, and therefore 2 classes, for example: category-weekly and category-daily
What can I do to add a red bottom border for daily category and after that add a yellow bottom border for weekly category
Elements cannot have two borders..but you can fake it with a pseudo-element.
body {
text-align: center;
}
div {
height: 100px;
width: 150px;
display: inline-block;
box-shadow: inset 0 0 1px 0 black;
/* for demo purposes only */
padding-bottom: 6px;
position: relative;
}
.category-daily:after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0;
border-bottom: red solid 3px;
}
.category-weekly:after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0;
border-bottom: yellow solid 3px;
}
.category-daily.category-weekly:after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0;
border-top: red solid 3px;
border-bottom: yellow solid 3px;
}
<div class="category-daily"></div>
<div class="category-weekly"></div>
<div class="category-daily category-weekly"></div>
There is no possibility to define more than one border bottom.
If you wish to achive such visual result you have to add an extra element for each category you're applying.
For example with div element for each category:
html
<div class="post">
<div class="daily"></div>
<div class="weekly"></div>
</div>
css
.post .daily{
background:red;
height:1px;
width:100%;
}
.post .weekly{
background:yellow;
height:1px;
width:100%;
}
Or even better: a hr for each category
html
<div class="post">
<hr class="daily"/>
<hr class="weekly" />
</div>
css
.post .daily{
background-color:red;
color:red;
border:0;
height:1px;
}
.post .weekly{
background-color:yellow;
color:yellow;
border:0;
height:1px;
}
One option is to use box-shadow for the other border, like this:
.daily {
border-bottom: 3px solid red;
}
.weekly {
box-shadow: 0 3px 0 yellow;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="daily">This is daily</div>
<div class="daily">This is daily</div>
<div class="weekly">This is weekly</div>
<div class="daily">This is daily</div>
<div class="daily weekly">This is daily and weekly</div>
<div class="weekly">This is weekly</div>
<div class="daily">This is daily</div>
</body>
</html>
You can't have two border-bottoms, but if you're comfortable with jquery, this may work for you:
$('.category-daily.category-weekly').wrap('<div class="bbottom"></div>');
div{
width: 60px;
height: 60px;
}
.category-daily.category-weekly {
border: 1px solid black;
border-bottom: 9px solid red;
}
.category-daily{
border: 1px solid black;
border-bottom: 9px solid red;
}
.category-weekly {
border: 1px solid black;
border-bottom: 9px solid yellow;
}
.bbottom{
padding-bottom: 9px;
border-bottom: 9px solid yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div class='category-daily category-weekly'>
</div>
<br>
<div class='category-daily'>
</div>
<br>
<div class='category-weekly'>
</div>
This wraps a div with both classes with another div which contains the secondary border-bottom.

Left border for dynamic div

I am struggling to get full length left border vertically on a div.
Problem is that i can not fix the height of div as content is loaded dynamically and it can be short and long.
I tried below approach:
Approach 1
CSS
div.right-col {
position: relative;
}
div.right-col:before {
content:"";
background: #ccc;
padding-left:2.5rem;
position: absolute;
bottom: 0;
left: 0;
height: 50%;
width: 1px;
}
HTML
<div class="left-col"></div>
<div class="right-col"></div>
Approach 2
CSS
.right-col {
position: relative;
}
#borderLeft {
border-left: 2px solid #f51c40;
position: absolute;
top: 50%;
bottom: 0;
}
HTML
<div class="left-col"></div>
<div class="right-col"><div id="border-left"></div></div>
Approach 3
CSS
.left-col {display:table-cell}
.right-col {
display:table-cell;
border-left: 1px solid #ccc;
padding-left: 2.5rem;
float: none;
height:auto;
}
HTML
<div class="left-col"></div>
<div class="right-col"></div>
But none of above things are working. Border is only till where content is present.
Is someting like that what you are looking for ?
<div>
<div style="height:100%;display:inline-block;background-color:#FF0000;">LEFT</div>
<div style="height:100%;display:inline-block;background-color:#0000FF;">RIGHT</div>
</div>
Otherwise, it may be easier to use bootstrap and the row & col-xx class.
Html
<div class="main">
<div class="left-col">sdfsdfsdf</div>
<div class="right-col">sdfsdfsdf
<br>
sdfsdfsdf
</div>
</div>
Css
.left-col {display:table-cell; width:100%; background-color: red;}
.right-col {
display:table-cell;
border-left: 1px solid #ccc;
padding-left: 2.5rem;
float: none;
height:auto;
background-color: green;
}
.main{
display:table;
}
Demo
This is sometimes referred to as the Holy Grail Problem and there are a number of hacks for it. However the first 'real' fix i believe is the flexbox. Here is how i would solve your problem:
Fiddle
HTML
<div class="container">
<div class="col left">Hi</br>Let's see how this works.</div>
<div class="col right">Hi</div>
</div>
CSS
.container, .col {
display:flex;
}
.container {
flex:1;
flex-direction:row;
}
.col {
flex-direction:column;
}
.col.right {
border-left: 1px solid black;
}
.col.left {
order:-1;
}

HTML/CSS shapes div layout one on top of the other

I've got this shape made:
https://jsfiddle.net/5vue1buj/1/
However, the way I'm doing this is by inserting:
<br /><br /><br /><br />
in between the top and bottom. How do I do this more elegantly?
Remove all inline styling.
DEMO
HTML
<div>
<div id="top">
<div class="triangle-down-right">
<!--empty-->
</div>
<div class="triangle-down-left">
<!--empty-->
</div>
</div>
<div id="bottom">
<div class="triangle-up-right">
<!--empty-->
</div>
<div class="triangle-up-left">
<!--empty-->
</div>
</div>
</div>
Then add this CSS:
#top, #bottom {
float: none;
overflow: hidden;
}
#top {
margin-bottom: 10px;
}
By using css styles margin/padding you can achieve this.
In your case you have to clear the space between the two containers [top and bottom]. By default div elements are left aligned. I have added an empty divwhich will remove the space in between the two container [using clear:both. height and overflow is added for Cross browser compatibility]
please check this Fiddle.
By using minimal of html and css
You can use only two div and two its psuedo elements :after and :before
.bottom {
position:absolute;
width:210px;
top:180px;
}
.upper {
position:absolute;
width:210px;
top:20px;
}
.upper:before {
content:'';
position:absolute;
left:0;
width: 0;
height: 0;
border-bottom: 100px solid #4679BD;
border-left: 100px solid transparent;
}
.upper:after {
content:'';
position:absolute;
right:0;
width: 0;
height: 0;
border-bottom: 100px solid #4679BD;
border-right: 100px solid transparent;
}
.bottom:before {
content:'';
position:absolute;
left:0;
width: 0;
height: 0;
border-top: 100px solid #4679BD;
border-left: 100px solid transparent;
}
.bottom:after {
content:'';
position:absolute;
right:0;
width: 0;
height: 0;
border-top: 100px solid #4679BD;
border-right: 100px solid transparent;
}
<div class="upper"></div>
<div class="bottom"></div>
Here's another way, with much less CSS...
#top, #bottom {
height: 200px;
position: relative;
}
.right, .left {
height: 0;
width: 0;
display: inline-block;
}
#top {
margin-bottom: 10px;
}
.left {
margin-right: 10px;
}
#top .left {
border-top: 200px solid transparent;
border-right: 200px solid #4679bd;
}
#top .right {
border-top: 200px solid transparent;
border-left: 200px solid #4679bd;
}
#bottom .left {
border-bottom : 200px solid transparent;
border-right: 200px solid #4679bd;
}
#bottom .right {
border-bottom: 200px solid transparent;
border-left: 200px solid #4679bd;
}
<div>
<div id="top">
<div class="left"></div><div class="right"></div>
</div>
<div id="bottom">
<div class="left"></div><div class="right"></div>
</div>
</div>
Just for fun, here another example.
It uses pseudo elements and some new css3 properties to minimize the html markup down to only one div. This div is relatively positioned, but could as well be positioned absolutely for easily placing it wherever you like on the page.
A sophisticated jsfiddle can be found here where you can play around if the values easily (using Sass).
#shape{
position:relative;
background:#4679BD;
width:200px;height:200px;
transform:rotate(45deg);
margin-top:50px;margin-left:50px;
overflow:hidden;
}
#shape::before,#shape::after{
content:"";display:block;
position:absolute;
width:300px;height:10px;
background:white;
transform:rotate(45deg);
transform-origin:5px 5px;
left:-5px;top:-5px;
}
#shape::after{
transform:rotate(-45deg);
bottom:-5px;top:auto;right:-5px;
}
<div id="shape"></div>
Size is easily adjustable by adjusting the width of the pseudo elements like: (dim of shape + 5) * 1,414 and the height determines the gap between the triangles.

How to Have the Div Aligned to the Middle, and That Relative to the Parent Div?

html:
<div class="parent">
<div>
First PageSecond PageThird Page
</div>
</div>
css:
div.parent > div > a {
margin-right: 2em;
}
div.parent {
border: 1px solid red;
height: 100px;
background-color: #bbb;
}
div.parent > div {
border: 1px solid lightblue;
background-color: #fff;
height: 40px;
width: 900px;
display: block;
float: default;
margin: 0 auto 0;
}
Example:
The result of the stylesheet can be seen on JSFiddle: http://jsfiddle.net/BMEXR/2/
Notice: The colors and spacings are set for better differentiating.
How can the div be aligned in the middle, and that relative to its parent?
So that the div will move relative to the height setting of the parent div?
If you don't mind some markup change:
http://jsfiddle.net/BMEXR/30/
HTML:
<div class="parent">
<div class="childContainer">
<div class="childContents">
First PageSecond PageThird Page
</div>
</div>
</div>
CSS:
div.childContents a {
margin-right: 2em;
}
div.childContents{
background-color:white;
height: 25px;
width: 900px;
}
div.parent {
border: 1px solid red;
height: 100px;
background-color: #bbb;
display:table-row;
}
div.childContainer {
border: 1px solid lightblue;
background-color: transparent;
vertical-align:middle;
display: table-cell;
}
Maybe you could try something like this using jQuery: http://jsfiddle.net/BMEXR/32/
HTML:
<div class="parent">
<div class="container">
<ul>
First Page | Second Page | Third Page
</ul>
</div>
</div>
​
CSS:
div.parent {
border: 1px solid red;
height: auto;
background-color: #bbb;
text-align: center;
padding: 5px;
}
div.container {
margin: 0 auto;
border: 1px solid lightblue;
}
​
JS:
$(function() {
var aSize = $('a').size();
var aTotalSize = 0;
for(var i = 0; i < aSize; i++) {
aTotalSize += $('a').eq(i).width()+10;
}
$('.container').width(aTotalSize);
});​