How can I eliminate the empty spaces surrounding an image?

How can I remove the extra space around an image that is placed inside a bootstrap column?

On the desktop version, I was able to solve the issue by setting -1rem margins on the left and right. I tried adjusting the body margin and padding to 0, as well as experimenting with the fluid settings for the container, row, and image in Bootstrap.

Here is the JSX/HTML code:

<React.Fragment>
    <div className="conatiner-fluid bg">
        <div className="row"
            <div className="col-lg-6 col-md-6 col-sm-12">
                <img classname="img-fluid" src={...}></img>
            </div>
            <div className="rightSide col-lg-6 col-md-6 col-sm-12 mt-5 text- 
             centered">
                <h1 className="display-4 text-center">Hello, world!</h1>
                <p className="lead">More text</p>
                <p>even more text</p>
            </div>
        </div>
    </div>
</React.Fragment>

And here is the CSS code:

.rightSide {
    background: black;
    color:white;
.bg {
    background: black;

.img-fluid {
    margin-right: -1rem;
    margin-left: -1rem;
}

However, I am now facing an issue with the mobile version. The image is aligned to the left with a lot of empty space on the right. The image size is 960x1080. Any suggestions on how to fix this for mobile devices would be really helpful. Thank you!

UPDATE: Adding p-0 to the image's div container and removing the -1rem margins on the left and right resolved the problem. There were also some naming conflicts when using img-fluid as the CSS class.

Previously:

<div className="col-lg-6 col-md-6 col-sm-12">
    <img classname="img-fluid" src={...}></img>
</div>
.img-fluid {
    margin-right: -1rem;
    margin-left: -1rem;
}

Updated version:

<div className="col-lg-6 col-md-6 col-sm-12 p-0">
    <img classname="img-fluid" src={...}></img>
</div>

.img-fluid {
    margin-right: -1rem;
    margin-left: -1rem;
}

Answer №1

G-Cyr was on point. Removing p-0 from the container div and eliminating the -1rem margins on the left and right resolved the padding problem on the image. The CSS styling inconsistencies were attributable to the naming conventions. The manipulation of img-fluid in a separate file contributed to the issues. Lesson learned for better naming practices! Big thanks to G-Cyr!

Answer №2

* {
  margin: 0;
  padding: 0;
}

img {
  width: 100%;
}

.rightSide {
    color:white;
}

.bg {
    background: black;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
  <section class="bg">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-6 col-md-6 col-sm-12">
          <img class="img-fluid" src=https://media.sproutsocial.com/uploads/2017/02/10x-featured-social-media-image-size.png>
      </div>
      <div class="rightSide col-lg-6 col-md-6 col-sm-12 pt-5 text- 
                  centered">
        <h1 class="display-4 text-center">Hello, world!</h1>
        <p class="lead">More text</p>
        <p>even more text</p>
      </div>
    </div>
    </div>
  </section>
  
  <script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

Give this a go! Hopefully, it will be of assistance

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

Updating a Textfield in React with a value sourced from an array of objects state variable: A step-by-step guide

While working in React, I encountered an issue with updating a rendered mapped array of objects in Textfields. The goal was to have the Textfield value set to the object's value initially and then allow for updating or changing it as needed. Currently ...

Data not displaying in useFieldArray of React Hook Form

Hey everyone, I've successfully created a material UI form using react hook form and the useFieldArray hook. <Box component="form" onSubmit={handleSubmit(submit)}> <Grid container spacing={2}> <Grid item xs={12} ...

Using Angular material to display a list of items inside a <mat-cell> element for searching

Can I use *ngFor inside a <mat-cell> in Angular? I want to add a new column in my Material table and keep it hidden, using it only for filtering purposes... My current table setup looks like this: <ng-container matColumnDef="email"> < ...

Can you explain the contrast between utilizing `next export` versus solely relying on React in your project?

After reading about the difference between next build && next export and next build && next start on this thread, I understand that the former generates a fully static app while the latter starts a server. This leads me to wonder, how does ...

List style image does not serve its intended purpose

I attempted to personalize my webpage by adding an image to a list, but I couldn't get the list-style image to work. You can view the page at Below is the code I used: CSS /* Fleet List */ ul#flotta li { list-style-image:url("http://ctp.serviziew ...

Show a tweet by its identification number

Is there a way to show a tweet by its unique id? For instance, if I have the id 876126300649525249, can I display the actual tweet on my website? Does anyone know if this is achievable? ...

What is the method for identifying the selected radio button that has been dynamically generated after clicking the submit button?

Despite this question being asked multiple times before, I am struggling to find a solution that fits my specific scenario. Initially, I have a static radio button that I create. The HTML code is as follows: <body> &l ...

Troubleshooting problem with CSS layout when adjusting the height of a section

I recently delved into creating a simple website using HTML5 and have successfully set up the site structure. Everything has been smooth sailing so far. However, I've encountered a section of the site that seems to have a mind of its own. When I ass ...

Encountering a fragment error while utilizing create-react-library

Recently, I embarked on the journey of publishing a React component to npm that I had created. In my quest for knowledge, I stumbled upon create-react-library, which I decided to use for the first time. As I started testing my component from the test folde ...

Why does it appear that Angular is undefined in this straightforward Angular demonstration?

I'm completely new to AngularJS and I've encountered an issue. Yesterday, I ran a very simple AngularJS application that I downloaded from a tutorial and it worked perfectly. The application consists of 2 files: 1) index.htm: <!DOCTYPE htm ...

Enhance the appearance of the circular heading

Is there anyway to customize the look of this rounded header? I am having trouble setting a white background. Update #1: This is my current header HTML code: Here is how it currently appears: I utilized Bootstrap along with some custom CSS. Update #2: ...

The jQuery script seems to be failing to execute

Recently, I have been delving into the world of jQuery. However, after adding my code, the functions I set up are failing to run. Strangely enough, just two weeks ago everything was running smoothly without any errors in linking files within my HTML docume ...

We encountered a surprising error: "EPERM: operation not allowed"

Struggling with ReactJS setup for a new project is driving me insane. The first hurdle came when I tried to add a package: yarn add @material-ui/icons But then, an error popped up in the terminal: error An unexpected error occurred: "EPERM: operation not ...

What are the appropriate Typescript typings for React Components that have the ability to return a string or their child components directly?

What are the suitable types for a React Component that can also output a string or directly its children, in addition to a JSX.Element? For example: type PropsStringExample = Readonly<{ returnString: boolean; }>; type PropsChildrenExample = Readon ...

Creating an immersive full-window background image with React CSS techniques

Struggling with a basic task in React, I am trying to set an image as the full-window background of my website. Although I successfully got the image to appear using CSS styling, achieving a full-window background proved challenging. This led me to experi ...

What is the best way to send file data using the form-data format in Postman with React?

When I first started working with React, I encountered the need to transmit data from the frontend to the backend using an API. The data was in the form of form-data from POSTMAN, as shown in the image below. https://i.sstatic.net/U9QMe.png I opted to us ...

Displaying PHP content using JavaScript classes

I have a popup feature implemented in JavaScript and all the necessary scripts added to my HTML page. I am attempting to load a PHP page in the popup when the submit button of my form is clicked. The popup is functioning correctly for buttons like the one ...

Interacting with a dynamically generated component in React triggers the render method upon hovering over it

One of the features in my application is a Google Map that displays markers based on coordinates retrieved from an API. When the app loads, it makes an API request to fetch a few coordinates to show on the map. The API data is then used to map to a <Mar ...

How can I replicate the Firefox style in IE8 using Bootstrap and CSS?

My webpage is styled with Bootstrap v2 and looks great in Firefox. However, I'm struggling to apply the same CSS color style in IE8: .navbar-inverse .navbar-inner { background-color: #1b1b1b; background-image: -moz-linear-gradient(top, #fab ...

Buttons are not aligned with the rest of the row's height

I'm currently working on designing an upload layout that consists of a container with a row inside. Within the row, there are two buttons (each containing input elements) and one h3. For some reason, I am having difficulty getting them to align at the ...