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 could be the reason behind CSS styles applying only when inserted within a style tag in my form, and not in the linked CSS file?

I want to express my gratitude for finding a solution to positioning Validator CallOuts on a web form through this question regarding repositioning the AJAX toolkit's CalloutExtender control. Does anyone know why applying CSS classes directly within ...

A guide on adding specific rows together in a list

I'm working with a grid that contains various items and I want to calculate the total of the val_itemservico column only for the selected rows, not all of them. How can I achieve this functionality? When the user clicks on the "Calculate" button, I n ...

Halt the Changing of Button and Typography Styles with React Router and MUI

I am currently working on a component that I want to make clickable. However, when I wrap them in a <Link> element (from react-router-dom) or set component={Link} to="" on them, all the text inside them automatically becomes hyperlinks. For ...

Merge a dropdown menu with an alphabetically arranged list that is interactive with clickable options

I am still learning HTML and Javascript but I'm doing my best. Currently, I am facing a challenge where I need to create a button that, when clicked, opens a dropdown menu containing a table of data. The user should then be able to select a number fr ...

Getting the value of a JavaScript variable and storing it in a Python variable within a Python-CGI script

Is there a way to capture the value of a JavaScript variable and store it in a Python variable? I have a Python-CGI script that generates a selection box where the user can choose an option from a list. I want to then take this selected value and save it ...

The hideCol method does not work on jqGrid

Encountering a problem with the hidecol method in jqGrid. Despite calling the method, nothing seems to happen. Using version 4.5 of jqGrid. The table is created as follows: // Table creation using jqGrid $('#shipTable').jqGrid({ data: tabl ...

Tips for effectively interpreting a json string

Trying to extract specific fields from a JSON string and display them on an HTML page, but encountering the following error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data This is the JSON being sent: { "Order0& ...

Tips on displaying text inside an icon

As a self-taught newcomer to web development, I apologize if this is a basic question. I'm trying to figure out how to display text inside an icon, like putting a number inside a heart. Would using a webfont icon not be suitable for this purpose? Is ...

The <img> tag is displaying with dimensions of 0 x 0 pixels even though its size was specified

Is there a way to control the image size within an <img> tag placed inside an html5 video element? Currently, it keeps defaulting back to 0 x 0 pixels. The reason behind using this img is to serve as a fallback for outdated browsers where the video ...

In bootstrap 3.0, columns are arranged in a vertical stack only

As a newcomer to web design, I've been struggling to figure out why my basic grid in Bootstrap 3 isn't working as expected. No matter what I try, my columns stack vertically instead of side by side and always resize to fill the browser window. Th ...

Creating an interactive map using Python with Folium, incorporating a draggable legend and clickable items for users to easily zoom into specific locations

Looking to create an interactive map with python folium package. Need icons for locations and a draggable legend that allows for repositioning by mouse drag. Also need ability to click on legend items to zoom into corresponding locations on the map. Can s ...

Stop hyperlinks from automatically opening in a new tab or window

I'm having trouble with my website links opening in new tabs. Even after changing the attributes to _self, it still doesn't work. Can someone please review my code below and provide a solution? Feel free to ask for more clarification if needed. ...

The Webix component is experiencing a lack of refreshment

function refresh_group_items(){ //console.log("calling1"); window.setTimeout(function(){ $$("cfg").reconstruct() }, 3000); } $.ajax({ type: "POST", xhrFields:{ withCredentials: true }, beforeSend: function(reque ...

Arrange 4 divs (children) at each corner of the parent element

Currently, I am facing a challenge in positioning 4 divs in the corners of their parent div. The HTML code structure is as follows: <div class="resize"> <div class="n w res"></div> <div class="n e res"></div> < ...

Is it possible to conceal and completely empty the TextBox once the checkbox is deselected?

When the checkbox is checked, the textbox is displayed; otherwise, it remains hidden. However, the value is not being cleared. Can someone please help me with this issue? Thank you in advance. HTML <div class="munna"> <in ...

Setting the color for the :hover and :active states on a react JSX component: A step-by-step guide

<button onClick={fetchExchangeRates} style={{ backgroundColor: `${selectedColor}` }} className="hover text-white px-3 py-1 flex justify-center items-center gap-2 text- rounded-md" > Convert <FontAwesomeIcon className=&apo ...

"Retrieve the position of a contenteditable element while also considering HTML

I've been exploring a method to generate annotations within HTML text using JavaScript. The approach involves the user clicking on a contenteditable <div> and obtaining the cursor's position based on their click. Subsequently, when insertin ...

"Retrieve data from an Excel file and either present it in a textarea or store it as an array

Is it possible to use JavaScript to read an Excel file and convert a single column (identified by name or index) into an array in JavaScript? ...

I keep encountering an issue with getJson

A snippet of my JavaScript code retrieves a JSON object from a URL. Here is the portion of the code in question: <script> $(document).ready(function(){ $("button").click(function(){ $.getJSON('url_address', {}, function(data) { ...

Having trouble with CSS messing up your gridview button display?

Oddly enough, whenever I place buttons inside a gridview, they end up looking very small (as shown in the image below), even though the gridview itself has no CSS applied to it. Is it possible that some other CSS is affecting the buttons? I have too much ...