Edit div container depending on the child

0

Is it possible to edit an element depending on the child it contains?

<div>
    <section id="content-area">Soy el hijo de div</section>
</div>

div < #content-area{
  border: solid 1px red;
}

When we have a child element, in the css we only have to specify who is the father to be able to edit it, in some cases the symbol > (greater than) to determine who is the father and who is the child, and I would like to know if there is any way to edit the father using the identifier of the child, or do not know if there is something similar as I mentioned in the previous css, using the symbol < (less than) where # content-area is child of div , to div to be able to put a red border only when parent of #content-area .

    
asked by Andres Beltrán 14.02.2018 в 16:30
source

3 answers

3

Why do not you try to do it with js and jquery, using the "parent" method

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>parent demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<div><p>Hello</p></div>
<div class="selected"><p>Hello Again</p></div>
 
<script>
$( "p" ).parent( ".selected" ).css( "background", "yellow" );
</script>
 
</body>
</html>
    
answered by 14.02.2018 / 16:34
source
3

Currently with CSS you can not achieve what you want.

In the fourth-level selectors of CSS the pseudo-clase : has () that allows you to select an element that has the child elements that are passed as a parameter.

Example:

  

The next selector selects only the <a> elements that contain <img>

a:has(> img)

The problem with this pseudo-clase is that it is not yet supported by any browser:

Therefore the only possible solution is to use JavaScript either native or with some of its libraries, such as jQuery

Example with JavaScript

window.onload = function(){
  var titulo = document.querySelector('h1');

  titulo.parentNode.style.border = 'solid 1px red';
  titulo.parentNode.style.background = '#ccc';
  titulo.parentNode.style.padding = '10px';
}
<div id="contenido">
  <h1>Hola Mundo</h1>
</div>

Example with jQuery

$(document).ready(function(){
  $('h1').parent('#contenido').css({
    'border':'solid 1px red',
    'background': '#ccc',
    'padding': '10px'
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="contenido">
  <h1>Hola Mundo</h1>
</div>
    
answered by 14.02.2018 в 16:55
1

You are developing the "has" selector for css ( link ), but not yet There is no ordinary browser that supports it, it would be like this:

<!doctype html>
<html lang="en">
    <head>
        <title>Hello, world!</title>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    <style>
        div:has(> section#content-area) {
            border: 1px solid red;
        }
    </style>
    <body>

        <div>
            <section id="content-area">Soy el hijo de div</section>
        </div>

        <div>
            <section >Soy el hijo de div falso</section>
        </div>

        <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </body>
</html>

Although as it is not supported, you only have javascript as suggested by Jorge Luis

    
answered by 14.02.2018 в 17:15