Enhance web design with dynamic size pseudo elements using only CSS, no

Before the title, I've added a pseudo element with a slanted background. When the title is wrapped, it increases in height. I've been attempting to make the pseudo element adjust to the title's height, but haven't succeeded yet.

I've experimented with various combinations of flex / (inline)block and absolute positioning, but have not been able to make it function correctly.

I want it to be fully responsive without relying on JavaScript.


The image below shows the title when it consists of only one line:

  • Orange: background color of the title wrapper
  • Purple: pseudo element positioned in front of the title
  • Green: the title text

https://i.stack.imgur.com/AnFCE.png


When I resize the screen and the title wraps, it appears like this:

  • As seen in the image, the purple element is too small

https://i.stack.imgur.com/DS2AS.png


If I adjust the z-index, you can see the purple element: https://i.stack.imgur.com/peiIW.png


My desired outcome is for the purple element to always be at the left side of the title, covering the full height of the title to display the slanted effect. https://i.stack.imgur.com/Hegvt.png

.title {
  display: flex;
  background-color: orange;
}

.title::before {
  flex-shrink: 0;
  height: 100%;
  width: 50px;
  content: '';
  transform: skew(-22.5deg);
  transform-origin: 0 100%;
  background-color: purple;
}

.title span {
  background-color: green;
}
<div class="title">
  <span>Some text for the title</span>
</div>

Answer №1

My innovative approach utilizes SVG vectors instead of pseudo elements to achieve the desired effect.

By adjusting the height of the .title element, you can maintain consistency in appearance.

.title {
  display: flex;
  background-color: green;
  position: relative;
  height: 50px;
  overflow: hidden;
}

.module_title span {
  background-color: green;
}

.title span {
  padding: 5px 60px;
  color: white;
}

svg {
  position: absolute;
  left: 0px;
  height: 100%;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8>
    <title>z</title>
    <link rel='stylesheet' href="/mysite/static/.other/z.css">
  </head>
  <body>

    <div class="title">
      <svg class="orange" width="80" viewBox="0 0 100 100" preserveAspectRatio="none">
        <polygon id="cover" points="0,100 0,0 50,0" style="fill: orange"></polygon>
      </svg>
      <svg class="purple" width="80" viewBox="0 0 100 100" preserveAspectRatio="none">
        <polygon id="cover" points="30,0 60,0 30,100 0,100" style="fill: purple"></polygon>
      </svg>
      <span>Some text for the title</span>
    </div>

  </body>
</html>

Answer №2

To implement the inherit value for height in your pseudo element, you can follow this example:

.header:before {
  flex-grow: 0;
  height: inherit;
  width: 70px;
  content: '';
  transform: rotate(-30deg);
  transform-origin: top left;
  background-color: blue;
}

Answer №3

While I appreciate @RobKwasowski's approach, achieving this effect does not necessarily require svg. Most of the styles used here are similar to yours, except I opted not to utilize flex. It is worth noting that the .title contains

position:relative;overflow:hidden;
. The reason for using position:relative; is to position the before element with position:absolute;, while overflow:hidden; ensures any extra content is clipped.

I trust this solution proves helpful.

.title {
  background-color: orange;
  padding:0 0 0 3em;
  position:relative;
  overflow:hidden;
}

.title::before {  
  display:block;
  position:absolute;
  top:0;left:0;
  height: 100%;
  width: 50px;
  content: '';
  transform: skew(-22.5deg);
  transform-origin: 0 100%;
  background-color: purple;
}

.title span {
  display:inline-block;
  padding:2em 0 2em 5em;
  width:100%;
  background-color: green;
}
<div class="title">
  <span>Some text for the title<br>Some text for the title<br>Some text for the title</span>
</div>

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

The HTMLEditor from ASP.NET AJAX Control Toolkit is not appearing correctly on the page

My experience with the ASP.NET AJAX Control Toolkit HTMLEditor has been less than satisfactory. The toolbar layout is not being displayed correctly, with what should be a 3-line toolbar appearing stretched out to 9 lines. ...

Resolving issues with JavaScript caused by Polymer updates

I am a novice when it comes to working with Polymer. From what I have gathered, there seems to be compatibility issues with Mozilla and Safari. After researching on StackOverflow, I found that adding addEventListener('WebComponentsReady', funct ...

What could be causing my website to extend beyond 100% width?

I've been working tirelessly on solving this issue for the past week, but unfortunately, I haven't had any luck finding a solution. The problem arose as I was designing a launch page for a program that my friend is developing. Despite setting the ...

Is it possible to merge two HTML selectors using jQuery?

In my HTML, I have two button elements defined as follows: <input type="button" class="button-a" value="Button a"/> <input type="button" class="button-b" value="Button b"/> When either of these buttons is clicked, I want to trigger the same ...

What causes <input> elements to vertically center when using the same technique for centering <span> elements? (The line-height of <input> does not match its parent's height)

Important: The height of <div> is set to 50px, and the line-height of <span> is also 50px When 'line-height' is not applied to the <span>, all elements align at the top of the parent. However, when 'line-height: 50px&apo ...

How to Align position:relative Divs Horizontally Using CSS

My goal is to showcase content from my website located at . I want to have the ability to place this content into a designated div where I can easily center or style it as needed. The issue arises when attempting to float the content, as it doesn't fu ...

Is there a way to maintain the selected position on the drop-down menu for users?

Whenever I choose an option from the drop-down field in my form, it automatically jumps back to the top of the page. Just to clarify, I have a drop-down menu at the top of the page and several input fields below. Users need to scroll down to reach the dro ...

Arrange the table in alphabetical order and categorize it

Hello there! I am currently working on creating a well-organized table list where names are sorted into specific categories based on their starting letter. For example, names beginning with 'A' will be placed in the 'A category', while ...

The custom classes I have created in Material UI are being overshadowed by the default classes provided by Material UI

I am trying to change the color of a TextField label to black when it is focused. I used classes in InputProps with a variable, but unfortunately, the default styling of material UI is taking precedence in chrome dev tools. Below is the code snippet. Pleas ...

In an HTML document, you may encounter whitespace that is not present in JSFiddle

Upon running this HTML file in my browser, I noticed that there is an 18px gap at the top of the first div. It's strange because this issue only occurs when I run the HTML locally on my browser, but everything works fine on JSFiddle. I'm puzzled ...

Using Angular JS to retrieve specific information from a JSON file

I am in the process of developing an AngularJS app that showcases notes. The notes are stored in a note.json file, and the main page of the app only displays a few fields for each note. I want to implement a feature where clicking on a specific note opens ...

The background image in the hovering textbox shifts and changes size

I've been trying to set a background image on a Kendo UI Textbox. It looks good until you hover over it. But when you do hover over it, this issue arises: How can I resolve this? The background image should remain in place even when I hover and cli ...

Is there a way to make my HTML file search beyond its current directory?

My HTML file seems to be having trouble searching in a different folder for the CSS file that I've linked. How can I make it search in the correct folder? I have my HTML file in a directory named INDEX and the CSS file in another directory named STYL ...

Utilize JavaScript to target the specific CSS class

Hello, I am currently using inline JS in my Wordpress navigation menu, redirecting users to a login page when clicked. However, I have been advised to use a regular menu item with a specific class and then target that class with JS instead. Despite searchi ...

The responsiveness of Slick Slider's breakpoints is malfunctioning

After implementing a slider using slick slider, I encountered an issue with the width when testing it online and in a normal HTML file. If anyone could assist me in resolving this issue, I would greatly appreciate it. Please inspect the code for both scen ...

Tips for presenting random images from an assortment of pictures on a webpage

I'm looking to enhance my website by adding a unique feature - a dynamic banner that showcases various images from a specific picture pool. However, I'm unsure of how to find the right resources or documentation for this. Can you provide any guid ...

Click event not triggering to update image

I'm currently working on a Codepen project where I want to use JavaScript to change the image of an element with the id "chrome". However, my code doesn't seem to be working as expected. Can someone help me troubleshoot and fix this issue? Your a ...

Obtaining the text of a span tag within a div using Selenium in C#

<div id="uniqueSummaryID" class="custom-text" data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$unique_key_25.1.1.0"> <span data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$unique_key_25.1.1.0.0">5</span> <span data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$unique_ke ...

Choose the field and set the default value

In my current project, I have a page dedicated to listing all categories with links to individual category pages for modification purposes. On the category page, I want to include a Select field for the Category Name, displaying all preset categories with ...

Can you explain the contrast between onsubmit="submitForm();" and onsubmit="return submitForm();"?

Is it possible that the form below is causing double submissions? <form name="myForm" action="demo_form.asp" onsubmit="submitForm();" method="post"> function submitForm(){ document.myForm.submit(); } I've noticed a bug where sometimes two ...