The First-Of-Type selector for div elements becomes ineffective when used after a div with the clear:both property

My issue is with the positioning of the first DIV in my container. Even though it should be styled using :first-of-type, it's not appearing as expected.

This problem arises after inserting

<div class="horizontal_line"></div>

right before the first instance of

<div class="field_container">
     <div class="field_icon"></div>
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse luctus eget lectus at convallis.</p>
</div>

the icon suddenly disappears and the entire dive shifts down, losing its first-of-type styling.

I want it to appear like this: IMAGE

View the jsFiddle for my page

This affects the common style across all boxes, as well as the specific style for the first one

.field_container
    {
    width: 216px;
    margin-left: 28px; 
    display: inline-block;
    float: left;
    text-align: justify;
    }
.field_container .field_icon
    {
    height: 80px;
    margin: 0 auto;
    background-repeat: no-repeat;
    }
.field_container:first-of-type
    {
    margin-left: 0px;
    }
.field_container:first-of-type .field_icon
    {
    background-image: url('images/front_end.png');
    width: 83px;
    }

and the style of the horizontal line that is causing the issue

.horizontal_line
    {
    width: 100%;
    margin: 25px 0px 25px 0px;
    height: 1px;
    clear: both;
    background: #1C788B;
    }

Answer №1

first-of-type is a selector designed for targeting the first occurrence of a specific element type (p, div, etc). It cannot be used to target elements based on their class or ID. The key thing to understand is that when you introduce a new element of the same type (e.g., adding a horizontal_line div), it becomes the new "first type of that element" in the selection order.

  • How to use CSS3 selector :first-of-type with a class name?

If you need to target specific instances within a container, consider using the :nth-child selector instead:

Learn more about :nth-child selector here

.field_container div:first-of-type {
    /* Selects the first div within .field_container */
}
.field_container {
    /* Remove left margin */
}
.field_container + .field_container {
    /* Apply left margin to adjacent .field_container */
    margin-left: 28px;
}
.field_container:nth-child(2) .field_icon {
    /* Customize the icon for every other nth-child() in the container */
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating an empty column in each succeeding row of a grid layout

Is there a way to create a grid layout with divs that leave space on the left side of each subsequent row? For example, having 4 div boxes in the first row, 3 in the second, 2 in the third, and so on. Demo https://i.sstatic.net/Gli6L.png Styling in CSS ...

“Tips for positioning text in the middle using HTML and CSS”

I'm trying to customize the text "TITLE" in my HTML and CSS code, #headerf { background-color: #717571; overflow-x: hidden; position: fixed; z-index: 99999; } .headert { position: fixed; z-index: 9999999; width: 100%; height: 60px; ...

How can I customize the pull-down menu options depending on the selected checkbox setting?

Here are the menu options I have defined: <label><strong>Release #1:</strong></label> <select id="selectedBaseRelease"> <option value="/~releases/file2">ICC2_O-2018.06</option> <option v ...

Tips for preserving HTML content in an XML file with Linq to XML

I have been experimenting with Linq to XML in order to save and retrieve HTML content between an XML file and a Windows Forms application. However, when the HTML is saved to the XML file, the tags get encoded and it doesn't retain its original form. ...

React Input increases the total width by 22 pixels

My React input has a CSS setting of 'width: 300px'. However, when displayed on the screen, the width appears to be '322px'. Below is the CSS code for the input: input { width: 300px; padding: 15px 10px; border: 1px #818181 solid; ...

I require assistance in troubleshooting and repairing my HTML and JavaScript code

I am working on creating a feature where users can input their upcoming birthday date and receive an alert showing how many days are left until then. However, I encountered an issue where the alert displays NaN (Not a Number) before the birthday is entered ...

What is the best way to verify an email address within a form using bootstrap styling

Looking to validate the submitted email for accuracy. Attempting to implement this validation using Bootstrap due to requirements. Please review the codepen here: https://codepen.io/yytimo/pen/ExWLbLd <div class="col-md-4"> ...

Using JavaScript to control the state of a button: enable or

In the process of creating a basic HTML application to collect customer information and store it in a database, I have encountered a specific user interface challenge. Once the user logs into their profile, they should see three buttons. Button 1 = Print ...

What could be causing my THREE.js Documentation Box to malfunction?

I am a newcomer trying to get the hang of THREE.js. I delved into the THREE.js Documentation and attempted to implement the code, but when I loaded my HTML page, it appeared blank. I am at a loss for what to do next. I utilized Visual Studio Code for codin ...

Unable to invoke function on HTML element

Recently, I've ventured into writing jQuery-like functions in Vanilla JS. While my selectors seem to be working fine, I encounter an "is not a function" error when trying to call the append function on an HTML element. var $ = function(){ this.se ...

Adjusting Bootstrap card content on hover

Currently, I am in the process of developing a website that features a list of products presented in bootstrap cards. I am seeking advice on how to dynamically change the text displayed on these cards when a user hovers over them. Specifically, I want to ...

Steps for Loading HTML5 Video Element in Ionic 2

Struggling to showcase a list of videos from my server, here's the issue I'm encountering and the layout of the app. https://i.stack.imgur.com/HWVt3.png This is the code snippet in HTML: <ion-list> <button ion-item *ngFor="let v ...

Extracting information from an HTML table with Jsoup

Currently, I am attempting to utilize jsoup in order to parse an HTML table. As a newcomer to jsoup, I have taken the time to review some tutorials on how it operates. My objective is to retrieve values from each column within this specific website's ...

Arrange matching divs from two columns on the same row

My webpage features 2 columns: The first column displays questions along with their previous answers, while the second column also shows the same questions but with input fields for new answers. This setup is designed for comparison purposes. However, due ...

Why doesn't the z-index of child elements function properly when their fixed parents share the same z-index value?

I have utilized jsfiddle to replicate my problem. My goal is to position .top .inside above .bottom .inside. I am aware that z-index only functions within its respective position type, for example fixed and absolute do not share the same z-index level. How ...

Sending information from a text area to a PHP script and receiving a response

While it may seem simple, I am still in the process of learning. I have a textarea where I input data that I want to send to a PHP script for decryption. Below is my HTML: <html> <form action="php.php" method="post"> ...

Position fixed property

While experimenting with the fixed position property to maintain a constantly positioned nav bar on mobile, I encountered some challenges during the trial and error process. However, by utilizing margin top and translation functions, I was able to successf ...

Securing PayPal's purchase forms for safe online transactions

Trying to assist someone with a donation issue, but discovered that the source code allows for editing of values in the Paypal button. When selecting 5 points worth $5, it's possible to manipulate the values in the source code of the Paypal form. How ...

Bootstrap 5: Position the cards in close vertical alignment with one another

For my latest project, I have been working on creating a responsive card collection using Bootstrap 5 and Vue 3. By utilizing the container and row classes, I was able to ensure that all cards are aligned at the same level. However, in order to keep the ti ...

How to efficiently pass multiple inputs from an HTML form to a controller in AngularJS using a single ng

Currently, I have multiple input text boxes in my HTML and I need to send their values to my controller to add them to an object. The challenge I'm facing is figuring out how to pass more than one value using just one ng-model. In my HTML code, I have ...