How to make alignment of 1 div and another float using CSS

I want my div2 to be centered and div3 to the right.

What is expected

I tried to do this with text align: center for the main div and float to the right on div3, but it aligns it to the center, considering the rest of the main div. I gave display: inline-flex for the main div

What's happening

<div style="height: 40px;width:120px;background-color: yellow;align-items: center;">

<div style="height: 20px;width:20px;background-color: red;">
  Hello
</div>

<div style="height: 20px;float: right;width:20px;background-color: red;">
</div>
</div>
      

Run codeHide result


+3


source to share


7 replies


Add style="margin: auto;"

div2 to your element. And also style="margin-left: auto;"

to the div3 element.



<div style="height: 40px;width:120px;background-color: yellow;align-items: center;">

<div style="margin:auto; height: 20px;width:20px;background-color: red;">
  Hello
</div>

<div style="margin-left:auto; height: 20px;float: right;width:20px;background-color: red;">
</div>
</div>
      

Run codeHide result


+1


source




.main {
  display: block;
  position: relative;
  width:100%;
  text-align: center;
  border: 1px solid red;
}
.main .div1 {
  display: inline-block;
  border: 1px solid;
}
.main .div2 {
  float: right;
  border: 1px solid;
  display: inline-block;
}
      

<div class="main">
  <div class="div1">
    div1
  </div>
  <div class="div2">
    div2
  </div>
</div>
      

Run codeHide result


+1


source


Divs are block level elements, so you can use the left and right auto margin to place it in the middle.

.center {
  margin: 0 auto;
}

.right {
  float: right;
}

      

In HTML, you will need to adjust the order of the sections. Place div 3 in front of div 2 so that when it floats they appear on the same line:

<div class="outer">
    <div class="right"></div>
    <div class="center"></div>
</div>

      

https://jsfiddle.net/dcqpw12u/1/

+1


source


You can use position:relative

for main and position:absolute

other div

as well as center it vertically

.main {
  text-align: center;
  background-color: red;
  height: 50px;
  position: relative;
}

.div2 {
  background-color: blue;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.div3 {
  background-color: green;
  position: absolute;
  right: 0;
  top: 50%;
  transform: translate(0, -50%);
}
      

<div class="main">
  <div class="div2">SOME DIV 2</div>
  <div class="div3">SOME DIV 3</div>
</div>
      

Run codeHide result


+1


source


Try with this code:

<div style="height: 40px;width:120px;background-color: yellow;align-items: center; position:relative;">

<div style="height: 20px;width:40px;background-color: red; overflow:auto; margin:0 auto">
  Hello
</div>

<div style="height: 20px;position:absolute; right:0px; top:0px; width:20px;background-color: red;">
</div>
</div>

      

+1


source


.contentmain{
    background: white none repeat scroll 0 0;
      color: black;
      height: auto;
      width: 35%;
      float: left;
      background:red;
  }
  .contentCenter{
    background: white none repeat scroll 0 0;
      color: black;
      height: auto;
      width: 30%;
      float: left;
      background:yellow;
  }
  .contentRight{
    background: white none repeat scroll 0 0;
      color: black;
      height: auto;
      width: 35%;
      float: right;
      background:red;
  }
      

<div class="contentmain">
		Main<br/>
		Content<br/>
	</div>
	<div class="contentCenter">
		Center<br/>
		Content<br/>
	</div>
	<div class="contentRight">
		Right<br/>
		Content<br/>
	</div>
      

Run codeHide result


It can meet your requirements.

+1


source


<!DOCTYPE html>
<head>
<style>
.div0 {
  text-align: center;
  border-style: solid;
  border-width: 5px;
  height: 50px;
  border-color: red;
  position: relative ;
}
.div1 {
  border-style: solid;
  border-width: 4px;
  right: 0%;
  height: 40px;
  width:40px;
  border-color: green;
  position: absolute;
 }

 .div2 {
  left: 50%;
  right:50%;
  width:40px;
  position: absolute;
  border-style: solid;
  height: 40px;
  border-width: 4px;
  border-color: green;
    }
</style>            
</head>
<body>
<div class="div0">
  <div class="div1"><p>div1</p></div>
  <div class="div2"><p>div2</p></div>
</div>  

</body>
</html>

      

basically you can achieve this by using the position property and the right and left CSS properties you can reference more. The Position and right property can be found on the site.

what i did in my answer sets the main div as relative position and the other sub divs (div2 and div3) as absoulute

To get one div in the right corner, you set the correct property to 0%
and to center the div, I used 50% on both the right and left.

+1


source







All Articles