HTML/CSS MCQ

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

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 <input> elements that have a defined (non-empty) value?

A input:not(:empty) {}

B input[value] {}

C input[value!=""] {}

D input:not(:blank) {}

B
The selector input[value] targets all <input> elements that have a defined value attribute, which means they have a non-empty value.
 

2. Which selector targets the first <li> element in a list group that is also a <strong> element?

A ul > li:first-child strong {}

B ul > li:first-of-type strong {}

C ul > li:first-child:strong {}

D ul > li > strong:first-child {}

B
The selector ul > li:first-of-type strong selects the first <li> element in a list that contains a <strong> element. The :first-of-type pseudo-class targets the first <li> child.
 

3. Which CSS selector targets a <span> element that contains exactly three words (using spaces as separators)?

A span:contains("word word word") {}

B span:words(3) {}

C span { word-count: 3; }

D This selector does not exist in pure CSS.

D
In CSS, there is no selector that can directly target an element based on the number of words it contains. This would require JavaScript or a CSS preprocessor.
 
 

4. Which CSS selector targets an <input> element whose value is exactly “email” (case-sensitive)?

A input[value="email"] {}

B input[value="email"]:exact {}

C input[value="email"] {}

D input[value~="email"] {}

A
The selector input[value="email"] selects all <input> elements with the value attribute equal to “email”. This is a case-sensitive exact match.
 

5. Which selector targets all <h1>, <h2>, and <h3> elements that are within a <header> container?

A header h1, header h2, header h3 {}

B header > h1, h2, h3 {}

C header:h1, header:h2, header:h3 {}

D h1, h2, h3 > header {}

A
The selector header h1, header h2, header h3 targets all <h1>, <h2>, and <h3> elements that are descendants of a <header> element. This helps style headings in the page header.
 

6. Which CSS selector targets all <li> elements that do not contain an <a> element inside?

A li:has(:not(a)) {}

B li:not(a) {}

C li:empty {}

D li:not(:has(a)) {}

D
The selector li:not(:has(a)) targets <li> elements that do not contain any <a> elements inside. It uses the :has() pseudo-class to select elements based on their children.
 
 

7. Which CSS selector targets all <p> elements that are immediately followed by a <footer> element?

A p + footer {}

B footer + p {}

C p ~ footer {}

D footer ~ p {}

A
The selector p + footer selects a <footer> element that immediately follows a <p> element. The + combinator means “immediately adjacent”.
 

8. Which CSS selector targets a <input> element of type text when its content is empty?

A input[type="text"]:empty {}

B input[type="text"]:not(:empty) {}

C input[type="text"]:value="" {}

D input[type="text"]:placeholder-shown {}

D
The selector input[type="text"]:placeholder-shown targets a text input element showing its placeholder text, which implies the field is empty. This is often used to style empty text fields.

 

 

9. Which selector targets a <p> element within a <div> whose text is bold?

A div p { font-weight: bold; }

B div p:font-weight(bold) {}

C div p b {}

D div > p:has(b) {}

C
The selector article p b targets all <b> elements inside a <p> within a <div>. This implies the text is bold due to the <b> tag, but it does not account for font-weight set directly on the <p>.
 
 

10. Which CSS selector targets all <span> elements inside a <p>, but only within the last paragraph on the page?

A p:last-of-type span {}

B p span:last-child {}

C p:last-child span {}

D p span:last-of-type {}

A
The selector p:last-of-type span targets all <span> elements inside the last <p> element of their parent. The :last-of-type pseudo-class selects the last paragraph of its type.
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 *