A method to ensure that inline <div> elements occupy the entire available width

The typical solution for this issue is using float, however, it does not work in my situation. I attempted to utilize flexbox and overflow:hidden for the parent element, but unfortunately, it did not resolve the issue.

Currently, I am dealing with three inline-block elements. The width of the center element is determined by the length of the text within it, while the other two are simply used to draw a black line on either side with a specified height. Here's an example:

.headline {
        width: 700px;
      }

      .headline>* {
        display: inline-block;
      }

      .blackline {
        background-color: #000;
        height: 10px;
      }
<div class="headline">
        <div class="blackline"></div>
        <h1>blah blah blah</h1>
        <div class="blackline"></div>
      </div>

The width of the parent element remains constant, but the center element's width is variable.

I aim to achieve a layout like this: |---blah blah blah---| ensuring that the <h1> element always stays centered and both <div> elements have the same width, occupying all available space based on the width of <h1> since the number of "blah" occurrences cannot be predetermined.

Answer №1

Utilizing flexbox, you can achieve the following design:

.headline {
  width: 500px;
  display:flex;
  align-items:center;
  border:1px solid;
}

.blackline {
  background-color: #000;
  height: 10px;
  flex:1;
}
<div class="headline">
  <div class="blackline"></div>
  <h1>blah blah blah</h1>
  <div class="blackline"></div>
</div>

A more simplified version of the markup would be:

.headline {
  width: 500px;
  display:flex;
  align-items:center;
  border:1px solid;
}

.headline:before,.headline:after {
  content:"";
  background-color: #000;
  height: 10px;
  flex:1;
}
<div class="headline">
  <h1>blah blah blah</h1>
</div>

Alternatively, you can achieve the desired layout like this:

.headline {
  width: 500px;
  display:flex;
  align-items:center;
  justify-content:center;
  border:1px solid;
  background:linear-gradient(#000,#000) 0 50%/100% 10px  no-repeat;
}

h1 {
  background:#fff;
}
<div class="headline">
  <h1>blah blah blah</h1>
</div>

Answer №2

Implementing responsive units is key to achieving this goal. Take a look at the example provided below:

.responsiveWidth{
width: 80vw;
height: 100px;
background-color: blue;
}
<div class="responsiveWidth"></div>

Answer №3

Utilizing the fieldset and legend tags in a unique way to achieve a different look. While these tags have specific purposes, thinking outside the box can lead to interesting results.

div {
  border-right: 1px solid #000;
  border-left: 1px solid #000;
  height: 20px;
}

fieldset {
  border: 0px;
  border-top: 1px solid #000;
}
<div>
  <fieldset>
    <legend align="center">Blah Blah Blah</legend>
  </fieldset>
</div>

Fiddle: https://jsfiddle.net/z364h1ar/

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

What methods are available to modify, append, or remove an attribute from a tag?

I have an HTML document that contains two different types of 'tr' tags. <tr bgcolor="lightgrey"> <tr> Each 'tr' tag includes 3-4 lines of code with embedded 'tags' within them. However, when I try to access the a ...

Encountering the issue "Error: Uncaught PDOException - SQLSTATE[42000] - Syntax error or access violation: 1064"

I encountered an error that says: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064. It seems to be a syntax error in my SQL statement. Can you help me figure out what's wrong with it? require_once "dbconnect.inc.php"; $e ...

Methods for Expanding a Div within an Oversized Row, Despite Height Constraints

I have a situation where I need to adjust the height of a cell in a table. One of the cells contains a larger element that causes the row to expand vertically. I also have another cell with a div element set to 100% of the cell's height. However, I wa ...

What is the best way to seamlessly transition from responsive design to mobile design?

While developing a single-page app, I encountered an issue with the layout when the screen width reaches a specific point. On narrow screens, I prefer to utilize the full width, but for wider screens, I want to limit the width for better readability. The l ...

What is the best way to display rows in alternating red and green colors?

Currently, I am implementing the react table in my React project. I found valuable resources on how to use it at these links: https://github.com/tannerlinsley/react-table/tree/v6#codesandbox-examples and also here: https://www.npmjs.com/package/react-tab ...

No content in a DIV element causing unusual height in IE7

UPDATE 1: After some experimenting, I discovered that by removing the width:100% property from the div, I am able to achieve the desired outcome of having a height of 0 when the div is empty. However, I still require the div to have a width of 100%, which ...

Adding and removing/hiding tab-panels in Angular 4 with PrimeNg: A step-by-step guide

I'm currently working on a tabView project with a list of tab-panels. However, I am struggling to find a way to dynamically hide and unhide one of the tab panels based on specific runtime conditions. Does anyone have any suggestions or insights on how ...

Tips for extending the space between one element and another when the width decreases:

Currently in the process of building a website using HTML/CSS/JS and have run into an issue. My front page features an image with text overlay, where the image has 100% width and a fixed height of 487px. I positioned the text using position:relative; and t ...

CSS Padding Hover Transition solution for Eliminating Flickering Text issue

Is it possible to achieve this? I'm attempting to animate the padding around a centered text link. When the text link is clicked, the padding changes from 20% to 100%, which works correctly. The text is horizontally and vertically centered using CSS ...

Organize column 1 on top of column 2 for mobile display

Currently, I am utilizing the grid system with 2 columns set up as follows: <div class="row"> <div class="col-md-6"> 1 </div> <div class="col-md-6"> 2 </div> </div> ...

Perfectly Aligning Image at the Center of the Screen

I'm having trouble centering a loading image on my screen. It seems to be centered closer to the bottom rather than in the middle. Any suggestions on how I can adjust it to be more centered vertically? Edit: I also want it to look good on mobile devi ...

Adjusting the background color of an individual element within a grid using CSS

<div id="56c46f8385953" class="mg_box mg_pre_show col1_3 row1_3 m_col1_2 m_row1_3 mgi_14 mg_pag_1 mg_gallery mg_transitions mg_closed " rel="pid_14" mgi_w="0.333" mgi_h="0.333" mgi_mw="0.5" mgi_mh="0.333" > <div class="mg_shadow_div"> ...

Sort by label using the pipe operator in RxJS with Angular

I have a situation where I am using an observable in my HTML code with the async pipe. I want to sort the observable by the 'label' property, but I'm not sure how to correctly implement this sorting logic within the pipe. The labels can be e ...

Saving solely the content of an HTML list element in a JavaScript array, excluding the image source

One issue I am facing is with an unordered list in HTML which contains items like <ul id="sortable"> <li class="ui-state-default"><img src="images/john.jpg">John</li> <li class="ui-state-default"><img src="images/lisa.jpg ...

Input the latest data into the Bootstrap form

Implementing the bootstrap4 framework in my web application, I have a table displaying various values. Each row includes an edit button at the end of the row (last column). The requirement is that when this edit button is clicked, a form should pop up with ...

Implementing Google Fonts into Next.js with the combination of Sass, CSS, and Semantic UI React

I have set up my next.config.js file with the following configuration: const withSass = require('@zeit/next-sass'); const withCss = require('@zeit/next-css'); module.exports = withSass(withCss({ webpack (config) { config.module. ...

Linking two div elements together with a circular connector at the termination point of the line

I am currently working on designing a set of cards that will showcase a timeline. I envision these cards to be connected by lines with circles at each end, for a visually appealing effect. At the moment, I have created the cards themselves but I am struggl ...

using jQuery to eliminate an HTML element based on its attribute

How can I remove an element with the attribute cursor = "pointer"? I want to achieve this using JavaScript in HTML. Specifically, I am looking to remove the following item: <g cursor="pointer"></g>. It's unclear to me why this element has ...

What is the process by which the browser displays pseudo-element selectors?

One thing that's been on my mind is the potential resource cost of using *:after. Would the pseudo element be the primary selector, or would all elements still be processed by the client? In simpler terms, what impact does *:after have compared to ju ...

Allowing horizontal scrolling in a table cell set to full width

I'm attempting to achieve the desired outcome by utilizing the code provided at What I'd like to know is why the scrolling function isn't working as expected. Instead of scrolling, it simply expands the table. To see the difference, try run ...