How to center image

Asked

Viewed 560 times

0

I have a menu and I’m trying to put the image to the center and leave 3 links to the left and 3 links to the right, I used:

display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); 

But it is only aligned in the mobile version, see below:

/*Main Menu CSS*/
*{
	margin: 0;
	padding: 0;
}
.header-area{
	background:#4DC3CF;
}
.header-area .logotipo {
display: inline-block;
line-height: 55px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.main-menu ul{
	text-align: center;
}
.main-menu ul li{
	list-style:none;
	display:inline-block;
	position:relative;
}
.main-menu ul li a{
	text-decoration: none;
	display: inline-block;
	font-size:18px;
	padding:30px 20px;
	margin:0px;
	color:#fff;
	font-weight: 600;
	text-transform: uppercase;
	transition: .4s;
}
.main-menu ul li a:hover{
	color:#000;
}

/*Dropdown Menu CSS*/
.main-menu ul li ul{
	position: absolute;
	left:0;
	top:100%;
	width:200px;
	z-index: 999;
	transform: scaleY(0);
	transform-origin: top center;
	opacity:0;
	visibility: hidden;
	transition: .4s;
	background:#4DC3CF;
	box-shadow: 0 2px 8px -2px rgb(37, 39, 38);
}
.main-menu ul li:hover ul{
	transform: scaleY(1);
	opacity:1;
	visibility: visible;
}
.main-menu ul li ul li a{
	padding:14px 18px;
}
   <header class="header-area">
			<a href="#" class="logotipo"><img src="https://i.imgur.com/eH60XtT.png" alt="logo alt"></a>
        <div class="main-menu">
            <nav>
                <ul>
                    <li><a href="#" title="">Home</a></li>
                    <li><a href="#" title="">About Us</a></li>
                    <li><a href="#" title="">Services</a>
                        <ul>
                            <li><a href="#">Service One</a></li>
                            <li><a href="#">Service Two</a></li>
                            <li><a href="#">Service Three</a></li>
                            <li><a href="#">Service Four</a></li>
                            <li><a href="#">Service Five</a></li>
                        </ul>
                    </li>
                    <li><a href="#" title="">Portfolio</a></li>
                    <li><a href="#" title="">Blog</a></li>
                    <li><a href="#" title="">Contact Us</a></li>
                </ul>
            </nav>
        </div>
    </header>

I saw post here and as far as I understood it was to be aligned because of the top: 50% and left and Translate and the display-inline-block would make it stay inside, but it didn’t work.

  • You can easily do it using bootstrap. visit: https://getbootstrap.com/docs/4.4/getting-started/download/

  • I know B4, but in this case I’m using HTML and CSS and with B4 will have to apply the code to Nav-Brand the same way, so I don’t understand why the code isn’t working.

  • @Gotab4 your question is misspelled and it’s hard to understand what you want. Can you rephrase the question?

  • @Kleberoliveira with you, in case I want to put the image in the middle in the menu and leave the links 3 the right and 3 the left, I can call in chat?

  • @Gotab4 tried to answer with the reworked question, see if it is your need.

2 answers

1

Or separating selectors in Transform

.elemento {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

-1


Using the property display with the value flex, you will have more features to align the elements on the screen. Use flex-direction: row;, justify-content: center; and align-items: center; to keep all elements centralized and a @media to make the layout responsive according to your guide.

Here is an example of the use of the value flex:

/*Main Menu CSS*/
* {
  margin: 0;
  padding: 0;
}
.header-area {
  background: #4dc3cf;
}
.header-area .logotipo {
  display: inline-block;
}
.main-menu ul {
  text-align: center;
}
.main-menu ul li {
  list-style: none;
  display: inline-block;
  position: relative;
}
.main-menu ul li a {
  text-decoration: none;
  display: inline-block;
  font-size: 18px;
  padding: 30px 20px;
  margin: 0px;
  color: #fff;
  font-weight: 600;
  text-transform: uppercase;
  transition: 0.4s;
}
.main-menu ul li a:hover {
  color: #000;
}

/*Dropdown Menu CSS*/
.main-menu ul li ul {
  position: absolute;
  left: 0;
  top: 100%;
  width: 200px;
  z-index: 999;
  transform: scaleY(0);
  transform-origin: top center;
  opacity: 0;
  visibility: hidden;
  transition: 0.4s;
  background: #4dc3cf;
  box-shadow: 0 2px 8px -2px rgb(37, 39, 38);
}
.main-menu ul li:hover ul {
  transform: scaleY(1);
  opacity: 1;
  visibility: visible;
}
.main-menu ul li ul li a {
  padding: 14px 18px;
}

.main-menu {
  display: flex;
  flex-direction: row;
  justify-content: center;
align-items: center;
}

@media (max-width: 650px) {
  .main-menu {
flex-direction: column;
  }
}
  <header class="header-area">

<div class="main-menu">
  <nav>
    <ul>
      <li><a href="#" title="">Home</a></li>
      <li><a href="#" title="">About Us</a></li>
      <li><a href="#" title="">Services</a>
        <ul>
          <li><a href="#">Service One</a></li>
          <li><a href="#">Service Two</a></li>
          <li><a href="#">Service Three</a></li>
          <li><a href="#">Service Four</a></li>
          <li><a href="#">Service Five</a></li>
        </ul>
      </li>
    </ul>
  </nav>
  <div class="logotipo">
    <a href="#"><img src="https://i.imgur.com/eH60XtT.png" alt="logo alt"></a>
  </div>
  <nav>
    <ul>
      <li><a href="#" title="">Portfolio</a></li>
      <li><a href="#" title="">Blog</a></li>
      <li><a href="#" title="">Contact Us</a></li>
    </ul>
  </nav>
</div>
  </header>

  • 1

    Why Flex for something so simple? A simple div with the IMG being a native INLINE element, it would suffice that the DIV, remove position:Absolute (from author code) parent is used text-align: center;, No wonder today’s websites get heavier and visually still simple, is because people really believe that new technologies come to replace what works so well, when in fact FLEX time and place to use fruitfully, you can choose to have a war tank to go to the bakery to get 6 loaves, but that doesn’t mean you should encourage others to this.

  • @Guilhermenascimentom agree :), help our developer friend with his simplest solution. And it may be negative if you’ve found my solution too complex.

  • Got it, I’ve always been recommended flexbox in general, I’m a beginner, I saw the code in mine didn’t work I guess because I use jquery and meanmenu, I could download the full code and have a look? https://drive.google.com/file/d/1TlsfuQD6WuENe5H2Q8JaWp_bMDWUjqqI/view It would be very useful if you like to call in chat.

  • No negative answers, rarely do so, only in case of spam or thing that this actually wrong, in your case is not wrong, only is not a convenient way to solve the problem.

  • @Gotab4 to separate the links the problem is not the image and yes the menu, there is something that is complicated to solve, outside that will depend on how you want something to be supported or not, I refer only to the menu, the logo is technically easy, even isolating it, of course could put it inside the MENU would already solve almost everything, but your question is very confusing, can not understand well where things have to be;

  • @Guilhermenascimento really understood his point and was a flea behind my ear on this subject of using flexbox to be slow, I found this post: https://developers.google.com/web/updates/2013/10/Flexbox-layout-isn-t-slow but I don’t really want to induce a beginner to make a mistake, I will search for the best way to help someone.

  • @Gotab4 changes your HTML too which will work.

  • 1

    @Kleberoliveira his last comment summarized the best possible solution, remake. The author wants to patch and this is complicated, the best would be to study the basics before jumping forward, in fact the question should be closed, because it is not even a well defined problem. The closing vows are not of evil ;)

  • @Kleberoliveira I’m trying to get this result: i.imgur.com/B7tmp7p.png 3 links left 3 right and right in the center I tested the snippi and here I did not believe that due to the other code I left the google drive link if I can take a look

  • @Gotab4 fatally you will need to change your menu.

  • @Kleberoliveira only changes the mobile part, I think it’s because I have inheritance, I’m using meanmenu a mini menu framework responsive.

  • 1

    @Kleberoliveira will close the post, good night.

  • @Kleberoliveira Thanks for the answers I will try to use another menu this is very complicated

  • @Guilhermenascimento Thank you for your answers as well.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.