Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS select previous sibling [duplicate]

I have a progress bar which has two children(parts). Whenever each of this children is hovered the total height of the progress and its children will change. I have managed to solve for the first children using the next sibling selector but I can't find a solution for the second children (the yellow part). Up to now I have solved this using jQuery but I want to do this in pure CSS.

fiddle: https://jsfiddle.net/zfh263r6/5/

$('#start').on({
  mouseenter: function () {
	  //$(this).css('height', '4px');
      //$( 'progress' ).css('height', '4px');
  },
  mouseleave: function () {
	  //$(this).css('height', '');
     // $( 'progress' ).css('height', '');
  }
});
#progress_wrap {
    position: relative;
    height: 4px; /*max height of progress*/
    background: #f3f3f3;
}

progress {
    appearance: none;
    -moz-appearance: none;
    -webkit-appearance: none;
    width: 100%;
    border:none;
    height: 2px;
    transition:all .25s ease-in;
    cursor: pointer;
    background-color: #fff;
    position: absolute;
    top: 0;
    left: 0;
    display: block;
}

progress:hover, progress:hover + #start {height: 4px}

progress[value]  {
    /* Reset the default appearance */
    -webkit-appearance: none;
     -moz-appearance: none;
    	  appearance: none;

    /* Get rid of default border in Firefox. */
    border: none;

    /* For IE10 */
    color: #f8008c;
}

progress[value]::-webkit-progress-bar {
    background-color: #fff;
    border:none;
}

progress[value]::-webkit-progress-value {background:#f8008c}

progress::-ms-progress-value {background:#f8008c}

progress::-moz-progress-bar {background:#f8008c}

progress::-ms-fill {border: none}

#start {
    height: 2px;
    transition: all .25s ease-in;
    cursor: pointer;
    background-color: #ffe232;
    position: absolute;
    top: 0;
    left: 0px;
    width: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="progress_wrap">
  <progress min="0" max="1" value="0.66" id="progress"></progress> 
  <div id="start" style="display: inline-block; left: 50px;"></div>
</div>
like image 613
M1X Avatar asked Dec 28 '15 21:12

M1X


People also ask

How do I select previous siblings in CSS?

No, there is no "previous sibling" selector. On a related note, ~ is for general successor sibling (meaning the element comes after this one, but not necessarily immediately after) and is a CSS3 selector. + is for next sibling and is CSS2.

How do I select a preceding element in CSS?

If you ever used CSS sibling selectors, you know there's only two. The + sibling combinator selects the first match that comes immediately after, and the ~ subsequent-sibling combinator matches all the ones that come after. But there's no way to select what came before.

What is general sibling selector?

The general sibling selector (~) selects all elements that are next siblings of a specified element.

How do you select parent element in CSS?

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.


Video Answer


2 Answers

No CSS doesn't have a previous sibling selector but you can use ~ selector -

Let's say you have a list of links and when hovering on one, all the previous ones should turn red. You can do it like this:

/* default link color is blue */
.parent a {
  color: blue;
}

/* prev siblings should be red */
.parent:hover a {
  color: red;
}
.parent a:hover,
.parent a:hover ~ a {
  color: blue;
}
<div class="parent">
  <a href="#">link</a>
  <a href="#">link</a>
  <a href="#">link</a>
  <a href="#">link</a>
  <a href="#">link</a>
</div>
like image 140
sudo_dudo Avatar answered Sep 29 '22 08:09

sudo_dudo


From comment:

make progress and start the hover-height and then make progress_wrap overflow:hidden and shorter and make that expand on hover.

To avoid pushing other elements around, we can add some negative bottom margin at the same time.

#progress_wrap {
    position: relative;
    height: 2px;
    background: #f3f3f3;
    overflow:hidden;
    transition: all .25s ease-in;
}

#progress_wrap:hover, progress:hover + #start {
  height: 4px;
  margin-bottom:-2px;
}

progress,#start {
  height: 4px; /*This is a change from existing, not a new declaration*/
}

https://jsfiddle.net/5t90s4jk/

like image 36
Draco18s no longer trusts SE Avatar answered Sep 29 '22 07:09

Draco18s no longer trusts SE