The text in the middle is not adapting to different screen sizes

I am attempting to create a design with a line above and below my text that is responsive. Currently, the only method I have found requires using width which is not ideal for responsiveness.

My preference would be to utilize before and after pseudo elements only, but if this is not feasible, I am open to exploring alternative approaches.

Here is the HTML code snippet:

<div class="section-header text-center">
  <h2>Testing</h2>
</div>

And here is the corresponding CSS code:

.section-header {
  position: relative;
}

.section-header h2 {
  border: 2px solid rgba(0, 0, 0, 0.1);
  padding: 0.3em 0.8em;
  display: inline-block;
}

.section-header::before,
.section-header::after {
  border-top: 1px solid black;
  display: block;
  height: 1px;
  content: " ";
  width: 40%;
  position: absolute;
  left: 0;
  top: 1.2em;
  opacity: 0.1;
}

.section-header::after {
  right: 0;
  left: auto;
}

.text-center {
    text-align: center !important;
}

View JSFiddle Demo

Answer №1

To achieve the desired effect, you can treat the pseudo element in relation to h2, utilizing overflow to conceal unnecessary parts:

.section-header {
  position: relative;
  overflow:hidden; /*mandatory*/
}

.section-header h2 {
  border: 2px solid black;
  padding: 0.3em 0.8em;
  display: inline-block;
  position:relative;
}

.section-header h2::before,
.section-header h2::after {
  content: " ";
  width: 100vw; /*big value here*/
  height: 1px;
  position: absolute;
  top: 0.6em;
  background:black;
}

.section-header h2::after {
  left: calc(100% + 40px); /*40px offset from the title*/
}
.section-header h2::before {
  right: calc(100% + 40px);
}

.text-center {
    text-align: center;
}
<div class="section-header text-center">
  <h2>Testing</h2>
</div>

Alternatively, you can explore a different approach without transparency by using background and box-shadow properties like so:

.section-header {
  position: relative;
  background:linear-gradient(black,black) center/100% 1px no-repeat;
}

.section-header h2 {
  border: 2px solid black;
  padding: 0.3em 0.8em;
  display: inline-block;
  position:relative;
  background:#fff;
  box-shadow: 
    40px 0 0 #fff,
    -40px 0 0 #fff;
}
.text-center {
    text-align: center;
}
<div class="section-header text-center">
  <h2>Testing</h2>
</div>

Answer №2

An effective approach is utilizing a flexbox for the section-header element to achieve the following:

  • Adjust the space between the h2 heading and the lines by setting a margin on the h2
  • Configure the width of the pseudo elements to occupy 100% - given that it's a flex item, it will automatically adjust based on the available space

For a visual demonstration, refer to the code snippet below:

.section-header {
  position: relative;
  display: flex; /* creating a flex container */
  align-items: center; /* vertical alignment */
}

.section-header h2 {
  border: 2px solid rgba(0, 0, 0, 0.1);
  padding: 0.3em 0.8em;
  display: inline-block;
  margin: 0 1em; /* space adjustment between h2 and line */
}

.section-header::before,
.section-header::after {
  border-top: 1px solid black;
  display: block;
  height: 1px;
  content: " ";
  width: 100%; /* occupying full-width */
  top: 1.2em;
  opacity: 0.1;
}

.text-center {
  text-align: center !important;
}
<div class="section-header text-center">
  <h2>Code Example</h2>
</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

Retrieve information following an Ajax call inside a pre-designed template

Upon rendering a page with data put together by EJS, the goal is to refresh a section (thirdRow) of the page whenever the user submits new data. The refreshed section should display both the newly submitted data and the existing data. Although I have obtai ...

Crafting a Presentation of Inspirational Words

I'm currently in the process of creating a photo album using AngularJS and CSS, but I'm facing an issue with the pictures showing up too large. Despite my efforts to resize them, they still appear oversized. I've even tried resizing them in ...

Adding flair to the encompassing container

Below is the HTML code that I am working with: <div class="um-field field-date"> <p class="form-row " id="date_field"> <label class="date"> <input data-label="Date" data-value="" type="date" class="input-date um-frontend-f ...

The <br> tag does not appear to be displaying correctly on the AngularJS page

I've encountered an issue in my angularJS application where setting a String value with the " " tag does not render as HTML and instead prints as "AA<br>BB" on the page. Even replacing it with "&lt;br&gt;" doesn't render properly, ...

Choose a selection from the options provided

This is a sample for demonstration purposes. I am trying to display an alert with the message "HI" when I click on Reports using the id main_menu_reports. My attempted solution is shown below. <ul class="nav" id='main_root_menu'> & ...

Encountering a 404 error when attempting to rewrite URLs using htaccess for enabling AngularJS html5 mode

Fixing 404 Error in AngularJS Page Refresh Without # and ! in URL I am currently working on an AngularJS project (version 1.7.5) where I needed to remove the # and ! from the URL structure. To achieve this, I implemented the following code snippet: myApp. ...

The issue with HTML5 anchor download attribute for saving files cannot be resolved

When I use an anchor tag like this.. <a class="btn btn-download" href="https://www.anotherdomain.com/file.jpg" download="customname.jpg">Download</a> The file is downloaded as file.jpg instead of customname.jpg Interestingly, it works fine i ...

Using Material UI, I have styled a typography element and a button to appear on the

Struggling to position a button and a <Typography> element side by side: Here's what I currently have: <Grid item lg={12} md={12} sm={12} xs={12} className={classes.register}> <Typography noWrap className={classes.typoText} varian ...

struggling to find the precise value in the array

my original result is displayed below: [status] => 1 [schedule_status] => 1 [cid] =>3 [cdate] => 16-10-18 01:10:52 [did] => 16 [jid] => 4 However, when I try to extract individual array values using the following code: $count = count($ ...

Shadow effect is present under every cell in the table row

I am struggling to create an HTML table with proper vertical spacing between rows and a shadow effect beneath each row. Unfortunately, the shadow always ends up overlapping other table content. I have tried adjusting the positioning of elements and setti ...

Deconstructing JavaScript scripts to incorporate HTML5/CSS3 functionality for outdated browsers such as Internet Explorer

I have been researching various resources and guides related to different scripting libraries, but none of them seem to address all the inquiries I have regarding performance and functionality. With so many scripts available, it can be overwhelming to dete ...

Showcasing the API-retrieved data in VueJS using checkboxes

I am encountering an issue where I need to present the data of an array from an API in checkboxes. The list of checkboxes is fetched from another API and I aim to display the selected values as checked in that checkbox list, but for some reason, it's ...

Can a C# application's design be enhanced using CSS?

While I excel at creating computer-based applications in C#, I've found that designing webpages in ASP.net can be a bit challenging. However, the power of CSS in creating stylish buttons and forms is really appealing to me. I recently searched on Goo ...

Modifying CSS post-rendering to accommodate right-to-left or left-to-right text orientation

I have a unique single-page JavaScript website that retrieves text from a web request in JSON format. The direction of the text (RTL or LTR) is only determined once the response is received. Is there a method to re-render the page after receiving the res ...

Importing CSV files into a PHP/MySQL database

Struggling for a whole week to upload a csv file into my database. Despite reading countless tutorials, I can't seem to figure out what's going wrong with this simple code. Any guidance or assistance would be greatly appreciated! :) if(isset($_ ...

Error 404: CSS3 file not found - - utilizing Node.JS and EJS

While it may appear similar to previous inquiries, I have explored other solutions without success. Below is my code snippet: NodeJs: var express = require("express"), app = express(), mongoose = require("mongoose"); mongoose.connect("mongod ...

Using td with a 100% width does not function properly

In my code, I have a table with 2 rows (tr) that are properly wrapped. However, I am facing an issue where the last row's td element (with the ID TD_16) is not expanding to 100% width. If you'd like to take a look at the code, here is the link: ...

extra elements within the 'navbar-toggler' icon button

How to create a navbar with multiple menu items that are always visible? Some items will be shown upon clicking the navbar button, while others will remain steady. Using the bootstrap navbar-toggler button can make the navbar longer when adding more items ...

"(PHP)WordPress produces a dynamic menu that adapts to different screen sizes

I recently created a menu on my WordPress site [ and I'm facing an issue with making it display vertically in a responsive way. The code in header.php is as follows: <!-- header --> <header> <div class="overNavPanel"> ...

Expandable fluid inline svg that dynamically fills the remaining space alongside text within a flex row

How can I ensure that an inline SVG occupies all available space within a row without specifying its height across different screen sizes? The solution involves setting the SVG to have 100% height with a viewBox, allowing it to adjust accordingly while ma ...