CSS Combinator

នៅ​ក្នុង​ភាសា CSS យើង​អាច​ធ្វើ​ការជ្រើសរើស​ element ទាំងឡាយ តាមរយៈ element ណា​ផ្សេង​ទៀត តាម​វិធី​មួយ​ចំនួន មាន​ដូចជា descendant selector (space), child selector (>), adjacent sibling selector (+), general sibling selector (~) ។

ដោយឡែក ការជ្រើសរើស element រង អាច​ត្រូវ​ធ្វើ​ឡើង​ដោយ​ប្រើប្រាស់​វិធី descendant selector (space) ដូច​ខាង​ក្រោម​នេះ៖

<!DOCTYPE html>
<html>
  <head>
  	<meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  	<title>CSS Combinator</title>
    <style>
      div p {
  		background-color: yellow;
	  }
    </style>
  </head>
  
  <body>
	

Paragraph 1 in the div.

Paragraph 2 in the div.

Paragraph 3 in the div.

Paragraph 4. Not in a div.

Paragraph 5. Not in a div.

</body> </html>

Paragraph 1 in the div.

Paragraph 2 in the div.

Paragraph 3 in the div.

Paragraph 4. Not in a div.

Paragraph 5. Not in a div.


ចំណែក​ឯ​វិធី​ជ្រើសរើស​ element តាម​បែប Child Selector វិញ​ត្រូវ​ធ្វើ​ឡើង​តាម​រយៈ​ធាតុ​មេ ដូច​ខាងក្រោម​នេះ៖

<!DOCTYPE html>
<html>
  <head>
  	<meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  	<title>CSS Combinator</title>
    <style>
      div > p {
  		background-color: yellow;
	  }
    </style>
  </head>
  
  <body>
	

Paragraph 1 in the div.

Paragraph 2 in the div.

Paragraph 3 in the div.

Paragraph 4 in the div.

Paragraph 5. Not in a div.

Paragraph 6. Not in a div.

</body> </html>

Paragraph 1 in the div.

Paragraph 2 in the div.

Paragraph 3 in the div.

Paragraph 4 in the div.

Paragraph 5. Not in a div.

Paragraph 6. Not in a div.


ចំណែក​ឯ Adjacent Sibling Selector វិញ ត្រូវ​ធ្វើ​ឡើង​តាម​ element ណា​មួយ ដើម្បី​ជ្រើស​រើស​យក​ element sibling ដទៃ​ទៀត​។

<!DOCTYPE html>
<html>
  <head>
  	<meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  	<title>CSS Combinator</title>
    <style>
      div + p {
  		background-color: yellow;
	  }
    </style>
  </head>
  
  <body>
	

Paragraph 1 in the div.

Paragraph 2 in the div.

Paragraph 3. Not in a div.

Paragraph 4. Not in a div.

</body> </html>

Paragraph 1 in the div.

Paragraph 2 in the div.

Paragraph 3. Not in a div.

Paragraph 4. Not in a div.


ចំពោះ​វិធី General Sibling Selector វិញ គឺ​ត្រូវ​ធ្វើ​ឡើង​តាម​រយៈ​ធាតុណាមួយ ដោយ​ប្រើប្រាស់​សញ្ញា ~ ដូច​ខាង​ក្រោម​នេះ៖

<!DOCTYPE html>
<html>
  <head>
  	<meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  	<title>CSS Combinator</title>
    <style>
      div ~ p {
  		background-color: yellow;
	  }
    </style>
  </head>
  
  <body>
	

Paragraph 1.

Paragraph 2.

Paragraph 3.

Some code.

Paragraph 4.

</body> </html>

Paragraph 1.

Paragraph 2.

Paragraph 3.

Some code.

Paragraph 4.