Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I centralize 5 divs without using margin-left?

Tags:

html

css

I'm trying to get 5 divs aligned inside a div, like this:

enter image description here

Is there any way to do this without using margin-left?

I mean.. In case I want to eliminate one of this middle divs and they still be aligned? For example.. If I remove div4, the others will be centered automatically. Like this:

enter image description here

I found a solution like this:

#parent {
  width: 615px;
  border: solid 1px #aaa;
  text-align: center;
  font-size: 20px;
  letter-spacing: 35px;
  white-space: nowrap;
  line-height: 12px;
  overflow: hidden;
}
.child {
  width: 100px;
  weight: 100px;
  border: solid 1px #ccc;
  display: inline-block;
  vertical-align: middle;
}

But the problem is: The first div and the last one must be like float left and float right... And this solution centers every thing resulting something like this:

enter image description here

like image 747
Marcelo Barganha Avatar asked Jun 08 '15 20:06

Marcelo Barganha


People also ask

How do I center a div without margins?

Div is basically BLOCK with FULL-WIDTH (100%) so set margin:auto is doesn't get anything since the width is full to the parent. To make it work, you can did that by 2 ways, use text-align:center for div -> this will align text inside div center. include width property in div (i.e. width:200px) and it will work fine.

How do you keep divs centered?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I align all divs in one row?

Answer: Use the CSS margin property If you would like to center align a <div> element horizontally with respect to the parent element you can use the CSS margin property with the value auto for the left and right side, i.e. set the style rule margin: 0 auto; for the div element.


2 Answers

The flexbox might be the answer you're looking for.

#container {
  display: flex;
  justify-content: space-between;
  border: 1px solid black;
  background: #ccc;
}
#container>div {
  width: 100px;
  border: 1px solid black;
  background: #fff;
  height: 100px;
}
<div id="container">
  <div>box 1</div>
  <div>box 2</div>
  <div>box 3</div>
  <div>box 4</div>
  <div>box 5</div>
</div>

If you want to maximise browser compatibility, be sure to also add the correct vendor prefixes:

#container {
  display: -moz-flex;
  display: -ms-flex;
  display: -webkit-flex;
  display: flex;
  /* … */
}
like image 181
Niet the Dark Absol Avatar answered Sep 20 '22 17:09

Niet the Dark Absol


maybe so

text-align: justify - this feature works with text, as well as our line-block (display: inline-block) points, in fact, and are inseparable words in a row, this behavior is quite natural.

Incidentally, it is worth considering that the text-align: justify inherited property, so text-align: left at the next descendants - a necessary measure. In this way, we would return as the alignment of the content of our blocks in the former state.

This algorithm does not apply to the last line, and works with all lines except her.Therefore, using :after I added to the end of another element, pacifier, and stretched it to 100% of the width, thus forcing him to stretch out on the very last line in the list.

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

div {    
    text-align: justify; 
    font-size: 0;
}
div:after {
    width: 100%;
    height: 0;
    visibility: hidden;
    overflow: hidden;
    content:'';
    display: inline-block;
}
div > div {
    background: #E76D13;
    width: 100px;
    height: 100px;
    display: inline-block;
    text-align: left;
    border: 1px solid #000;    
    line-height: normal;
    font-size: 14px;
    vertical-align: top;    
}
<div>
    <div>1</div>
   <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</div>
like image 33
Dmitriy Avatar answered Sep 21 '22 17:09

Dmitriy