Struggling to center a column without affecting the alignment of other elements within the parent row

Seeking help on how to center the first column (a photo) in a row without centering the second column and its content. This is for a responsive layout on small devices, but applying the text-center class to the parent row centers all content. Here is the link to view the layout: https://i.sstatic.net/jW0Rq.jpg

Open to any suggestions or workarounds to achieve this layout.

.blog-topics {
  max-width: 180px;
  max-height: 180px;
}

.blog-post {
  max-width: 250px;
  max-height: 250px;
  margin-top: 5px;
}

.blog-box {
  max-width: 700px;
}

.blog-post-topic {
  color: #17a2b8;
  font-size: 14px;
  margin: 0px;
  padding: 0px;
}

.blog-post-title {
  text-transform: uppercase;
  font-weight: 600;
  margin: 0px 0px 8px 0px;
  padding: 0px;
}

.blog-post-excerpt {
  font-size: 16px;
  color: #686868;
  margin: 0px 0px 10px 0px;
  padding: 0px;
}

@media (max-width: 540px) {
  .btn-blog {
    display: block;
    width: 100%;
  }
}
<div class="row p-3">
  <div class="col border">
    <h3 class="text-center">Recent Posts</h3>
    <div class="row">
      <div class="col-auto">
        <img src="img/guy.jpg" alt="topic" class="blog-post">
      </div>
      <div class="col-md blog-box">
        <p class="blog-post-topic">Privary & Security</p>
        <p class="blog-post-title">Guarding Against Computer Malware</p>
        <p class="blog-post-excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit</p>
        <button type="button" class="btn btn-info btn-blog mb-4">Read More</button>
      </div>
    </div>
  </div>
</div>

Answer №1

To achieve this layout, I utilized Bootstrap classes strategically. Instead of applying col-auto on all breakpoints, I opted to use col-auto only on a higher breakpoint such as col-md-auto. This adjustment ensures that the image column takes up the full available width when stacked, allowing for easy centering of the image.

<div class="row p-3">
        <div class="col border">
            <h3 class="text-center">Recent Posts</h3>
            <div class="row">
                <div class="col-md-auto d-flex justify-content-center">
                    <img src="http://via.placeholder.com/250" alt="topic" class="blog-post">
                </div>
                <div class="col-md blog-box">
                    <p class="blog-post-topic">Privary & Security</p>
                    <p class="blog-post-title">Guarding Against Computer Malware</p>
                    <p class="blog-post-excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
                        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit</p>
                    <button type="button" class="btn btn-info btn-blog mb-4">Read More</button>
                </div>
            </div>
        </div>
    </div>

Answer №2

To apply the CSS rule margin: 0 auto to the parent element .col-auto, follow these steps:

.blog-topics {
  max-width: 180px;
  max-height: 180px;
}

.blog-post {
  max-width: 250px;
  max-height: 250px;
  margin-top: 5px;
}

.blog-box {
  max-width: 700px;
}

.blog-post-topic {
  color: #17a2b8;
  font-size: 14px;
  margin: 0px;
  padding: 0px;
}

.blog-post-title {
  text-transform: uppercase;
  font-weight: 600;
  margin: 0px 0px 8px 0px;
  padding: 0px;
}

.blog-post-excerpt {
  font-size: 16px;
  color: #686868;
  margin: 0px 0px 10px 0px;
  padding: 0px;
}

.col-auto {
  margin: 0 auto;
}

@media (max-width: 540px) {
  .btn-blog {
    display: block;
    width: 100%;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" 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>


<div class="row p-3">
  <div class="col border">
    <h3 class="text-center">Recent Posts</h3>
    <div class="row">
      <div class="col-auto">
        <img src="https://placehold.it/100" alt="topic" class="blog-post">
      </div>
      <div class="col-md blog-box">
        <p class="blog-post-topic">Privary & Security</p>
        <p class="blog-post-title">Guarding Against Computer Malware</p>
        <p class="blog-post-excerpt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
          dolor in reprehenderit</p>
        <button type="button" class="btn btn-info btn-blog mb-4">Read More</button>
      </div>
    </div>
  </div>
</div>

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

Nested CSS selector within a parent element

How can I change the color of both elements inside a custom button when the button is active? The first color is defined by the class of the button, and the second color is defined by the class of the span inside that button. CSS: .buttonClass{ color:b ...

What could be causing my PHP code to not execute properly alongside the HTML?

I am currently sharpening my skills in PHP by working on a simple web form project. To put it simply, I need to develop a basic web form that accepts a two-dimensional array of European cities along with the distance in kilometers between each pair of citi ...

organize multiple blocks in the middle of the page (html)

This code is unique and belongs to me. <html> <head><title>HOME||~All About EDM~</title> </head> <body > <center><img src="edm1.jpg" alt="edm" width="950" height="250"></body></center> <c ...

The collapsible menu is not expanding properly after a click event due to the small size of

My website has a menu placed in the header with a background color. When resizing the page (max width 1044 px), it switches to a mobile menu, but the div holding the menu and anchors is too small compared to the header. Is there a solution to this issue? ...

The iframe does not completely fill the col-sm column

https://i.sstatic.net/VeqFj.jpgHaving an issue styling embedded Grafana panels on my website. How can I adjust the iframes to fit within col-sm without large margins (refer to attached screenshot) This is my html: <p class= ...

How can I alter the div once the form has been submitted?

Is there a way to change the background of a form and its results after submitting the form? I need to switch the image background to a plain color. I attempted to use a solution I found, but it doesn't seem to be working as expected. You can view my ...

Interactive HTML5 map featuring geo tagged points

Seeking an HTML5-based tool to efficiently display a zoomable, panable map (similar to Google Maps) and incorporate a circle at a specific location upon clicking a button on a webpage. Any suggestions for HTML5-compatible frameworks or would JQuery be the ...

Troubleshooting a Node.js server issue with a two-dimensional array

I am currently facing an issue with submitting a form that contains two-dimensional array fields on a post request in node.js. The problem lies in the fact that the server side is receiving a one-dimensional array with all the values combined. Below is an ...

Invoke the designated JavaScript function within a subordinate <frame>

Below is an example of the standard HTML structure for a parent page named "index.html": <html> <head> <title>test title</title> </head> <frameset cols="150,*"> <frame name="left" src="testleft.html"> ...

Discover the concealed_elem annotations through the power of JavaScript

As I work on my new website, I am struggling with narrowing down the web code. I came across a solution that seems fitting for what I need, but unfortunately, I can't seem to make it work: I attempted the non-jQuery solution, however, I must be missi ...

What is the process for converting Database/Table Data into a File Name?

Hey there! I have a query regarding Leaflet Markers that I need help with. So, I have this database table with a field named icon_name which contains values like: |icon_name| ___________ |FIRE | |HOMICIDE | |RAINSTORM| Additionally, I have a folder ...

HTML5 validation fails to activate

Addressing the issue head-on Check out the form I have created: <form action="<?php echo base_url()?>" method="post" id="formfield"> <center> <label>How much? <small>( Minimum of 100 )</small></label&g ...

Enhancing CSS and HTML: Increasing the Border Color of Dropdown Menus and Utilizing Multiple Words per Dropdown Item

https://i.sstatic.net/ZI8Ch.png CODE div.box { display: block; text-align: center; margin: auto; z-index: 5 } #navMenu { margin: 0; padding: 0; } #navMenu ul { margin: 0; padding: 0; line-height: 30px; } #navMenu li { margin: 0; padding: 0; list- ...

Place an element in relation to the vertical size of a div that includes written content

Currently, I am working on implementing a button with a popup that appears underneath it when the user hovers over it. The specific requirements for this setup are: The size of the button should not affect the size of the popup The popup should always be ...

CSS and JavaScript Nav Menu Collapse (No Bootstrap)

I have written a navbar code using pure HTML/SASS, but I am facing a challenge in adding a collapse element to the navigation bar. Despite trying various solutions from Stack Overflow, I still haven't found one that works for me. Therefore, I am rea ...

Can anyone suggest a stellar sprite generator for me?

I am in search of a sprite generator to streamline my web development process. With so many options available, I am seeking recommendations for the best tool. Ideally, I want a generator that can produce both a sprite image and corresponding CSS files wi ...

Guide on creating CSS for webkit scrollbar through the use of Javascript

I have a div named 'dynamic' with text inside, and I am applying styling to it using javascript. var styleElement = document.createElement("style"); styleElement.appendChild(document.createTextNode("#dynamic { color: red; }")); document.getEleme ...

Guide on traversing to various sections within a single page with Vuetify

I attempted to use HTML anchors to achieve this: <ul> <li><a href="#dashobard">Dashboard</a></li> </ul> Here is what my target looks like: <v-card id="dashboard"> <v-card-text class="document"> Con ...

What is the best way to vertically center content in Bootstrap 4.5?

Below is a snippet of the code I've been working on. <body style="height: 100vh; margin: 0; padding: 0; "> <!-- Form Section ============================================= --> <p>[wpshortcode1]</p> <div cl ...

Is there a feature on the webpage that allows users to save clicked xy points and display them as text?

Is there a way to incorporate a feature on a website that displays clicked xy points as text, allowing them to be easily copied and pasted? ...