2

I have the following HTML structure:

.parent{
  border: 1px solid;
  width: 60%;
}

.child2{
  margin: 0 auto;
}
<div class="parent">
  <a href="#" class="child1">it should stay in the left side</a>
  <br>
  <a href="#" class="child2">this should be center</a>
</div>

I need to center a.child2 . I can do this by adding text-align: center to div.parent. But in that case, a.child1 will be centered too and I don't want to.

As you can see, I've also added margin: 0 auto; to a.child2, but it is still placed on the left side.

To sum up, how can I center a.child2 while mantaining a.child1 on the left side?

kukkuz
  • 41,512
  • 6
  • 59
  • 95
Martin AJ
  • 6,261
  • 8
  • 53
  • 111

6 Answers6

3

Set display property as block show them in different line. And then apply text-align property to the .child2 to align text in center.

.parent {
  border: 1px solid;
  width: 60%;
}
.parent a {
  display: block;
}
.child2 {
  text-align: center;
}
<div class="parent">
  <a href="#" class="child1">it should stay in the left side</a>
  <a href="#" class="child2">this should be center</a>
</div>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
3

set .child2 to display:table

.parent{
  border: 1px solid;
  width: 60%;
}

.child2{
  margin:auto;
  display:table
}
<div class="parent">
  <a href="#" class="child1">it should stay in the left side</a>
  <br>
  <a href="#" class="child2">this should be center</a>
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126
1

You can center using display: table and margin: 0 auto which is already in place.

.parent{
  border: 1px solid;
  width: 60%;
}

.child2{
  display: table;
  margin: 0 auto;
}
<div class="parent">
  <a href="#" class="child1">it should stay in the left side</a>
  <br>
  <a href="#" class="child2">this should be center</a>
</div>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
1

If you don't mind, you can make child2 display inline-block and width 100%, then you can center its content.

.parent{
  border: 1px solid;
  width: 60%;

}

.child2{
 width:100%;
 text-align:center;
 display: inline-block;
}
<div class="parent">
  <a href="#" class="child1">it should stay in the left side</a>
  <br>
  <a href="#" class="child2">this should be center</a>
</div>
Jordi Flores
  • 2,080
  • 10
  • 16
1

For the margin: 0 auto to work there are few things you need to keep in mind.

  • The element must display: block
  • The element must not float
  • The element must not have a fixed or absolute position
  • The element must have a width that is not auto.

So in your case only 2 things were missing - width & display: block(since <a> is by default display:inline-block). This should fix

.child2{
  margin: 0 auto;
  width:40%;
  display: block;
}
Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44
0

Put those lines in p tags and assign text-align: center to the second one:

.parent{
  border: 1px solid;
  width: 60%;
}

.child2{
  text-align: center;
}
<div class="parent">
<p class="child1">
  <a href="#" >it should stay in the left side</a>
  </p>
<p class="child2">
  <a href="#">this should be center</a>
</p>
</div>
Johannes
  • 64,305
  • 18
  • 73
  • 130
  • If you have access to the html, this approach would be my preferred one. But I would replace the `p` by `li` and make `div.parent` a `ul`. – connexo Sep 16 '16 at 14:43
  • if it's a navigation menu, yes – Johannes Sep 16 '16 at 14:46
  • I don't see what this would have to do with it being a navigation element or not. Unordered lists are the right elements to use when you have a list of items that have a common semantic origin. – connexo Sep 16 '16 at 17:14
  • exactly - which a nagivation menu is good example for... – Johannes Sep 16 '16 at 20:42