Using Bootstrap4 container-fluid on elements other than the "div" tag

For my Bootstrap 4 alpha 5 project:

I am experimenting with using the .container-fluid class on HTML elements that are not divs. However, I have noticed a change in output when applying it to non-div elements. My main objective is to create custom CSS for specific HTML elements within my application to define things like an application body or header, which I can style once and reuse throughout, reducing redundancy in my CSS code and improving the readability of my HTML.

Below are four tests I conducted:

  1. Standard approach using a div with class="container-fluid":
  2. Using an element named "test-body" with class="container-fluid":
  3. Utilizing SASS / Grunt generated CSS with @include make-container()
  4. Implementing SASS / Grunt generated CSS with @extend .container-fluid

<div class="container-fluid mt-1 mx-2">
  <div class="row">
    <div class="col-xs-6 text-white bg-danger">regular div Left</div>
    <div class="col-xs-6 text-white bg-danger text-xs-right">regular div Right</div>
  </div>
</div>

<test-body class="container-fluid mt-1 mx-2">
  <div class="row">
    <div class="col-xs-6 text-white bg-danger">container-fluid attribute Left</div>
    <div class="col-xs-6 text-white bg-danger text-xs-right">container-fluid attribute Right</div>
  </div>
</test-body>

<!-- For the next two examples, see the -->
<!-- SCSS source code and compiled CSS lower in the post -->

<pm-body class="mt-1 mb-3 mx-2">
  <div class="row pm-decoration">
    <div class="col-xs-6">Sass @include make-container() Left</div>
    <div class="col-xs-6 text-xs-right">Sass @include make-container() Right</div>
  </div>
</pm-body>

<pm-header class="mt-1 mb-3 mx-2">
  <div class="row pm-decoration">
    <div class="col-xs-6">Sass @extend Left</div>
    <div class="col-xs-6 text-xs-right">Sass @extend Right</div>
  </div>
</pm-header>

Click the link below to view the output from this code:

Output from above code

The only working example as expected is the one using a DIV element. It seems that margins are respected only when the container is wrapped in a DIV. Whether $enable-flex is enabled or not, the output remains consistent in this regard.

Even if the answer turns out to be "you can't achieve that," I am keen on understanding the academic reasoning behind this. There appears to be no discernible difference other than the absence of a native HTML DIV surrounding the CSS class.


REFERENCE FOR THE LAST TWO TESTS:

In the last two tests, you'll find the SCSS source code alongside the compiled CSS output after running it through Grunt. The color coding served as a validation tool to ensure correct SASS compilation and usage, with a focus on @extend and @include functionalities. See below:

SCSS for the @Include make-container:

pm-body{
    @include make-container();

    .pm-decoration{
        background-color: $pm-row-header-background;
        color: $pm-row-header-color;
        font-weight: bold;
    }
}

CSS output from the @include make-container:

pm-body {
  margin-left: auto;
  margin-right: auto;
  padding-left: 15px;
  padding-right: 15px;
}

pm-body::after {
  content: "";
  display: table;
  clear: both;
}

pm-body .pm-decoration {
  background-color: #DDE5FF;
  color: #6699FF;
  font-weight: bold;
}

SCSS from the @extend .container-fluid:

pm-header{
    @extend .container-fluid;

    .pm-decoration{
        background-color: $pm-app-border-blue;
        color: BLACK;
        font-weight: bold;
    }
}

CSS output from the @extend .container-fluid:

.container-fluid, pm-header {
  margin-left: auto;
  margin-right: auto;
  padding-left: 15px;
  padding-right: 15px;
}

.container-fluid::after, pm-header::after {
  content: "";
  display: table;
  clear: both;
}

pm-header .pm-decoration {
  background-color: #BBCCFF;
  color: BLACK;
  font-weight: bold;
}

Answer №1

Check out this link for more information on HTML block-level elements

<section> is an example of a block level element and typically has display: block; as its default display property. When you define a custom element type, it usually defaults to display: inline; unless specified otherwise.

The bootstrap css style .container-fluid assumes that the element it's applied to is block-level (like <div>), so applying it to a non block-level element can cause issues.

To fix this issue, you can add the following code:

custom-element {
  display: block;
}

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

Working with Python and BeautifulSoup to retrieve <td> elements in a table without specified IDs or classes on HTML/CSS documents

While utilizing Selenium, Python, and Beautiful Soup to scrape a web page, I encountered the challenge of outputting table rows as comma-separated values due to the messy structure of the HTML. Despite successfully extracting two columns using their elemen ...

Columns that adapt to different screen sizes while maintaining a fixed aspect ratio, limited to a maximum of two columns

I'm facing a challenge trying to blend a few elements together: My goal is to have two columns take up 100% of the browser width with a height that adjusts relative to this width. I want these columns to switch to one column when the viewport is sma ...

Storing data locally with Localstorage and manipulating it with Jquery

I'm experiencing an issue with localstorage that I need help with. Context I am currently working on a web application for quotes that requires a bookmark system to save users' favorite quotes. To address this issue, I have created a jQuery scri ...

The placeholder text in the matInput field is not being displayed

As a newcomer to Angular, I am facing a challenge that has left me unable to find a solution. Below is the code snippet in question: <mat-form-field> <input matInput placeholder="ZIP" style="color:red;"> </mat-form-field& ...

The css cropping issue with sprite image is not being resolved

I'm currently working on creating a sprite image for the bottom links on our media page, but I seem to be having trouble getting the image to crop correctly. Can anyone point out where I may be making a mistake? CSS .footer{ position:absolute; ...

How to target the attribute value of an image element that has a specific parent class using jQuery

I'm working with a list that looks like this: <ul> <li><div class="thumb"><img src="image-1.jpg"></div></li> <li><div class="thumb"><img src="image-2.jpg"></div></li> <l ...

Override the CSS property in question

I have tackled numerous lines of pure CSS code, and I always knew where each property was coming from. However, the inline CSS works fine, but not the class/id. The doctype is correctly typed. Despite spending a significant amount of time researching, noth ...

Guide on developing a JavaScript script for implementing across numerous Bootstrap modals within a single webpage

I have been working on setting up a page with 14 different modals. Initially, all the modals were opening normally, but I recently discovered a way to make them draggable when opened. After some trial and error, I managed to implement this feature successf ...

Discovering the compiled CSS file in React when using Sass: A step-by-step guide

In my React project, I have incorporated SASS. Now I am looking to find the final compiled CSS file from that SASS code. Whenever I navigate to: While there is the SCSS file visible, where can I locate the CSS version? I am referring to the compiled outp ...

Unwanted transparency issue in MaterialUI (MUI) BottomNavigation

Greetings fellow hobby developer! I recently embarked on a project using create-react-app and incorporated MUI dependencies. One feature I added was a fixed BottomNavigation, which you can view in action here. Interestingly, in CodeSandbox, the BottomNavi ...

Div's background image displays flawlessly on Code Pen desktop, yet fails to appear when viewed on mobile devices

I am currently experiencing an issue with my background images not rendering properly on mobile devices, specifically safari and chrome on iPhone XR. Although they appear to work perfectly on Code Pen - resizing properly as the viewport shrinks and switchi ...

Tips for styling your Angular Material Card on mobile devices

Currently, I am very happy with my desktop layout which looks like this: https://i.stack.imgur.com/tG0pw.png However, when it comes to the mobile version of my site, here is what I have so far: https://i.stack.imgur.com/KD1hh.jpg While I do like the ho ...

"Any tips on maintaining the blue color of my dropdown link when it is selected as active

I'm struggling to keep the bootstrap accordion dropdown link blue when it's active. I've tried different methods but none seem to work. What am I missing? Here's my code: <div class="visible-sm visible-xs" id="accordion" role="t ...

What is the best way to apply variables from a SCSS file to functions within a .vue file?

After following the steps provided in this link, I have successfully configured sass for my project. First, I installed sass using npm install -D sass-loader sass. Next, I created a file named variables.scss under the src directory: src/variables.scss $ ...

blending the transition from dark image to black background

Utilizing the CSS below: #divPic { height: 32pc; background-image: url('https://wallpaperscraft.com/image/black_black_white_bone_time_game_noise_74543_4000x2248.jpg'); background-repeat: no-repeat; background-position: left center; b ...

The Stylebook in Delphi 10 Seattle does not apply correctly on forms other than the Main form

Within my application's MainForm, I have 3 Stylebooks available for users to choose from. Once a selection is made, the same Stylebook is applied to other Forms within the program. While most of the styles are properly set, there is one toolbar that s ...

Steps to display an angled bracket to indicate a line break

I have a div that is editable, allowing me to display the entered text. To indicate each new line, I would like to show a ">" symbol at the beginning of the line. HTML <body> <div> <span> &gt; </span> ...

Adjust the spacing between the inputRoot and input in Material UI Autocomplete

Recently, in one of my projects, I've been utilizing Material-UI's Autocomplete component. Despite its lack of flexibility with resizing, I managed to tweak certain width, height, and font size attributes successfully. However, I'm currently ...

Library or theme with CSS that does not require the use of HTML classes

While there are many popular CSS libraries like Bootstrap and Foundation, they all require adding specific classes to HTML tags for styling. However, I find it tedious to add individual classes like .form-control from Bootstrap to every input element on m ...

What is the process for implementing hover transitions on link elements?

I am trying to incorporate a transition effect for my links. Essentially, I want the text-decoration (which is currently set to underlink) to gradually appear. Unfortunately, my previous attempt at achieving this has been unsuccessful: #slink{ transit ...