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

Leveraging SVG filters for enhancing text with unique borders

I recently discovered a helpful tip on adding a background to text in SVG using a source. However, I am still unsure how to incorporate a border around the text. <svg width="100%" height="100%"> <defs> <filter x="0" y="0" width="1" ...

Optimal method for storing large arrays of numbers in localStorage using JavaScript

*"Efficient" in this context refers to smaller size (to reduce IO waiting time) and fast retrieval/deserialization times. Storing times are less important in this scenario. I need to store multiple arrays of integers in the browser's localStorage, ea ...

Div with CSS shadow on all sides

Is there a way to achieve this effect using only CSS (not CSS3) so that it is supported by all browsers? I am looking to create a div with shadows on all sides, where the top area will be used for navigation. I have tried searching for tutorials but haven ...

What is the best way to ensure that form inputs and labels stay aligned?

Let's consider a scenario where there is a web page containing a form: <form> <label for="FirstName">First:</label> <input name="FirstName" type="text"> <label for="MiddleName">Middle:</label> <input n ...

Tips for combining multiple fields into a single variable in PHP

When I click the submit button, the image source does not show any result but the image color is displayed. Any help would be greatly appreciated. <div class="form-group"> <input type="file" name="images[source][]" class="form-control input-l ...

Eliminating the issue of double scroll bars that overlap each other

Currently, I am developing a website that utilizes javascript, html, and css. One of the pages on the site displays all users as list items within an unordered list, which is nested inside two separate div elements. When accessing the page on my phone, bot ...

Tips for utilizing the Toggle Slider JS functionality

I'm attempting to change a value using a slider in HTML, here is my approach: html file : <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script scr="./scripts.js" ...

Using JavaScript to display a line using `document.write`

I'm having trouble displaying a line using the Document.Write function in JavaScript. When I submit the form, the line briefly appears and then disappears Any ideas on why this might be happening? <!DOCTYPE html> <html> <head& ...

Click to position custom text on image

Is there a way to adjust the position of text on an image after it has been clicked? I have included an image below to demonstrate how I would like it to appear: https://i.stack.imgur.com/unmoD.png function revealContent(obj) { var hiddenText = obj. ...

Tips on how to showcase it to cover a wide area

I'm having trouble figuring out how to display the output in a span element. Every time I try, I just get a blank result, and when I alert the txtNumber variable, I see "object html span element" in the alert message. <span id="displayQty"> num ...

What could be causing my jQuery to only toggle the first arrow in my HTML?

I am facing an issue with a series of divs generated from data, each containing an arrow that should toggle an expandable section. Strangely, only the first div is functioning properly: toggling the arrow icon and revealing the content upon click. When th ...

Add new information after modifying it

Is there a way to retrieve the updated values from the table product and insert them into the purchases table? My code successfully updates the data, but encounters issues when trying to insert into the purchases table. <?php $ID=$_GET["stocksid"]; ...

In search of a way to verify if a string contains a specific word

I'm currently working on a dynamic tooltip feature. My goal is to detect multiline tooltips by checking if my data includes \r. The code snippet below is an example of how I am trying to analyze my description, but so far, I have been unsuccessfu ...

Animation in CSS/transform drifting out of view

I have a challenge with animating text to each corner of the screen, where the text is rotated in every corner. However, I am facing an issue where the text goes off-screen in the top right and bottom left corners. It seems like the rotation might be causi ...

An unexpected issue occurred: Unable to invoke method on NPObject

I am new to JSON and having trouble accessing object data. Here is the code snippet: <!doctype html> <html> <head> <meta charset="utf-8"> <title>ajax </title> </head> <body> <p id="p"></p& ...

Generate a catalog of individual files within nested folders

I have a structure: [catalogues] [catalogue-name-1] [data_html] name-1.html - table of posted values from another form name-2.html - table of posted values from another form name-3.html - table of posted values from another form ... name-n.html - ta ...

Is there a way I can invoke my function prior to the form being submitted?

For the last couple of days, I've been struggling to make this code function properly. My goal is to execute a function before submitting the form to verify if the class for each variable is consistent. You can access my code here. Any assistance you ...

"Adjusting the height of a div element while considering the

I have 2 elements with different heights and I want to make them equal: var highestEl = $('#secondElement').height(); $('.element').first().height(highestEl); I understand that the second element is always taller than the first one. W ...

What is the best way to create a sidebar that remains open without triggering a new sidebar every time it is clicked

I am looking to add a sidebar that opens a new view without navigating to a different page. The sidebar remains consistent and I want to avoid duplicating it on all pages. Check out this photo for an example. My goal is to provide additional content or fe ...

Center the image and align the floated texts on each side horizontally

I've recently started learning about HTML and CSS, but I'm having trouble grasping one concept. My goal is to align two floating <p> elements on either side of a centered <img>. Here's an example of what I'm trying to achiev ...