0

I've read a hundred posts about how to center a row of divs, but I can't seem to get this to work. I want these elements to align to the center of their container, while maintaining a gap between each of them. No matter what I seem to do, they hug the left.

Do I need to change the CSS on the container element, or the individual div items themselves?

Help!

.news_section {

    width: 100%; 
    height: 100%; 
    margin-left: auto;
    margin-right: auto;
  }
  
  
.news_container { 

border: 1px red solid; 
width: 100%; 
height: 100%;
position: relative;
overflow: hidden; 
margin-left: auto; 
margin-right: auto;
text-align: center; 


}
  
.news_item { position: relative; float: left; font-family: graphik-light; font-size: 1.1em; margin: 25px 25px 25px 0px; width: 220px; height: 400px; cursor: pointer; transition: all 0.2s ease-out;
    -webkit-box-shadow: 0px 3px 10px 1px rgba(204, 204, 204 0, 1);
    -moz-box-shadow: 0px 3px 10px 1px rgba(204, 204, 204 0, 1);
    box-shadow: 0px 3px 10px 1px rgba(204, 204, 204, 1);
    
     } 
.news_item:hover { background: #efefef; transform: scale(1.04); transition: all 0.2s ease-in; cursor: pointer; }

.news_img { 
    width: 100%;
    height: 100%; 
    object-fit: contain;
}
.news_header { font-family: 'Oswald', sans-serif; font-size: 22px; line-height: 1.4em; text-align: left; margin-top: 18px; margin-left: 14px; margin-right: 14px; }
.news_outlet { font-size: 13px; text-align: left; margin-top: 7px; margin-left: 14px; color: #c1c1c1; font-family: graphik-regular; text-transform: uppercase; }
.news_subtext { font-size: 14px; text-align: left; margin-top: 20px; margin-left: 14px; margin-right: 14px; color: #4d5051; font-family: graphik-regular; }


.news_more { position: absolute; bottom: 10px; font-size: 16px; margin-left: 14px; color: #000; font-family: 'Oswald', sans-serif; }
<section class="news_section"> 
<div class="news_container">

            <div class="news_item">
                <div><img src="https://thruline.com/prototype/images/news/news.jpeg" class="news_img"/></div>
                <div class="news_header">News title Here</div>
                <div class="news_outlet">MAGAZINE OUTLET</div>
                <div class="news_subtext">Description of articles goes here....</div>
                <div class="news_more">Click Here to Read More...</div>
            </div>
      
      
      
                <div class="news_item">
                <div><img src="https://thruline.com/prototype/images/news/news.jpeg" class="news_img"/></div>
                <div class="news_header">News title Here</div>
                <div class="news_outlet">MAGAZINE OUTLET</div>
                <div class="news_subtext">Description of articles goes here....</div>
                <div class="news_more">Click Here to Read More...</div>
            </div>
      
      
      
      
                <div class="news_item">
                <div><img src="https://thruline.com/prototype/images/news/news.jpeg" class="news_img"/></div>
                <div class="news_header">News title Here</div>
                <div class="news_outlet">MAGAZINE OUTLET</div>
                <div class="news_subtext">Description of articles goes here....</div>
                <div class="news_more">Click Here to Read More...</div>
            </div>
      
      
      

      
</div>
</section> 

2 Answers2

0

On the news_item ... Change that

float: left;

to

display: inline-block;

.news_section {

    width: 100%; 
    height: 100%; 
    margin-left: auto;
    margin-right: auto;
  }
  
  
.news_container { 

border: 1px red solid; 
width: 100%; 
height: 100%;
position: relative;
overflow: hidden; 
margin-left: auto; 
margin-right: auto;
text-align: center; 


}
  
.news_item { position: relative; display: inline-block; font-family: graphik-light; font-size: 1.1em; margin: 25px 25px 25px 0px; width: 220px; height: 400px; cursor: pointer; transition: all 0.2s ease-out;
    -webkit-box-shadow: 0px 3px 10px 1px rgba(204, 204, 204 0, 1);
    -moz-box-shadow: 0px 3px 10px 1px rgba(204, 204, 204 0, 1);
    box-shadow: 0px 3px 10px 1px rgba(204, 204, 204, 1);
    
     } 
.news_item:hover { background: #efefef; transform: scale(1.04); transition: all 0.2s ease-in; cursor: pointer; }

.news_img { 
    width: 100%;
    height: 100%; 
    object-fit: contain;
}
.news_header { font-family: 'Oswald', sans-serif; font-size: 22px; line-height: 1.4em; text-align: left; margin-top: 18px; margin-left: 14px; margin-right: 14px; }
.news_outlet { font-size: 13px; text-align: left; margin-top: 7px; margin-left: 14px; color: #c1c1c1; font-family: graphik-regular; text-transform: uppercase; }
.news_subtext { font-size: 14px; text-align: left; margin-top: 20px; margin-left: 14px; margin-right: 14px; color: #4d5051; font-family: graphik-regular; }


.news_more { position: absolute; bottom: 10px; font-size: 16px; margin-left: 14px; color: #000; font-family: 'Oswald', sans-serif; }
<section class="news_section"> 
<div class="news_container">

            <div class="news_item">
                <div><img src="https://thruline.com/prototype/images/news/news.jpeg" class="news_img"/></div>
                <div class="news_header">News title Here</div>
                <div class="news_outlet">MAGAZINE OUTLET</div>
                <div class="news_subtext">Description of articles goes here....</div>
                <div class="news_more">Click Here to Read More...</div>
            </div>
      
      
      
                <div class="news_item">
                <div><img src="https://thruline.com/prototype/images/news/news.jpeg" class="news_img"/></div>
                <div class="news_header">News title Here</div>
                <div class="news_outlet">MAGAZINE OUTLET</div>
                <div class="news_subtext">Description of articles goes here....</div>
                <div class="news_more">Click Here to Read More...</div>
            </div>
      
      
      
      
                <div class="news_item">
                <div><img src="https://thruline.com/prototype/images/news/news.jpeg" class="news_img"/></div>
                <div class="news_header">News title Here</div>
                <div class="news_outlet">MAGAZINE OUTLET</div>
                <div class="news_subtext">Description of articles goes here....</div>
                <div class="news_more">Click Here to Read More...</div>
            </div>
      
      
      

      
</div>
</section> 

Better still, take a look at flex-box: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Neil W
  • 7,670
  • 3
  • 28
  • 41
  • Changing float: left to display: inline-block does align them to the center, but then for some reason they are not level-- the boxes being to look like stairs where each one is then lower than the last. Any ideas why? – Jordan McMahon Feb 16 '22 at 17:29
  • Hmmm. If you click "Run code snippet" on my answer, it looks ok to me. – Neil W Feb 16 '22 at 17:31
0

If you want to align all items to the center, you need to use Flexbox. Basically, you gotta add this code to align everything of your container in center:

CSS:

.container: {
   display: flex; /* flexbox */
   justify-content: center;  /* horizontal center */
   align-items: center; /* vertical center */
}

This is an excelent article about Flexbox, made by W3School:

dududornelees
  • 177
  • 3
  • 13