So, to better explain, I am attaching a simple image of what I am trying to achieve.
There is a button, and three contents (two visible and one hidden located in between as shown above).
When the button is clicked, I want to show the hidden content which is located in between to contents.
Is it possible to achieve this with CSS alone?
For example, I can have the hidden content with display:none; then force it visible somehow?
I am not sure what the most efficient way to achieve this.
Any suggestions will be appreciated.
Thanks!
If you want to achieve this using CSS only then there is a way using check boxes or Radio buttons instead of HMTL button tag.
here is a demo https://jsfiddle.net/nileshmahaja/yt76h26n/
HTML
<input type="checkbox" class="check"/>
<div class="box one">1</div>
<div class="box two">2</div>
<div class="box three">3</div>
.box{
height:50px;
width:50px;
background: gray;
margin-bottom:20px
}
.two{
display:none;
}
.check:checked ~ .two {
display:block
}
And you can style checkbox or Radio button as per your convenience.
Do it purely in css solution here:
How to change an image on click using CSS alone?
However, with jquery it can easily be done in 3 seconds with only a couple of lines if you're not opposed to it as shown below!
$("#buttonsid button").click(function(){
$("#idofhiddencontent").toggle();
});
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.btn{
display: inline-block;
padding: 15px 45px;
border: 2px solid #000;
color: #000;
text-decoration: none;
}
div{
width: 100px;
height: 100px;
border: 2px solid #000;
margin: 25px auto;
}
.block-hidden{
display: none;
border: 2px solid #f00;
color: #f00;
}
.btn:focus ~ .block-hidden{
display: block;
}
Button Click
<div class="block">Block</div>
<div class="block block-hidden">Block Hidden</div>
<div class="block">Block</div>
.visible_content:hover > .hidden_content {display:block;}
when you hover on .visible_content , .hidden_content will be shown.
but with click , I don't think that there be any way to do this via css.
but in jquery , it is simple:
$(".visible_content").click(function(){
$(".hidden_content").fadeIn();
});
Related
I know this is an old question, but I wanted to know what is the best way to put text into a border of a div tag. This is what I have
.componentWrapper {
border: solid cadetblue;
border-radius: 40px;
padding: 10px;
width: 95%;
}
<div class='componentWrapper'>Text inside div </div>
I want to add a title into the border of the div tag. I know one option is to use fieldsets but I prefer not to go with this approach. Another approach is the put a box and move it up and set the background color, but I think this fails when you set a different background color on the page.
What is the best solution for this?
I would suggest something like that, using position:absolute:
.componentWrapper {
border: solid cadetblue;
border-radius: 40px;
padding: 15px 10px 10px;
width: 95%;
}
.componentWrapper .header {
position:absolute;
margin-top:-25px;
margin-left:10px;
color:white;
background:cadetblue;
border-radius:10px;
padding:2px 10px;
}
<div class='componentWrapper'><div class="header">Headline</div>Text inside div </div>
Im seeing some strange behavior on my text inputs, they are wrapping on to 2 lines when the text is too long.
They have padding and are 100% width. If I remove either of these 2 CSS rules the text stops wrapping.
I cant put my actual site live and when I try and recreate the issue (eg with jsfiddle) I cant recreate it. Here are screen shots from my iPhone:
What could be causing this? This is not default behavior but the padding and 100% width are required from my design, so I need to find another way of preventing the wrapping.
UPDATE As I said I cant recreate the issue. My attempt is below. Ive used chrome dev tools to copy all the CSS rules, and the html is the same, however the result does not wrap.
http://codepen.io/anon/pen/uHCav
<div class="cont">
<div class="one">
<input placeholder="Middle name" type="text" maxlength="40" name="middle" id="edit-middle" size="60" value="" >
</div>
</div>
.cont {
background: grey;
width: 300px;
margin-left: 100px
}
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
input {
border-left-color: #e35235 !important;
}
input {
border-left-style: solid;
border-left-color: #c9d8e6;
border-left-width: 6px !important;
font-size: 1.2em;
border-top: none !important;
border-bottom: none !important;
border-right: none !important;
}
input {
background: white;
}
input {
padding: 15px 37px 15px 14px;
}
input {
margin: 0;
border: none;
outline: none;
}
input {
width: 100%;
-webkit-appearance: none;
appearance: none;
-moz-border-radius: 0;
-webkit-border-radius: 0;
border-radius: 0;
background-color: transparent;
}
input {
font-weight: 300;
}
input {
-webkit-text-fill-color: #19465e;
}
input {
color: #19465e;
}
.one {
margin-left: -20px;
margin-right: -20px;
}
You might have a word-break property that your input is inheriting. Try adding word-break: normal; to the input.
Start by paring back your document.
Create a new (blank) page with just the basics
<!DOCTYPE html>
<html>
<head>
<title>My page title</title>
</head>
<body>
<div class="cont">
<div class="one">
<input placeholder="Middle name" type="text" maxlength="40" name="middle" id="edit-middle" size="60" value="" >
</div>
</div>
</body>
</html>
Simplify your inputs CSS, just the basics
input {
font-size:1.2em;
background:#DDD;
padding:15px 37px 15px 14px;
border:none;
outline:none;
width:100%;
}
You will notice the input will not wrap and you still have width: 100%; and padding.
Add the rest of your document one bit at a time:
Javascript - check that there is no problem with input
The rest of your CSS - check that there is no problem with input
Any remaining styles for your input - check that there is no problem with input
Any missing HTML - check that there is no problem with input
Put the jigsaw back together one piece at a time and clean up your code as you go.
Replace the width:100% to position:absolute; left:0; right:0; if you need a quick and dirty fix for this. Just ensure that your container div then has the appropriate rules.
As a side note, it's really bad practice to have all those input rules in separate style blocks. Try consolidating those into one, if possible.
Usually this kind of issues are related to the box-sizing context. You can try to define a border context for your input to ignore the padding calculation:
input {
box-sizing: border-box;
}
I am trying to style my css dropdown box using only css...
First of all my dropdown have the custom html arrow down, i am trying to put an image on my css to take this default arrow down so i can put my own image to it but i have no idea how to do it...
This is my css for the dropdown box:
select {
height:32px;
line-height:42px;
width: 88%;
}
How can i put my own image to this code? I also tried following tutorial but its no use for me i can't make it work right...
http://bavotasan.com/2011/style-select-box-using-only-css/
The tutorial works for me very well.
I created a fiddle for you to understand, but you have to understand what the author want to say.
HTML code
<div class="styled-select">
<select>
<option>Here is the first option</option>
<option>The second option</option>
</select>
</div>
CSS code
.styled-select {
width: 240px;
height: 34px;
overflow: hidden;
background: url('http://cdn.bavotasan.com/wp-content/uploads/2011/05/down_arrow_select.jpg') no-repeat right #ddd;
border: 1px solid #ccc;
}
.styled-select select {
background: transparent;
width: 268px;
padding: 5px;
font-size: 16px;
line-height: 1;
border: 0;
border-radius: 0;
height: 34px;
-webkit-appearance: none;
}
http://jsfiddle.net/zNCBY/
I'd like to create a button that changes its style when it gets pressed. This is my CSS code:
button {
font-size: 18px;
border: 2px solid gray;
border-radius: 100px;
width: 100px;
height: 100px;
}
button:active {
font-size: 18px;
border: 2px solid red;
border-radius: 100px;
width: 100px;
height: 100px;
}
<button>Button</button>
It is changed only when I click & hold on it. I want to make it change style after it's pressed. For example, normal state would be white, state while being clicked would be green and after click is released it would be red.
You can do this if you use an <a> tag instead of a button. I know it's not exactly what you asked for, but it might give you some other options if you cannot find a solution to this:
Borrowing from a demo from another answer here I produced this:
a {
display: block;
font-size: 18px;
border: 2px solid gray;
border-radius: 100px;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
}
a:active {
font-size: 18px;
border: 2px solid green;
border-radius: 100px;
width: 100px;
height: 100px;
}
a:target {
font-size: 18px;
border: 2px solid red;
border-radius: 100px;
width: 100px;
height: 100px;
}
<a id="btn" href="#btn">Demo</a>
Notice the use of :target; this will be the style applied when the element is targeted via the hash. Which also means your HTML will need to be this: <a id="btn" href="#btn">Demo</a> a link targeting itself. and the demo http://jsfiddle.net/rlemon/Awdq5/4/
Thanks to #BenjaminGruenbaum here is a better demo: http://jsfiddle.net/agzVt/
Also, as a footnote: this should really be done with JavaScript and applying / removing CSS classes from the element. It would be much less convoluted.
You could use :focus which will remain the style as long as the user doesn't click elsewhere.
button:active {
border: 2px solid green;
}
button:focus {
border: 2px solid red;
}
Should we include a little JS? Because CSS was not basically created for this job. CSS was just a style sheet to add styles to the HTML, but its pseudo classes can do something that the basic CSS can't do. For example button:active active is pseudo.
Reference:
http://css-tricks.com/pseudo-class-selectors/ You can learn more about pseudo here!
Your code:
The code that you're having the basic but helpfull. And yes :active will only occur once the click event is triggered.
button {
font-size: 18px;
border: 2px solid gray;
border-radius: 100px;
width: 100px;
height: 100px;
}
button:active {
font-size: 18px;
border: 2px solid red;
border-radius: 100px;
width: 100px;
height: 100px;
}
This is what CSS would do, what rlemon suggested is good, but that would as he suggested would require a tag.
How to use CSS:
You can use :focus too. :focus would work once the click is made and would stay untill you click somewhere else, this was the CSS, you were trying to use CSS, so use :focus to make the buttons change.
What JS would do:
The JavaScript's jQuery library is going to help us for this code. Here is the example:
$('button').click(function () {
$(this).css('border', '1px solid red');
}
This will make sure that the button stays red even if the click gets out. To change the focus type (to change the color of red to other) you can use this:
$('button').click(function () {
$(this).css('border', '1px solid red');
// find any other button with a specific id, and change it back to white like
$('button#red').css('border', '1px solid white');
}
This way, you will create a navigation menu. Which will automatically change the color of the tabs as you click on them. :)
Hope you get the answer. Good luck! Cheers.
You can do this with php if the button opens a new page.
For example if the button link to a page named pagename.php as, url: www.website.com/pagename.php the button will stay red as long as you stay on that page.
I exploded the url by '/' an got something like:
url[0] = pagename.php
<? $url = explode('/', substr($_SERVER['REQUEST_URI'], strpos('/',$_SERVER['REQUEST_URI'] )+1,strlen($_SERVER['REQUEST_URI']))); ?>
<html>
<head>
<style>
.btn{
background:white;
}
.btn:hover,
.btn-on{
background:red;
}
</style>
</head>
<body>
Click Me
</body>
</html>
note: I didn't try this code. It might need adjustments.
Maybe :active over :focus with :hover will help!
Try
button {
background:lime;
}
button:hover {
background:green;
}
button:focus {
background:gray;
}
button:active {
background:red;
}
Then:
<button onkeydown="alerted_of_key_pressed()" id="button" title="Test button" href="#button">Demo</button>
Then:
<!--JAVASCRIPT-->
<script>
function alerted_of_key_pressed() { alert("You pressed a key when hovering over this button.") }
</script>
Sorry about that last one. :) I was just showing you a cool function!
Wait... did I just emphasize a code block? This is cool!!!
In CSS, is it possible that when I rollover one element, I make another element visible? I have an icon, and when someone mouses over it, I want it to make visible a text element that describes what the icon does.
Here's a CSS only tooltip I use all the time :) Works great, even in IE.
a:hover {
background:#ffffff;
text-decoration:none;
}
/*BG color is a must for IE6*/
a.tooltip span {
display:none;
padding:2px 3px;
margin-left:8px;
width:130px;
}
a.tooltip:hover span{
display:inline;
position:absolute;
background:#ffffff;
border:1px solid #cccccc;
color:#6c6c6c;
}
Easy
<a class="tooltip" href="#">
Tooltip
<span>T his is the crazy little Easy Tooltip Text.
</span>
</a>
Hope it helps.
You can make child-elements visible by hovering on the parent (as Hunter suggests), or siblings:
span:hover + span {display: block; }
There are maybe some slight cross-browser compatibility issues, but with a valid doctype I think IE7+ is okay with sibling selectors (though I've not tried to test that theory).
sure it is!
.me:hover span { display: block; }
If you want to show an element that isn't a child of the element hovered you might need to use javascript
Here's a little slapped-together example that won't work on IE...
<html>
<head>
<style>
div.tooltip
{
margin-top: 16px;
margin-left: -1px;
position: absolute;
border: 1px solid black;
background-color: blue;
color: yellow;
display: none;
}
div.icon
{
width: 16px;
height: 16px;
border: 1px solid blue;
background-color: cyan;
}
div.icon:hover .tooltip
{
display: block;
}
</style>
</head>
<body>
<div class="icon">
<div class="tooltip">This is what the icon does.</div>
</div>
</body>
</html>
But you really should just use jQuery.
Agree with the JavaScript recommendation. Specifically jQuery is easy and most appropriate for page behavior logic. I think CSS should only be look/feel/style...Javascript should be were all your event and behavior logic is.