Bootstrap - Border padding excessively wide

For a project exercise using bootstrap, I'm working on recreating a page that can be found here: https://codepen.io/freeCodeCamp/full/NNvBQW

I've hit a roadblock in trying to put a border around the image. Here's the code I have so far: https://codepen.io/krassheit/pen/mxmdWG?editors=1100

HTML

<div class="container">
  <div class="row" id="mainbox">
     <div class="col-xl">
       <h1 class="text-center">Dr. Norman Borlaug</h1>
       <h2 class="text-center"><em>The man who saved a billion lives</em></h2>
       <div id="test">
       </div>
       <figure class="figure mx-auto d-block image-box">      
          <img src="https://c2.staticflickr.com/4/3689/10613180113_fdf7bcd316_b.jpg" class="img-fluid mx-auto d-block" alt="Placeholder">
          <figcaption class="figure-caption text-center"> Dr. Norman Borlaug, third from left, trains biologists in Mexico on how to increase wheat yields - part of his life-long war on hunger.</figcaption>
       </figure>      
    </div>
  </div>
</div>

CSS

body {background-color: gray;}

#mainbox {
  background-color: #eed2ee;  
}

.image-box{
    padding: 2px 2px 2px 2px;
    background-color: red;
    margin-left: 100px;
}

But I am encountering an issue with the border expanding too wide when the window size is increased. Only by resizing the window smaller than the native picture size do the borders adjust correctly to the intended padding. Attached are screenshots for reference:

Wide Narrow

Answer №1

The issue lies in the fact that the .image-box class has a CSS property of display:block;. This causes the div with that class to expand to full width on wide screens based on the screen resolution.

The solution is simple - you just need to change .image-box to display:inline-block;. Once you make this change, your image will no longer be centered. To center it again, set the parent div to text-align:center. That's all there is to it!

For more details, refer to this link: https://codepen.io/anon/pen/oqQZoe?editors=1100

Answer №2

I'm a bit confused about why the padding should be applied to the outer element. Can you provide some clarification?

Even after rewriting the code with the thumbnail class, the appearance remains unchanged. Here is the updated code:

<div class="container">
  <div class="row" id="mainbox">
     <div class="col-xl">
       <h1 class="text-center">Dr. Norman Borlaug</h1>
       <h2 class="text-center"><em>The man who saved a billion lives</em></h2>
       <div class="thumbnail image-box">
          <img src="https://c2.staticflickr.com/4/3689/10613180113_fdf7bcd316_b.jpg" alt="Placeholder" class="img-fluid mx-auto d-block">
          <p class="text-center">Dr. Norman Borlaug, third from left, trains biologists in Mexico on how to increase wheat yields - part of his life-long war on hunger.</p>
       </div>      
    </div>
  </div>
</div>

I opt not to utilize the jumbotron class as I believe there should be a simpler solution available.

Answer №3

Eliminate the figure element and simply use

<div class="img-thumbnail">
    <img src="https://c2.staticflickr.com/4/3689/10613180113_fdf7bcd316_b.jpg">
</div>

The padding should be on the outer element (outside #mainbox, inside .container). It's worth checking .jumbotron since that is what is used in the example page...

You could opt for using <figure>, however, applying the .mx-auto class will not have any impact unless the parent element is utilizing flexbox (can be activated by .d-flex)

Refer to .img-thumbnail in the Bootstrap documentation. Isn't that what you need? :)

Answer №4

The issue you're facing may be due to the lack of left and right padding assigned to the outer div. You can resolve this by incorporating the following HTML code.

<div class="container">
    <div id="mainbox">
        <div class="row">
            <div class="col-xl">
                <h1 class="text-center">Dr. Norman Borlaug</h1>
                <h2 class="text-center"><em>The man who saved a billion lives</em></h2>
                <div class="thumbnail image-box">
                    <img src="https://c2.staticflickr.com/4/3689/10613180113_fdf7bcd316_b.jpg" alt="Placeholder" class="img-fluid mx-auto d-block">
                    <p class="text-center">Dr. Norman Borlaug, third from left, trains biologists in Mexico on how to increase wheat yields - part of his life-long war on hunger.</p>
                </div>
            </div>
        </div>
    </div>
</div>

Additionally, include the following CSS code.

body {background-color: gray;}
    #mainbox {
      background-color: #eed2ee;
      padding: 30px 60px;
    }
    .image-box{
      background-color: red;
      padding: 5px 5px 5px 5px;
    }
    .image-box img {
      width: 100%;
    }

Answer №5

Experiment with border styles

To add a border to your image, simply use the CSS style ".img-fluid"

.img-fluid{
    border-color: blue;
    border-style: solid;
}     

Alternatively, you can match the width of the figure section to that of the image and apply the style there for the same visual effect.

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

Tips for enhancing contrast in MUI dark theme modals

Currently, I am implementing MUI dark mode for my Next.js application. While the MUI modal functions perfectly in light mode, I am struggling with visibility issues when using it in dark mode. The contrast is not strong enough, making it difficult to disti ...

How to verify the parent nodes in a jstree

I have implemented a two state jstree. However, I am encountering an issue where it is not possible to select any other node in relation to a node. My goal is that when I click on a specific node, all of its parent nodes should also be checked. Any assist ...

Exploring ways to transfer a function variable between files in React

Currently, I am working on a quiz application and have the final score stored in a function in app.js. My goal is to generate a bar graph visualization of the user's results (e.g. 6 right, 4 wrong) based on this score. To achieve this, I created anoth ...

PHP Pear Mail HTML Edition

Despite being able to successfully send emails with text content, I'm facing difficulties in sending HTML emails. The authentication is turned off and Google Apps authenticates based on the server's IP address. $message = 'Blah blah blah&ap ...

Tips for importing font files from the node_module directory (specifically otf files)

We cannot seem to retrieve the fonts file from the node module and are encountering this error message. Can't find 'assets/fonts/roman.woff2' in 'C:\Projects\GIT2\newbusinessapp\projects\newbusinessapp\src ...

The GET method is designed to automatically eliminate any trailing whitespace in the

Whenever I utilize the GET method to transmit a text to the server, like 'abc ', I notice that the whitespace characters at the end are always removed. As a result, the server receives 'abc' instead of 'abc ' My question is w ...

Style large and small text side by side with CSS styling

I've been trying to figure out how to position a smaller biography-style text next to this large title, but I'm having trouble finding a solution. I've experimented with float: left, float: right, and display: flex in my CSS code. Below is t ...

Can a shape similar to an inverted circle be created using CSS and jQuery?

I'm stumped on how to create an inverted circle in the corners. I've included a picture for reference. https://i.stack.imgur.com/jeNdh.jpg Is it feasible to achieve this effect using CSS3 or maybe jQuery? ...

problem with placing text into input field

When using my search program, I am encountering an issue where there is some added space before the text once it is set into the input box after clicking on a search result. To see the problem in action, you can visit the following link: http://jsfiddle. ...

The .keypress() function isn't behaving as expected

I've encountered a coding dilemma. Below is the code in question: $(document).ready(function () { var selectedDay = '#selected_day'; $(function () { $("#date").datepicker({ dateFormat: "DD, d M yy", a ...

I'm looking for a method in JavaScript that can search through my XML file to locate specific attributes and return the entire parent element containing that attribute. Can anyone help with

I'm completely new to XML and Javascript. I currently have this code in my HTML file: <select id="eigenschaften" name="eigenschaften" type="text" onchange=""> <option value="">Choose Property</option> <option value="soci ...

Techniques for Adding Background Color to Fieldset Border

Recently starting to learn HTML and stumbled upon a question: How can I create a background color or image for a field set border? Can I simply use regular color values, or do I need special codes for creating a background color in a field set? Any insig ...

Problem with selecting odd and even div elements

I have a div populated with a list of rows, and I want to alternate the row colors. To achieve this, I am using the following code: $('#PlatformErrorsTableData').html(table1Html); $('#PlatformErrorsTableData div:nth-child(even)').css(" ...

position property in div element disrupts slider functionality

I've been working on incorporating a simple slider into my website and stumbled upon this example on jsfiddle My goal is to have the slider positioned "relative" within my site, but when I change the CSS to position: relative;, the slider no longer ...

Conditional rendering is effective for displaying a form item based on certain conditions, but it may not be as effective for

I want a textarea element to appear or disappear based on the selected state of the radio buttons before it. If "no" is chosen, the textarea will be hidden, and if "yes" is chosen, the textarea will become visible. <fieldset class="input-group form-che ...

color of the foreground/background input in a dropdown menu that

I am attempting to modify the foreground or background color utilized by a dash searchable dropdown when text is entered for searching in the list. The input text is currently black, which makes it extremely difficult to read when using a dark theme. Wit ...

Having difficulty inserting an image with style="background-image:url(' ')" in Rails from the database

Hi everyone! I am new to both Rails and Stack Overflow, and I would really appreciate it if someone could guide me here. I am currently working on a test site that resembles a personal blog. It's mainly for showcasing one user's portfolio. In t ...

Applying CSS styles to a shadow DOM element will not produce the desired visual

I'm encountering an issue while attempting to apply CSS to an element within a shadow-root by adding a class to it. In my Angular component, I have the following code: CSS .test { border: 1px solid red; } TS document.getElementById('my-div&a ...

Preventing div elements from being highlighted when double-clicked

I'm working with a div element that has a background image and I need help figuring out how to disable the highlighting effect when double-clicking on it. Is there a CSS property that can achieve this? ...

The placement is set to absolute, with a designated height

Here is an example http://jsfiddle.net/HnfCU/ I am using jQuery to toggle the visibility of the .child div. The position of the .child is set to absolute relative to its parent element, .parent. The challenge I am facing is adjusting the height of the .ch ...