CSS Expander with only CSS - css

i don't want to flood my visitors display with all news, so i want to use expanders for each news. But i want to support vistors with JavaScript disabled too.
My try:
#news > .panel > .panel-heading > .panel-title > .label{
float: right;
}
#news > .panel > .panel-body {
display: none;
}
#news > .panel > panel-heading > panel-title > a:visited < .panel-title < .panel-heading < .panel > .panel-body {
display: block;
}
<div id="news" class="tab-pane active">
{% for announcement in server.announcements.all %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ announcement.title }} <span class="label label-default">By {{ announcement.writer.get_username }} at {{ announcement.date_created }}</span></h3>
</div>
<div class="panel-body">
{{ announcement.content|safe_html }}
</div>
</div>
{% endfor %}
</div>

You need to make some changes
First, you will never (at current CSS3 capablity) be able to get what you desire using pure CSS using the :visited psuedo-class for two reasons: (1) the a element is not at the sibling level of the .panel-body, so it cannot control .panel-body through css, and (2) the :visited pseudo-class has severe restrictions on what it allows a designer to control (for privacy reasons).
So what can you do? Use :target instead.
But that will (1) limit you to allowing only one news item open at a time, and (2) requires you to set id properties on your .panel-body elements to match the href of the a tag controlling it. So you would need html structure like this:
<div id="news" class="tab-pane active">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Title 1
<span class="label label-default">By writer name</span>
</h3>
</div>
<div class="panel-body" id="Item1">
Panel 1 body
</div>
</div>
</div>
Where each a has a unique href that is tied to the id of the .panel-body of the item. Then you can get the functionality similar to what you seek by this CSS for the display:
#news > .panel > .panel-body {
display: none;
}
#news > .panel > .panel-body:target {
display: block;
}
You can see how this works in this fiddle example, and to see how it would work with multiple news items, take a look at this fiddle example.
This solution is only CSS3 compatible, so older browsers with javascript disabled would not be able to see any news items (with javascript you can use that to expand)

Graceful degradation:
I would show all news in a container with internal scrollbar (constrained in height) as in : http://jsfiddle.net/Py2HU/1/
And when JS available would add a Show/Hide button, hide N last news and show/hide them after a click (or add Previous/Next buttons to allow scrolling news one by one)
CSS
.news-wrapper {
width: 300px;
max-height: 400px;
overflow: auto;
border: 1px solid #000;
}
HTML
<div class="news-wrapper">
<ul class="news">
<li class="news-item">Lorem ipsum </li>
<li class="news-item">Lorem ipsum </li>
<li class="news-item">Lorem ipsum </li>
<li class="news-item">Lorem ipsum </li>
</ul>
</div>
Compatibility: IE7+ and easily with IE6 (as simple as .ie6 .news-wrapper { height: 400px } if anyone cares)

This answer is for people who are looking for Single expander only with CSS3.
Bootstrap reference is given only to use Glyph-icons(Up/Down).
check Plunker
HTML
<div class="expandercheckbox">
<input id="e1" type="checkbox" checked="checked" />
<label for="e1" class="expanderheader">Click me to Expand/Collpase</label>
<div class="expandercontainer">
I am in container. I am visible. Click above to make be collpase.
</div>
</div>
CSS
body{
padding:50px;
background: #484848;
color:#fff;
}
.expandercheckbox input[type="checkbox"] {
display: none;
}
.expandercheckbox .expanderheader {
cursor: pointer;
}
.expandercheckbox input[type="checkbox"] + .expanderheader {
color: #fff;
font-weight: bold;
font-size: 12px;
white-space: nowrap;
user-select:none;
-webkit-user-select:none;
}
.expandercheckbox input[type="checkbox"] + .expanderheader:before {
content: "\e113";
display: inline-block;
font: 14px/1em Glyphicons Halflings;
height: 20px;
width: 20px;
border-radius: 20px;
margin: -2px 0.25em 0 0;
padding: 1.5px 3.5px;
vertical-align: top;
background: #717171;
/* Old browsers */
}
.expandercheckbox input[type="checkbox"]:checked + .expanderheader:before {
content: "\e114";
padding: 2.5px;
}
.expandercheckbox input[type="checkbox"]:checked + .expanderheader:after {
font-weight: bold;
color:#000;
}
.expandercontainer{
background:#000;
padding:15px;
}
.expandercheckbox input[type="checkbox"]:checked ~ .expandercontainer {
display: block;
}
.expandercheckbox input[type="checkbox"]:not(:checked) ~ .expandercontainer {
display: none;
}

Related

CSS Grid: how to make grid cell a hyperlink target?

I am designing a simple two-column layout with CSS Grid; the grid areas are named Cell1 and Cell2. In the left column (Cell1) I want a list of hyperlinks; when a hyperlink is clicked, I want the content to open in the right column (Cell2).
I think I could use bookmarks to content already loaded into Cell2, but I prefer a way to display content in the right cell when a link is clicked, without using bookmarks.
Using a CSS Grid layout, is there any way to designate a cell where content should go when a hyperlink is clicked, other than the cell that contains the hyperlinks -- using bookmarks or anything else?
Thanks very much for any info.
Yes, this is possible, but is much easier to do if you are permitted to use JavaScript/jQuery. Here is an example of using HTML and CSS only to accomplish what you need:
a {
text-decoration: none;
color: #333;
}
.tabs {
position: relative;
clear: both;
}
.tabs .tab {
float: left;
margin-right: 10px;
}
.content {
position: absolute;
background-color: white;
width: 100%;
left: 0px;
}
.tabs .tab:nth-of-type(1) .content {
z-index: 1;
}
.tab:target a {
font-weight: bold;
}
.tab:target .content {
z-index: 1;
}
<div class="tabs">
<div class="tab" id="tab1">
Tab 1
<div class="content">Content of Tab1</div>
</div>
<div class="tab" id="tab2">
Tab 2
<div class="content">Content of Tab2</div>
</div>
<div class="tab" id="tab3">
Tab 3
<div class="content">Content of Tab3</div>
</div>
</div>

non-classified element interrupts targeted styling of child of classified elements

I'm trying to style first and last child, inside span with a specific class.
It doesn't matter if I use > or not in my CSS,
This code won't work unless I remove the first and the last DIV, even though they are unrelated to the class I'm trying to target.
Why?
CSS:
.parent-span .cta-wrapper {background:gray;}
.parent-span:last-child > .cta-wrapper {background:red; }
.parent-span:first-child > .cta-wrapper {background:green}
HTML:
<div>xxxxxxxxx</div>
<span class="parent-span">
<div class="cta-wrapper">
<div>1</div>
</div>
</span>
<span class="parent-span">
<div class="cta-wrapper">
<div>2</div>
</div>
</span>
<span class="parent-span">
<div class="cta-wrapper">
<div>3</div>
</div>
</span>
<div>xxxxxxxxx</div>
Use last-of-type and first-of-type instead of last-child and first-child
The last-child / first-child address and count ALL siblings of the parent element: divs, spans, p etc.
The last-of-type / first-of-type selectors address only the one TYPE (the tag, not the class), i.e. the divs OR the spans OR the p tags etc.
.parent-span .cta-wrapper {
background: gray;
}
.parent-span:last-of-type>.cta-wrapper {
background: red;
}
.parent-span:first-of-type>.cta-wrapper {
background: green
}
<div>xxxxxxxxx</div>
<span class="parent-span">
<div class="cta-wrapper">
<div>1</div>
</div>
</span>
<span class="parent-span">
<div class="cta-wrapper">
<div>2</div>
</div>
</span>
<span class="parent-span">
<div class="cta-wrapper">
<div>3</div>
</div>
</span>
<div>xxxxxxxxx</div>
nth CSS Selectors🟉
When using nth CSS selectors, we must take certain things into consideration:
Hint #1 - The ancestor element that all of the target elements have in common.
Hint #2 - Each of the target elements' tagNames and their ancestors as well.
Hint #3 - The nth point of entry which isn't always the target elements. It could possibly be more than one level depending if the target element and/or ancestor elements have sibling elements or not and how they are positioned.
Hierarchy
The layout is a 5 level hierarchy.
ROOT ---- tagName: <html>
LEVEL 0 - tagName: <head>, <body> - Role: <<<COMMON ANCESTOR>>>
LEVEL 1 - tagName: <div>, <span> -- Role: <<<POINT OF ENTRY>>>
LEVEL 2 - tagName: <div> -------------- Role: <<<TARGET ELEMENT>>>
LEVEL 3 - tagName: <div>
Two nth's - nth-child and nth-of-type
nth-child:
If you use nth-child, ignore hint #2,
So at the point of entry the common ancestor (i.e. <body>) has 5 children, not 3 children.
The line-up is: <div>, <span>, <span>, <span>, <div>
So instead of first-child it should be nth-child(2)
Instead of last-child, it's nth-child(4)
nth-of-type:
If you use nth-of-type, Hint #2 is key to understanding how to use nth-of-type.
At the point of entry the common ancestor has 2 <div>s and 3 <span>s.
Now we can specify a <span> as first, last, etc. because nth-of-type differentiates between element tagNames.
[common ancestor]................:body
{direct descendant(a.k.a. child)}:>
[point of entry].................: span:first-of-type
[target element].................: div
body>span:first-of-type div
Of course the selector above can have variations, but the important part is the point of entry span:first-of-type.
Demo
Click any span and it will revert from display:block to display:inline which is what was posted in OP
Hover over any element to see its Level in the hierarchy, its tagName, and what its role is.
There is a version of nth-child styles commented out included as well.*
// For demonstration purposes
Array.from(document.querySelectorAll('span')).forEach(function(spn, idx) {
spn.addEventListener('click', function(e) {
spn.classList.toggle('inline');
}, false);
});
:root::before {
content: 'ROOT';
}
html {
height: 111vh;
width: 90vw;
background: rgba(255, 200, 50, 0.2);
font: 600 15px/1 Consolas;
cursor: crosshair;
overflow-y: scroll;
text-align: center;
color: black;
}
body {
height: 101%;
width: 50vw;
padding: 0 10vw;
margin: 2vh auto;
outline: 0.5rem solid rgba(250, 150, 150, 0.9);
background: rgba(50, 255, 0, 0.4);
font-size: 0.8rem;
}
div {
max-height: 15vh;
max-width: 50vw;
padding: 10px;
outline: 2px dashed darkblue;
background: rgba(255, 200, 50, 0.5);
}
span {
/* OP: display:inline */
display: block;
min-height: 10%;
max-width: 50vw;
padding: 0 20px;
margin: 8px auto;
outline: 3px solid blue;
background: rgba(255, 0, 100, 0.7);
}
.inline {
display: inline;
}
/*::..BEGIN DISABLED OP..::
.parent-span .cta-wrapper {background:gray;}
.parent-span:last-child > .cta-wrapper {background:red; }
.parent-span:first-child > .cta-wrapper {background:green}
}
::..END DISABLED OP..::*/
/*::..BEGIN nth-type-of..::*/
body>span>div {
background: gray;
}
body>span:first-of-type>div {
background: red;
}
body>span:last-of-type>div {
background: green;
}
/*::..END nth-of-type..::*/
/*::..BEGIN nth-child..::
body>span>div {
background: gray;
}
body>span:nth-child(2)>div {
background: red;
}
body>span:nth-child(4)>div {
background: green;
}
::..END nth-child..::*/
<html title='ROOT-HTML'>
<head title='L0-HEAD'>
</head>
<body title='L0-BODY [COMMON ANCESTOR]'>L0
<div title='L1-DIV'>[XXXX] L1 [XXXX]</div>
<span class="parent-span" title='L1-SPAN [POINT OF ENTRY]'>L1
<div class="cta-wrapper" title='L2-DIV [TARGET ELEMENT]'>L2
<div title='L3-DIV'>L3 [1]</div>
</div>
</span>
<span class="parent-span" title='L1-SPAN [POINT OF ENTRY]'>L1
<div class="cta-wrapper" title='L2-DIV [TARGET ELEMENT]'>L2
<div title='L3-DIV'>L3 [2]</div>
</div>
</span>
<span class="parent-span" title='L1-SPAN [POINT OF ENTRY]'>L1
<div class="cta-wrapper" title='L2-DIV [TARGET ELEMENT]'>L2
<div title='L3-DIV'>L3 [3]</div>
</div>
</span>
<div title='L1-DIV'>[XXXX] L1 [XXXX]</div>
</body>
</html>
🟉The terms: common ancestor™, point of entry™, role™, and hierarchy™ are not standard terms, they are of my own creation because these rules are never explained very well AFAIK by anyone (myself included).
In order for first-child and last-child pseudos to work your html content needs to be inside a list or some other series of items. Your spans are singular and so there's no first and last item.
See the unordered list I created and the styling to select first and last list item there.
In order for you to do this, put your spans in a div with the parent class, then select that parent div and then a first-child/last-child for a repeating element inside.
.parent-span .cta-wrapper {background:gray;}
.parent-span:last-child > .cta-wrapper {background:red; }
.parent-span:first-child > .cta-wrapper {background:green}
ul {
list-style: none;
margin: 2em 0 0 0;
padding: 0;
}
ul.parent-span li:first-child
{
background:green;
}
ul.parent-span li:last-child
{
background:red;
}
<div>xxxxxxxxx</div>
<span class="parent-span">
<div class="cta-wrapper">
<div>1</div>
</div>
</span>
<span class="parent-span">
<div class="cta-wrapper">
<div>2</div>
</div>
</span>
<span class="parent-span">
<div class="cta-wrapper">
<div>3</div>
</div>
</span>
<ul class="parent-span">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<div>xxxxxxxxx</div>

CSS styling for a Kendo TreeView

I've got some Bootstrap css styling issues on several Kendo TreeView widgets I'm rendering.
Basically, I have a wizard which the user will navigate through, and on one of the pages I'm display two Kendo TreeView widgets. The user will be able to drag items from the left "source" tree onto their own customized data tree.
Right now the datasource is the same, but that doesn't matter.
The important thing is that the sprite folder image on my left navbar tree is getting hidden every time I navigate to my wizard page which contains the dual treeviews.
I'd like to style of the TreeView so it doesn't interfere with another tree widget in my sidebar. The sprite folder icon is getting hidden in my sidebar tree once I click on the "+" icon which changes my page.
Here is the reportmaint.html page which renders when I click on the "+" icon in sidebar.html (see screen image below). Keep in mind that the reportmaint view contains the left and right TreeViews:
<section data-report-wizard id="reportmaint-view" class="mainbar" data-ng-controller="reportmaint as vm">
<section class="matter">
<div class="container-fluid">
<div class="row-fluid">
<div class="col-md-12">
<div class="widget wlightblue">
<div data-cc-widget-header title="{{vm.wizardStep}}" subtitle="" allow-collapse="true"></div>
<div class="widget-content">
<div class="clearfix">
<!-- Wizard steps 1 thru 4 omiteed for brevity -->
<div id="wizard4" class="reportwizard">
<div class="row-fluid">
<h3>Choose KRIs</h3>
<h4>Selected: {{vm.selectedItem.text}}</h4>
<div class="col-sm-6"> <!-- kendo TreeView widgets -->
<span id="treeview-left" kendo-tree-view="tree"
k-options="vm.treeOptions"
k-datasource="vm.kriDataSource1"
k-on-change="vm.onTreeSelect(kendoEvent)"
</span>
</div>
<div class="col-sm-6">
<span id="treeview-right" kendo-tree-view="tree"
k-options="vm.treeOptions"
k-data-source="vm.kriDataSource2"
k-on-change="vm.onTreeSelect(kendoEvent)">
</span>
</div>
</div>
<div class="buttons">
<button class="goto5" ng-click="vm.wizardStep='Report Dimensions'">Next</button>
<button class="cancel-btn backto3" ng-click="vm.wizardStep='Report Dimensions'">Back</button>
<button class="cancel-btn close-popup">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
css code in reportmaint.html :
<style scoped>
#treeview-left, #treeview-right {
color:#000;
}
.k-textbox {
width: 11.8em;
}
.demo-section {
width: 700px;
}
.reportwizard {
width: 510px;
height: 323px;
margin: 0 auto;
padding: 10px 20px 20px 170px;
}
.reportwizard h3 {
font-weight: normal;
font-size: 1.4em;
border-bottom: 1px solid #ccc;
}
.reportwizard ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.reportwizard li {
margin: 7px 0 0 0;
}
textarea {
vertical-align: top;
}
label {
display: inline-block;
width: 90px;
text-align: right;
}
.required {
font-weight: bold;
}
.accept, .status {
padding-left: 90px;
}
.valid {
color: green;
}
.invalid {
color: red;
}
span.k-tooltip {
margin-left: 6px;
}
</style>
and my sidebar.html with css code which styles the left TreeView "reports" menu :
<div style="float:left;">
<span id="treeview" kendo-tree-view="tree"
k-options="vm.treeOptions"
k-data-source="vm.reportsTree"
k-on-change="vm.onTreeSelect(kendoEvent)">
</span>
</div>
<style scoped>
.k-treeview .k-plus, .k-treeview .k-minus, .k-treeview .k-plus-disabled, .k-treeview .k-minus-disabled {
background-image: url("../../Content/kendo/2014.1.624/Uniform/sprite.png");
background-position: -161px -192px;
width: 10px;
height: 11px;
cursor: pointer;
margin-left:-10px;
}
#treeview_tv_active > div > span.k-icon.k-plus {
background-image: url("../../Content/kendo/2014.1.624/Uniform/sprite.png");
background-position: -176px -192px;
width: 10px;
height: 11px;
}
#treeview_tv_active > div > span.k-icon.k-minus {
background-image:url("../../Content/kendo/2014.1.624/Uniform/sprite.png");
background-position: -177px -211px;
width: 10px;
height: 11px;
}
#treeview {
color: #fff; /* white */
}
</style>
Again, once I click on the "+" sign I navigate to reportmain.html. This is when the sidebar treeview styling inadvertently changes; and the background-image folder icon gets hidden.
thanks.
Bob

Centering a Display:inline; element

I can´t center this element. I believe it is because of display:inline; in the CSS block. Does anyone have an idea?
<p class="mr">Monatliche Rate </p>
<a class="info">
<div class="circle-text">
<div>?</div></div>
<span> Netto-Rate</span>
</a>
<p class="mr">:
<span id="results"></span> €</p>
CSS
.mr {
color: #1d6912;
font-size: 18px;
font-weight: bold;
margin-bottom: 5px;
margin-top:2px;
text-align:center;
**display:inline;**
}
You can place it in a wrapper, and add text-align:center to the wrapper.
<div class="mr-wrapper">
<p class="mr">Monatliche Rate</p>
</div>
.mr-wrapper {
text-align:center
}
.mr {
display: inline;
}
Demo
based on your question and the 1st answer I have prepared 3 examples for you to choose from:
http://jsfiddle.net/mofeenster/6V6Z7/1/
The simplest answer is this:
.mr {
margin: auto;
display: table;
}

Pure CSS collapse/expand div

I have a pure CSS collapsable div which is based on someone else's code who uses the :target psuedoclass. What I am trying to set up is a page with 12+ questions, and when you click on the + button the answer div expands beneath. I cannot figure out how to make multiple collapsing div elements on this page without writing a ton of extra CSS. Anyone have suggestions on how to write this so my CSS code is minimized? (i.e., so i dont have to input a bunch of unique selectors for each of the 12+ questions).
I cannot use Javascript since this is going on a wordpress.com site which does not allow JS.
Here is my jfiddle: http://jsfiddle.net/dmarvs/94ukA/4/
<div class="FAQ">
+
-
<div class="question"> Question Question Question Question Question Question Question Question Question Question Question? </div>
<div class="list">
<p>Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer </p>
</div>
</div>
/* source: http://www.ehow.com/how_12214447_make-collapsing-lists-java.html */
.FAQ {
vertical-align: top;
height:auto !important;
}
.list {
display:none;
height:auto;
margin:0;
float: left;
}
.show {
display: none;
}
.hide:target + .show {
display: inline;
}
.hide:target {
display: none;
}
.hide:target ~ .list {
display:inline;
}
/*style the (+) and (-) */
.hide, .show {
width: 30px;
height: 30px;
border-radius: 30px;
font-size: 20px;
color: #fff;
text-shadow: 0 1px 0 #666;
text-align: center;
text-decoration: none;
box-shadow: 1px 1px 2px #000;
background: #cccbbb;
opacity: .95;
margin-right: 0;
float: left;
margin-bottom: 25px;
}
.hide:hover, .show:hover {
color: #eee;
text-shadow: 0 0 1px #666;
text-decoration: none;
box-shadow: 0 0 4px #222 inset;
opacity: 1;
margin-bottom: 25px;
}
.list p{
height:auto;
margin:0;
}
.question {
float: left;
height: auto;
width: 90%;
line-height: 20px;
padding-left: 20px;
margin-bottom: 25px;
font-style: italic;
}
Depending on what browsers/devices you are looking to support, or what you are prepared to put up with for non-compliant browsers you may want to check out the <summary> and <detail> tags. They are for exactly this purpose. No css is required at all as the collapsing and showing are part of the tags definition/formatting.
I've made an example here:
<details>
<summary>This is what you want to show before expanding</summary>
<p>This is where you put the details that are shown once expanded</p>
</details>
Browser support varies. Try in webkit for best results. Other browsers may default to showing all the solutions. You can perhaps fallback to the hide/show method described above.
Using <summary> and <details>
Using <summary> and <details> elements is the simplest but see browser support as current IE is not supporting it. You can polyfill though (most are jQuery-based). Do note that unsupported browser will simply show the expanded version of course, so that may be acceptable in some cases.
/* Optional styling */
summary::-webkit-details-marker {
color: blue;
}
summary:focus {
outline-style: none;
}
<details>
<summary>Summary, caption, or legend for the content</summary>
Content goes here.
</details>
See also how to style the <details> element (HTML5 Doctor) (little bit tricky).
Pure CSS3
The :target selector has a pretty good browser support, and it can be used to make a single collapsible element within the frame.
.details,
.show,
.hide:target {
display: none;
}
.hide:target + .show,
.hide:target ~ .details {
display: block;
}
<div>
<a id="hide1" href="#hide1" class="hide">+ Summary goes here</a>
<a id="show1" href="#show1" class="show">- Summary goes here</a>
<div class="details">
Content goes here.
</div>
</div>
<div>
<a id="hide2" href="#hide2" class="hide">+ Summary goes here</a>
<a id="show2" href="#show2" class="show">- Summary goes here</a>
<div class="details">
Content goes here.
</div>
</div>
#gbtimmon's answer is great, but way, way too complicated. I've simplified his code as much as I could.
#answer,
#show,
#hide:target {
display: none;
}
#hide:target + #show,
#hide:target ~ #answer {
display: inherit;
}
Show
Hide
<div id="answer"><p>Answer</p></div>
You just need to iterate the anchors in the two links.
+
-
See this jsfiddle http://jsfiddle.net/eJX8z/
I also added some margin to the FAQ call to improve the format.
Or a super simple version with barely any css :)
<style>
.faq ul li {
display:block;
float:left;
padding:5px;
}
.faq ul li div {
display:none;
}
.faq ul li div:target {
display:block;
}
</style>
<div class="faq">
<ul>
<li>Question 1
<div id="question1">Answer 1 </div>
</li>
<li>Question 2
<div id="question2">Answer 2 </div>
</li>
<li>Question 3
<div id="question3">Answer 3 </div>
</li>
<li>Question 4
<div id="question4">Answer 4 </div>
</li>
<li>Question 5
<div id="question5">Answer 5 </div>
</li>
<li>Question 6
<div id="question6">Answer 6 </div>
</li>
</ul>
</div>
http://jsfiddle.net/ionko22/4sKD3/

Resources