Uteuzi wa Kipengele cha Juu katika CSS- Mbinu na Mifano

Madarasa ya uwongo

Madarasa ya uwongo hukuruhusu kuchagua hali au nafasi maalum za kipengee. Kwa mfano, :hover huchagua kipengele wakati pointer ya panya iko juu yake, :focus huchagua kipengele wakati imechaguliwa au ina lengo, :nth-child() huchagua kipengele maalum cha mtoto katika kikundi.

Mifano:

/* Select all links when hovered over and change the text color */  
a:hover {  
  color: red;  
}  
  
/* Select the <input> element when it is focused and change the border */  
input:focus {  
  border: 2px solid blue;  
}  
  
/* Select the second element in a group of <li> elements and change the text color */  
li:nth-child(2) {  
  color: green;  
}  

 

Vipengee vya uwongo

Vipengele vya uwongo hukuruhusu kuunda vipengee pepe ili kubinafsisha kipengee kilichopo.

Kwa mfano, ::before na ::after unda vipengele kabla na baada ya kipengele, ::first-line na ::first-letter uchague mstari wa kwanza na herufi ya kwanza ya kipengele.

Mifano:

/* Add content before each <p> element and style it */  
p::before {  
  content: ">> ";  
  font-weight: bold;  
  color: gray;  
}  
  
/* Style the first letter of <h1> element */  
h1::first-letter {  
  font-size: 2em;  
  font-weight: bold;  
  color: red;  
}  

 

Wachanganyaji

Wachanganyaji hukuruhusu kuchanganya wateule ili kuchagua vipengee kulingana na uhusiano wao. Kwa mfano, element1 element2 huchagua element2 ndani element1, element1 > element2 huchagua vipengele vya mtoto moja kwa moja vya element1, element1 + element2 huchagua element2 mara baada ya element1.

Mifano:

/* Select <span> elements inside an element with class "container" */  
.container span {  
  color: purple;  
}  
  
/* Select <li> elements that are direct children of <ul> */  
ul > li {  
  list-style-type: square;  
  color: blue;  
}  

 

Viteuzi vya sifa

Viteuzi vya sifa hukuruhusu kuchagua vipengee kulingana na thamani ya sifa zao. Kwa mfano, [attribute] huchagua vipengele vilivyo na sifa attribute, [attribute=value] huchagua vipengele vilivyo na sifa attribute sawa na value, [attribute^=value] huchagua vipengele vyenye sifa attribute inayoanza na value.

Mifano:

/* Select all elements with the attribute data-type */  
[data-type] {  
  font-weight: bold;  
  color: orange;  
}  
  
/* Select all <a> elements with the href attribute starting with "https://" */  
a[href^="https://"] {  
  color: blue;  
  text-decoration: underline;  
}  

 

:not() kiteuzi

Kiteuzi hukuruhusu kuchagua vipengee ambavyo havilingani na kiteuzi maalum. Kwa mfano, huchagua vipengele ambavyo havina darasa, huchagua vipengele ambavyo havina kitambulisho. :not() :not(.class) class :not(#id) id

Mifano:

/* Select all <div> elements that do not have the class "hidden" */  
div:not(.hidden) {  
  display: block;  
  background-color: lightgray;  
}  
  
/* Select all <input> elements that do not have the ID "email-input" */  
input:not(#email-input) {  
  border: 1px solid gray;  
}  

 

Mifano hii inaonyesha uteuzi wa vipengele vya kina katika CSS. Unaweza kubinafsisha na kutumia mbinu hizi ili kuunda na kubinafsisha vipengee kwenye ukurasa wako wa wavuti unavyotaka.