When this code runs on a tablet/mobile, the user has to tap twice to reach the linked URL. Instead, is there a way to remove the hover effect when viewing on a touchscreen device without using '#media screen and (max-width: XXXpx)'? - I would really like the hover effect to remain on a desktop site, no matter what width a desktop browser is resized to.
Many thanks in advance!
a:link,
a:visited,
a:hover,
a:active {
color: white;
text-decoration: none;
}
#container {
position: absolute;
display: table;
width: 200px;
}
#one {
position: relative;
display: table-cell;
background-color: orange;
vertical-align: middle;
text-align: center;
height: 200px;
width: 200px;
}
#one:hover {
background-color: green;
}
#one:hover > #hello {
display: none;
}
#one:hover > #world {
font-size: 1.2em;
display: block;
}
#hello {
font-size: 1.2em;
display: block;
}
#world {
display: none;
}
<div id="container">
<a id="one" href="http://www.google.com">
<p id="hello">Hello</p>
<p id="world">World</p>
</a>
</div>
You can add a class on the root / html element ie:
var root = document.querySelector(":root");
if ( 'ontouchstart' in window ) {root.classList.add("touch")}
then use the negation CSS pseudo-class
:root:not(.touch) #one:hover {
background-color: green;
}
:root:not(.touch) #one:hover > #hello {
display: none;
}
:root:not(.touch) #one:hover > #world {
font-size: 1.2em;
display: block;
}
alternative
var root = document.querySelector(":root");
'ontouchstart' in window ? root.classList.add("touch") : root.classList.add("no-touch")
then
.no-touch #one:hover {
background-color: green;
}
.no-touch #one:hover > #hello {
display: none;
}
.no-touch #one:hover > #world {
font-size: 1.2em;
display: block;
}
for touch
.touch #one{
/*something else*/
}
Related
I have a problem with the nth-child proprety.
The problem is on #subnav element. I don't understand why nth-child doesn't work (suppose to change border color and width).
Here my problem and code :
<div class="container">
<!-- subnav -->
<ul id="subnav">
<li><span>Philosophie</span></li>
<li><span>Musiciens</span></li>
<li><span>Programmes</span></li>
<li><span>Médias</span></li>
<li><span>Agenda</span></li>
</ul>
and the sass
.container {
#subnav {
margin-top: 2rem;
display: flex;
justify-content: space-evenly;
li {
padding-bottom: 0.5rem;
border-bottom: 4px 0.5rem;
border-color: solid #f36e52;
}
a {
color: #fff;
text-decoration: none;
span:nth-child(1):after, span:nth-child(2):after{
content: '';
height: 4px;
text-align: right;
float:right;
margin-top: 0.3rem;
}
span:nth-child(1):after {
width: 30%;
background-color: #f36e52;
}
span:nth-child(2):after {
content: '';
width: 100%!important;
background-color: #fff555;
}
}
}
}
I would like to change the width and also the color of each links, but it only take the attributes of span:nth-child(1).
Any idea ? thanks for your help
They all spans are first-child (or nth-child(1)), you should use nth-child on li to select correctly.
And to change the color of links you should write color attribute directly on span not :after.
The final code should be like below:
#subnav {
li {
&:nth-child(1) {
span {
color: red;
&:after {
content: '';
/*some code*/
}
}
}
&:nth-child(2) {
span {
color: green;
&:after {
content: '';
/*some code*/
}
}
}
}
}
Currently I'm working on a website with a dropdown menu.
I want, that if you click on it, it appears, but if you hover away, it disappears until you click it again.
I have the following snippet for the HTML-part (The links are not implemented yet and the dropdown menu is only on the main page at the moment):
<div class='dropdown-project' id="project">
<a class='dropbtn-project' href='#project'>Projects</a>
<div class='dropcontent-project'>
Project1
Project2
</div>
</div>
and this for the CSS-part:
.dropbtn-project {
padding: 16px;
border: none;
}
.dropdown-project {
position: relative;
display: inline-block;
}
.dropcontent-project {
display: none;
position: absolute;
background-color: var(--main-header-background);
min-width: 150px;
z-index: 1;
}
.dropcontent-project a {
padding: 5px 10px;
display: block;
}
.dropcontent-project a:hover {
color: var(--hover-fonts-color);
background: var(--main-decor-color)
}
.dropdown-project:target .dropcontent-project {display: block;}
.dropdown-project:hover .dropbtn-project {background-color: var(--main-decor-color);}
.dropdown-project:not(:hover) .dropcontent-project {display: none;}
But with this I have the problem, that the target will stay after I clicked once, so it will reappear on hover after one click.
If you want to check it out, it's on https://www.mikecraft1224.tk.
(The text is in German, so "Projekte" is "projects" and "Projekt" is "project")
add javascript functions to handle this.
Here I added two functions, one for adding class and another one for removing the class. and I give style to the class also
.active .dropcontent-project {display: block;}
when clicking on the a element, an active class is added to the project element.
On mouse leave from the project element, the added class gets removed by calling the removeClass function.
function addClass() {
var element = document.getElementById("project");
element.classList.add("active");
}
function removeClass() {
var element = document.getElementById("project");
element.classList.remove("active");
}
.dropbtn-project {
padding: 16px;
border: none;
}
.dropdown-project {
position: relative;
display: inline-block;
}
.dropcontent-project {
display: none;
position: absolute;
background-color: var(--main-header-background);
min-width: 150px;
z-index: 1;
}
.dropcontent-project a {
padding: 5px 10px;
display: block;
}
.dropcontent-project a:hover {
color: var(--hover-fonts-color);
background: var(--main-decor-color)
}
.active .dropcontent-project {display: block;}
.dropdown-project:hover .dropbtn-project {background-color: var(--main-decor-color);}
<div class='dropdown-project' id="project" onmouseleave="removeClass()">
<a class='dropbtn-project' href='#project' onclick="addClass()" >Projects</a>
<div class='dropcontent-project'>
Project1
Project2
</div>
</div>
I recommend that you make the link that you will use as a base for the dropdown in the HTML array as a container element and add the dropdown content inside it.
But if you still say you need to run it with this HTML structure, the following style codes will solve the problem.
.dropbtn-project {
padding: 16px;
border: none;
}
.dropcontent-project {
transition: 0.2s ease-out;
opacity: 0;
pointer-events: none;
background-color: var(--main-header-background);
min-width: 150px;
z-index: 1;
}
.dropcontent-project a {
padding: 5px 10px;
display: block;
}
.dropcontent-project a:hover {
color: var(--hover-fonts-color);
background: var(--main-decor-color)
}
.dropbtn-project:hover ~ .dropcontent-project,
.dropcontent-project:hover {
opacity: 1;
pointer-events: initial;
}
I need help :)
There is a screenshot of my Electron app. I work on the title bar but I want to center the title, how I can do that, thanks you guys! And as I'm a beginner I don't know if this method to do a Title bar is good, can you say if this is good ?
(And if I can abuse your kindness, how I can do the double button with "valider", it will be useful to me for the future.
Thank you again very much!
What I want to do
Screen of the begin
Html:
<div id="title-bar">
<button id="close-btn" class="tb-buttons">
<img src="Images/Close.png" />
</button>
<p id="title">CharactersPalette</p>
<button id="eye-btn" class="tb-buttons">
<img src="Images/Hide.png" />
</button>
<button id="min-btn" class="tb-buttons">
<img src="Images/Min.png" />
</button>
</div>
Css:
/* ~~~~~~~~~~~~~ TitleBar ~~~~~~~~~~~~ */
* {
margin: 0px;
padding: 0px;
border: 0px;
-webkit-user-select: none;
-webkit-app-region: no-drag;
}
#title-bar {
width: 100%;
height: 30px;
-webkit-app-region: drag;
background-color: #21252b;
}
#title-bar > button {
height: 30px;
width: 34px;
display: inline-block;
background: transparent;
outline: none;
transition: 0.15s linear;
}
#title-bar > button:hover {
background-color: #d52015;
}
#title-bar > #title {
display: inline-block;
color: #f8f4f4;
}
#title-bar > #eye-btn,
#title-bar > #min-btn {
height: 30px;
width: 34px;
display: inline-block;
background: transparent;
float: right;
transition: 0.15s linear;
}
#title-bar > #eye-btn:hover,
#title-bar > #min-btn:hover {
background-color: #3e4146;
}
#title-bar > button > img {
height: 18px;
width: 18px;
vertical-align: middle;
}
Update you css with below code.
#title-bar {
width: 100%;
height: 30px;
-webkit-app-region: drag;
background-color: #21252b;
display: flex; /* Add this */
align-items: center; /* Add this */
}
#title-bar > #title {
display: inline-block;
color: #f8f4f4;
margin: 0 auto; /* Add this */
}
You need add "text align center"
#title-bar > #title {
display: inline-block;
color: #f8f4f4;
text-align: center;
}
complement the answer ...
You can apply text-align:center; to the parent block and apply float: left to the first button.
Title says it all; my css:
a.menu:hover {
opacity: 1;
text-shadow: 0 0 10px white;
}
a.menu:hover ~ .dropdown {
display: block;
}
.dropdown {
display: none;
width: 50px;
height: 50px;
position: absolute;
top: 120px;
left: 120px;
background: red;
}
HTML:
<p class="left_topbar">
<img src="css/img/logo.png">
Games ▾
</p>
<div class="dropdown"></div>
Why does the .dropdown now get visible when hovering over the menu link?
Actually your h2 is not a child of h1 tag. You have to use sibling operator(+) to achieve this.
h1:hover + h2{
display: block;
}
The above solution will point the next immediate sibling element. If you want to target all the elements then use the ~ operator.
h1:hover ~ h2{
display: block;
}
EDIT:
Based on your edit, Looks like you have to change the order like below.
a.menu:hover {
opacity: 1;
text-shadow: 0 0 10px white;
}
.dropdown {
display: none;
width: 50px;
height: 50px;
position: absolute;
top: 120px;
left: 120px;
background: red;
}
a.menu:hover ~ .dropdown {
display: block;
}
HTML
<div class="left_topbar">
<img src="css/img/logo.png">
Games ▾
<div class="dropdown"></div>
</div>
You want to change
>
to
+
As the arrow is a descendant selector whereas the plus is a sibling selector.
I have a horizontal nav bar, and my a elements are not expanding to be the width & height of the parent li element.
How can I fix my CSS so that the a elements are as wide & tall as the outer/parent li element?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
<!--
#navbar {
width: 450px;
height: 40px;
background-color: RGB(112,112,112);
list-style: none;
}
#navbar li {
width: 112.5px;
height: 40px;
display: inline;
float: left;
}
#navbar li a {
color: white;
width: 112.5px;
height: 40px;
}
#navbar a:link {
background-color: red;
}
#navbar a:visited {
background-color: purple;
}
#navbar a:active {
background-color: green;
}
#navbar a:hover {
background-color: blue;
}
-->
</style>
</head>
<body>
<ul id="navbar">
<li>home</li>
<li>contact us</li>
<li>about us</li>
<li>other</li>
</ul>
</body>
</html>
By making all a elements display:block
But why, sdleihssirhc? Why?
Because the <a> element is an inline element by default, which means (among many, many other things) you cannot give it a width or a height. If you try, the browser will ignore you.
Make all a elements display: inline-block.
It combines the best of both worlds - you can put them side-by-side without the need for float, and give them whatever width and height you choose. You can even use vertical-align to determine how it should be aligned with surrounding text!
I have indented the additional lines to help with appearance. The a elements must be set to display:block as mentioned by sdleihssirhc. Notice also that by adding some padding to the top ( and subtracting from the overall height of the block) we have some vertical centering as well.
<style type="text/css">
<!--
#navbar {
width: 450px;
height: 40px;
background-color: RGB(112,112,112);
list-style: none;
padding: 0;
}
#navbar li {
width: 112.5px;
height: 40px;
display: inline;
float: left;
}
#navbar li a {
display: block;
float:left;
padding-top: 6px;
text-align: center;
color: white;
width: 112.5px;
height: 34px;
}
#navbar a:link {
background-color: red;
}
#navbar a:visited {
background-color: purple;
}
#navbar a:active {
background-color: green;
}
#navbar a:hover {
background-color: blue;
}
-->
</style>
Modify your CSS as follows:
#navbar li a {
...
display: table;
height: 100%;
width: 100%;
...
}