What steps can I take to have the text extend beyond the background at both the top and bottom edges?

I am attempting to replicate this design using CSS:

https://i.sstatic.net/zXInr.png

So far, this is what I have achieved:

.container{
  height: 30px;
  margin-bottom: 10px;  
}
.redLine{
  background-color: red;
  opacity: 0.5;
  height: 20px;  
  border-radius: 5px;
  text-align: center;      
}
.centered{  
  font-size: 30px;  
}
<div class="container">
    <div class="redLine">
        <h2 class="centered">Streaming</h2>    
    </div>                        
</div>

How do I achieve this CSS effect?

Answer №1

To achieve the desired effect of cutting the upper and lower part, you can use the clip-path property without the need for extra elements or pseudoelements.

h2 {
   background: #f16c73;
   color: #fff;
   font: 2.4em Arial;
   font-style: italic;
   font-weight: 800;
   text-align: center;
   letter-spacing: 2px;
   clip-path: polygon(20% 33%, 20% 70%, 80% 70%, 80% 33%);
 }
<h2>STREAMING</h2>

Answer №2

Consider exploring the use of line-height in this creative idea:

h2 {
   background: #f16c73;
   color: #fff;
   font: 2.4em Arial;
   font-style: italic;
   font-weight: 800;
   text-align: center;
   letter-spacing: 2px;
   line-height:0.45; 
   /*overflow:hidden; uncomment this if you want to hide the overflow */
 }
<h2>STREAMING</h2>

Here's another suggestion to customize the background:

h2 {
   color: #fff;
   font: 2.4em Arial;
   font-style: italic;
   font-weight: 800;
   text-align: center;
   letter-spacing: 2px;
   background: 
    linear-gradient(#f16c73,#f16c73) center
    /100% 45%  /* Adjust this value */
    no-repeat;
 }
<h2>STREAMING</h2>

Answer №3

To achieve the desired effect, I recommend using white text with a white background instead of opacity. To vertically position the text, you can utilize the position: relative property along with the bottom setting to fine-tune its placement.

.container {
  height: 30px;
  margin-bottom: 10px;
}
.redLine {
  background-color: red;
  height: 24px;
  border-radius: 5px;
  text-align: center;
}
.centered {
  font-family: sans-serif;
  font-style: oblique;
  font-size: 50px;
  color: white;
  position: relative;
  bottom: 8px;
}
<div class="container">
  <div class="redLine">
    <h2 class="centered">STREAMING</h2>
  </div>
</div>

Answer №4

Another approach is to use the transform property in CSS to reposition the text.

.container {
  height: 30px;
}

.redLine {
  background-color: rgba(255, 0 ,0, 0.5);
  opacity: 0.5;
  height: 20px;
  border-radius: 5px;
  text-align: center;
  overflow: hidden;
}

.centered {
  font-size: 70px;
  line-height: 70px;
  color: white;
  margin: 0;
  padding: 0;
  transform: translateY(-40%);
}
<div class="container">
  <div class="redLine">
    <h2 class="centered">Streaming</h2>
  </div>
</div>

Answer №5

Adjust the text color to transparent and apply an italic font style.

    
    .centreado{  
      font-size: 60px;  
      color: transparent;
      font-style: italic;
    }

Shift the position of the div downwards.


.contenedor{
  height: 30px;
  margin-bottom: 10px;  
  transform: translateY(20px); /*Modify this value as needed */
}

Answer №6

Sample HTML code:

<div class="container">
  <div class="redLine">
    <h2 class="centered">Streaming</h2>
  </div>
</div>

Corresponding CSS:

.container {
  height: 30px;
  margin-bottom: 10px;
}

.redLine {
  position: relative;
  background-color: red;
  opacity: 0.5;
  height: 20px;
  border-radius: 5px;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 38px;
  text-transform: uppercase;
  font-style: italic;
  margin: 0
}

Link to view on JSfiddle: https://jsfiddle.net/hansfelix50/q0zxh9ku/

Answer №7

One trick to note is setting overflow: hidden; on the red line element, which will clip any content that overflows it.

.container {
  height: 30px;
  margin-bottom: 10px;
}

.redLine {
  background-color: red;
  opacity: 0.5;
  height: 20px;
  border-radius: 5px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.centeredText {
  font-size: 55px;
  position: absolute;
  width: 100%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
  margin: 0;
  color: #fff;
  text-transform: uppercase;
}
<div class="container">
  <div class="redLine">
    <h2 class="centeredText">Streaming</h2>
  </div>
</div>

Answer №8

To achieve this effect easily, utilize css flexbox.

Instructions:

  1. Remove the opacity: 0.5; and text-align: center; from .lineaRoja
  2. Add background-color: #f36167; to .lineaRoja
  3. Add
    display: flex; align-items: center; justify content: center;
    to .lineaRoja
  4. Customize the h1 style accordingly; for example:
    letter-spacing: 2px; font-size: 55px; font-family: arial; font-style: italic; color: white;

Below is a simple illustration:

body {
  background-color: grey;
  padding-top: 50px;
}
.lineaRoja {
  background-color: #f36167;
  height: 20px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.centreado {
  letter-spacing: 2px;
  font-size: 55px;
  font-family: arial;
  font-style: italic;
  color: white;
}
<div class="lineaRoja">
  <h1 class="centreado">STREAMING</h1>    
</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

IE11 not running Angular code inline as expected

Currently in the process of developing an AngularJS application, I came across a strange issue that I have never encountered before. It may be a simple fix, but I am unsure of the exact terminology to search for the right solution, so I apologize in advanc ...

Responsiveness of the element along the vertical axis

I have an element that, when clicked, should display additional content. However, when the headline contains a lot of text, the element does not expand vertically as it should. Thank you for your help! Check out how it looks currently This is my HTML co ...

What is the best way to arrange two buttons in a row with a border separating each row?

I am currently working with angular 8 and I am in need of creating a webpage design using HTML, CSS, or Bootstrap. The issue I am encountering is how to align every two buttons next to each other with borders, and once the first row is completed, move to ...

Transforming an image by masking it to create a binocular-like view

I've been experimenting with creating a unique visual effect where an image is revealed through a binocular-shaped element that follows the mouse cursor. The circular part of the element moves along with the cursor, unveiling parts of the image as it ...

Why does the HTML and CSS slider fail to load upon page refresh, yet works perfectly when the next or previous button is clicked?

There seems to be an issue with the slider images not loading on page refresh. Oddly enough, they appear only after clicking the next button. However, upon refreshing the page again, the images disappear. <div class="slide-banner"> < ...

I am looking to have three rows of input text area comments instead of just one

Utilizing Bootstrap 4.3.1 with React Components I currently have this image: https://i.sstatic.net/rudsE.png And I am aiming for this design: https://i.sstatic.net/VmyTW.png comments: { elementLabel: "Comments", elementType: 'textar ...

Positioning parent and child elements using CSS

If you have a moment, please check out this URL where I've provided a more detailed explanation of the issue: How to Make an Image Responsive Within a Division I am looking to adjust the width of .slider-wrapper to 100%. Can anyone advise on how to u ...

Certain components display with greater height on Internet Explorer

When viewing my website in Internet Explorer, I noticed that some elements appear taller compared to other browsers. After investigating further, I discovered that all these elements contained SVG images. It seems like they have a fixed height even though ...

Incorrect Alignment of Centered Boxes with CSS

I'm having trouble creating centered boxes with CSS. Currently, I have the following code: http://jsfiddle.net/LW7mN/2/ .trex-clear-all{ display: inline-block; width: 99%; height: 17px; font-size: 12px !important; text-align: -w ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

Utilizing CSS styling to create page breaks in R Markdown disrupts the flow of table of contents

Encountering difficulties in achieving a visually pleasing document using knitr's export to pdf, I have resorted to converting the html file for my Markdown project to pdf through the wkhtmltopdf command line utility tool. A simplified Markdown docum ...

The issue of exceeding margins

I am facing an issue with some CSS. Below is my HTML code: <body> <div id="cn"> <div id="hd"> <ul> <some li's> </ul> </div><!-- /#hd --> <div id="bd"> ...

Tips for customizing the appearance of date circles and text in Vue V-Calendar.io

On v-calendar.io, there is a demo showcasing the correct display where the date "2018-01-01" has a red background and dark text: However, my display does not match that. I have included Vue.js and v-calendar through CDN, and the page is formatted in HTML ...

Shift the content downwards within an 'a' snippet located in the sidebar when it reaches the edge of the right border

I'm having an issue with my style.css file. As a beginner in programming, I am trying to position text next to an image attached in the sidebar. However, the text is overflowing past the border. Here's the HTML code: Please see this first to ide ...

What is the best way to stack text boxes vertically in CSS/HTML?

How can I arrange these textboxes so that .textbox appears at the top, followed by textbox1 below? CSS .textbox { border: 1px solid #848484; -moz-border-radius-topleft: 30px; -webkit-border-top-left-radius: 30px; border-top-left-radius: ...

Is the footer html/css duplicated on a different page and displaying differently?

I have been working on a website and just finished the main page with the header, body, and footer. Now, as I moved on to creating the second page, I encountered an issue. I copied and pasted the code for the header from the main page onto the second page ...

Set iframe or form to be in a fixed position

Having issues with my site where an iframe is contained within a div. When only one character is entered in the telephone box, validation fails and red warning text is displayed. Furthermore, after clicking in the email box and pressing tab twice, the fo ...

Whenever the user hits the "Enter" key, a new element will be generated and added to the bottom of an existing element

I am in need of creating a new element at the end of another element. For instance, let's say I have this element: <div id="elmtobetrig">Element that should be followed by another element when the user hits enter after this text. <! ...

Ways to adjust the width of grid columns within an implicit row

I am curious about changing grid columns on an implicit row. While I won't provide the exact code that I'm working on, I will offer a brief example to clarify my question. <div class='container'> <p>item-1</p> <p& ...

What is the best way to adjust the size of a column when hovering

What I am attempting to achieve is, I would like the column box to have a 100% width, and when it is hovered over, I want it to transition to the left, with the width of the column box being about 60%, revealing a second block that shows up as 20% width o ...