Styling underlines using floats in CSS pseudo-after.How to

My headings have underlines created using pseudo :after elements. However, when these headings are displayed next to a floated image/div, the underline shifts over the image/div.

h2:after {
   content: '';
  position: relative;
  max-width: 100px;
  display: block;
  height: 4px;
  background: #0073ae;
}

You can view a brief demonstration of this issue on Codepen: http://codepen.io/costelc/pen/GqgdvB

If you have any insights or solutions, please share. Thank you!

Answer №1

Floats push elements out of the flow, which is why the header overlaps with them. To prevent this, creating a block formatting context is necessary.

An effective method is to set overflow to anything other than visible, for example:

h2 {
  overflow: hidden;
}

As stated in CSS 2.1 Floats,

Since a float is not in the flow, non-positioned block boxes created before and after the float box flow vertically as if the float did not exist.

The border box of a table, a block-level replaced element, or an element in the normal flow that establishes a new block formatting context (like an element with overflow other than visible) should not overlap the margin box of any floats within the same block formatting context

body {
  max-width: 300px;
}
.right {
  width: 100px;
  height: 100px;
  background: #eee;
  float: right;
  margin: 0 0 0 20px;
}
.clear {
  clear: both;
}
.left {
  width: 100px;
  height: 100px;
  background: #eee;
  float: left;
  margin: 0 20px 0 0;
}
h2 {
  overflow: hidden;
}
h2:after {
  content: '';
  position: relative;
  max-width: 100px;
  display: block;
  height: 4px;
  background: #0073ae;
}
<h2>Good heading here</h2>
<div class="right"></div>
<h2>Another good heading here</h2>
<p>anything here</p>
<br class="clear">
<div class="left"></div>
<h2>Bad heading here</h2>
<p>anything here</p>

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

I created some jQuery code that modifies a button when it is hovered over, however, I ended up writing the code individually for each button. What steps can I take to turn it

Is there a way to turn the code for each button on my website into a jQuery function that can be applied to any button? This is how the code currently appears: $(document).ready(function() { $("#linkXML").hover( function() { ...

HTML5's video tags are capable of displaying video content without audio playback functionality

My current project involves importing videos using PHP, utilizing a video tag. However, I've encountered an audio issue while doing so. I've tried various codes such as: <video controls> <source src="https://www.w3schools.com/html/mov_ ...

Rotating an HTML caret using CSS does not pivot from the center point

Is there a way to adjust my simple caret rotation to make it rotate from the center? What changes should I make? const Wrap = styled.div` width: 100px; background: grey; span { display: block; transform: ${(props) => (props.isOpen ? " ...

Struggling to achieve the desired layout in the navigation code using CSS Grid. I am aiming for it to occupy the entire width of the screen

I'm facing an issue with customizing my HTML layout using grids. I want my nav element to occupy the entire available width. Below is my current code: header { text-align: center; grid-area: header; border: solid; } #nav1 { text-de ...

Guide on integrating animate.css animations with Vue's Transition and TransitionGroup components

Vue offers the v-if and v-for directives that allow you to manipulate elements in the DOM based on certain conditions. In order to animate elements controlled by v-if and v-for, you need to utilize the built-in Transition and TransitionGroup components. Bu ...

Troubleshooting Alignment Problems in Bootstrap 4 Tables

Having trouble aligning certain items. In thisFiddle, I have two options: Option1 and Option2. I want to align the toggle switches with the ones in the first two rows. Specifically, I want the toggle switches to be in the second column of the table and t ...

Revamping Sign-out Design in AdminLTE with ASP.NET

For my dashboard, I am utilizing the fantastic AdminLTE. However, I am facing a small styling issue with the logout functionality as it needs to be a form in ASP.NET Core MVC. Does anyone have any suggestions on how to use a normal anchor tag instead of t ...

Tips on utilizing multiple dynamically generated toggle switches efficiently

As a Frontend beginner, I am working on implementing toggle switches using HTML, CSS, and Vanilla JavaScript. Approach: I am generating an HTML table with multiple rows based on the data from my Django database. Each row contains details like name, id, a ...

Creating HTML that adjusts to the size of the viewport

After completing my first webpage, I implemented a parallax/split scroll effect using ScrollMagic.js. However, when testing the page in mobile view using Chrome's developer tools, I noticed that the parallax effect was not working properly due to mis ...

Changed static divs into flexible divs

I am currently working on designing a webpage layout where the header occupies 100% of the width and 20% of the height in conjunction with a left navbar (20% width) and a main body adjacent to it (80% width). Additionally, I want the page to be responsive ...

Inconsistent expansion panel widths observed in Chrome when using React material-ui

I've encountered an issue in Chrome where the width adjusts to fit its content instead of taking up the full width of the container. However, this problem does not occur on Firefox. https://i.stack.imgur.com/HyWGU.png When I click on the expand icon ...

Adding color to the header's top section and leaving the navigation bar untouched

A few days ago, I posted a question on this platform regarding customizing CSS related to search forms within Bootstrap. The response I received from Charlie was incredibly helpful in refining the code for my header and navigation elements. You can view hi ...

The anchor tag dwarfs the element it contains

I'm experiencing an issue on my website where the clickable area of my two images/buttons, labeled Get Started and Learn More, is larger than the actual image itself. I've wrapped the images with anchor tags to link to separate pages, but for som ...

Having trouble with images not showing up properly when applying rounded CSS borders and opacity effects

Trying to solve a unique problem, I am seeking advice: On my website, I have included a 300 x 300 Pixel square png-image of a circle. Utilizing Zurb's Foundation 5.0.2 as a CSS Grid basis. My objective is to create a CSS-border around the circle and ...

"Using enchant.js to create a game that utilizes an if statement to show a specific

I am looking to implement an if statement into my game that will display an html div (#game-over) once a certain score is reached. The div is currently set to "display:none". I have created a js fiddle for the game with the code $('#game-over'). ...

What is the best way to target elements that have the contentEditable attribute set to true using CSS?

Can someone help me style table cells that have contentEditable set to "true"? I've tried various CSS selectors like td[contenteditable], td[contenteditable=true], and td[contenteditable="true"], but none seem to work. Is there a specific selector for ...

Images failing to load properly

Hi there, I'm new to programming and I'm in the process of creating a website. However, when I open my HTML file directly, the images are not displayed. Strangely, when I use VS Code or Brackets editor's live server, all the photos and pictu ...

Tips for altering the color of an image using CSS attributes

I am looking to create a male Head component in this design draft and modify the CSS according to the skin prop. I have attempted to use filter and mix-blend-mode properties, but have not found a solution yet. Any guidance on how to achieve this would be ...

Issue with z-index not applying to a fixed header

I've been attempting to recreate the problem using this plunker. .demo-blog.mdl-layout .mdl-layout__content { padding-top: 0px; position: relative; margin-top: -80px; z-index: 100; } header.main-header{ z-index: -50; } My goal is ...