HTML/CSS MCQ

HTML/CSS MCQs – Multiple Choice Questions and Answers – Part 58

Multiple choice questions and answers (MCQs) on HTML/CSS to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as tags, web standards, CSS selector, objects, and more. This quiz will easily prepare anyone to pass their online test.
 
 

1. Which CSS selector targets all <div> elements with the class card that contain a <p> with the class description and whose text includes the word “promotion”?

A div.card p.description:contains("promotion") {}

B div.card p.description {}

C div.card p.description:has(:contains("promotion")) {}

D div.card p.description { content: "promotion"; }

A
The selector div.card p.description:contains("promotion") targets <p> elements inside a <div> with the class card, where the paragraph text contains the word “promotion”. Although the pseudo-selector :contains() is useful in jQuery, it is not standard in pure CSS. However, it works in some implementations or libraries.
 

2. Which CSS selector targets all <input> elements in a form with the ID feedback-form that have a placeholder attribute containing the word “Commentaire”?

A #feedback-form input[placeholder*="Commentaire"] {}

B #feedback-form input[placeholder^="Commentaire"] {}

C #feedback-form input[placeholder$="Commentaire"] {}

D #feedback-form input[placeholder="Commentaire"] {}

A
The selector #feedback-form input[placeholder*="Commentaire"] targets all <input> elements in the #feedback-form whose placeholder attribute contains the word “Commentaire”. The *= means partial match in attribute value.
 

3. Which CSS selector targets all <span> elements that are immediately followed by an <img> in a section with the class gallery?

A .gallery img + span {}

B .gallery span img + {}

C .gallery span + img {}

D .gallery span > img {}

A
The selector .gallery span + img targets all <img> elements immediately following a <span> inside a section with the class gallery. The + is an adjacent sibling selector.
 
 

4. Which CSS selector targets all <div> elements whose class starts with “alert-” and are immediately followed by a <span> element?

A div[class^="alert-"] + span {}

B div[class*="alert-"] + span {}

C div.alert- + span {}

D div[class^="alert-"]:not(span) {}

A
The selector div[class^="alert-"] + span targets all <span> elements immediately following a <div> whose class starts with "alert-". The ^ is used to match elements whose class attribute starts with “alert-“.
 

5. Which CSS selector targets an <h2> element that is immediately preceded by a <div> with the class header?

A .header + h2 {}

B div.header + h2 {}

C h2 + .header {}

D .header h2 + {}

B
The selector div.header + h2 targets all <h2> elements that immediately follow a <div> with the class header. The + is an adjacent sibling selector.
 

6. Which CSS selector targets all <a> elements in a <ul> list that are the last children of a <li>?

A ul li a:last-child {}

B ul li a:last-of-type {}

C ul li a:last-child:link {}

D ul li a:last-of-type:link {}

B
The selector ul li a:last-of-type targets the last <a> element within a <li>, regardless of its position among other child elements. The :last-of-type selects the last of a given element type.
 
 

7. Which CSS selector targets all <li> elements that are children of a <ul> with the data-type attribute equal to “main”?

A ul[data-type="main"] li {}

B ul[data-type="main"] > li {}

C ul[data-type="main"] li:first-child {}

D ul li[data-type="main"] {}

A
The selector ul[data-type="main"] li targets all <li> elements that are children of a <ul> with the data-type attribute equal to “main”.
 

8. Which CSS selector targets all <ul> elements that do not contain any <li> elements?

A ul:empty {}

B ul:has(li) {}

C ul:not(:has(li)) {}

D ul:not(li) {}

C
The selector ul:not(:has(li)) targets all <ul> elements that do not contain any <li> elements. The :has() checks if an element contains a given child, and :not() negates it.
 

9. Which CSS selector targets all <p> elements that are direct children of a <section> and have uppercase text?

A section > p { text-transform: uppercase; }

B section p { text-transform: uppercase; }

C section > p:first-child { text-transform: uppercase; }

D section p.uppercase { text-transform: uppercase; }

A
The selector section > p targets all <p> elements that are direct children of a <section>. The style text-transform: uppercase; converts their text to uppercase.
 
 

10. Which CSS selector targets all <p> elements inside an <article> with the class post and applies uppercase to their first letter?

A .post article p:first-letter { text-transform: uppercase; }

B .post article p:first-letter {}

C .post article p::first-letter { text-transform: uppercase; }

D .post p::first-letter { text-transform: uppercase; }

C
The selector .post article p::first-letter { text-transform: uppercase; } targets all <p> elements inside an <article> with the class post and applies a style to their first letter (using ::first-letter), transforming it to uppercase.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *