2

I am having a flex container with an image and a text. The image is in a <div> and the text is within a <ul> and <li> elements. The <li> is another flex container to justify the text in the center. However, there is a space between the <li> container and the child <div> of the first container. Is there a way to remove the space and center it vertically? I used margin-left:-4%; to remove the space. Is it the right approach?

Currently this is how it looks:

enter image description here

Removing the space and vertically center aligned this is how i want it to look-

enter image description here

CSS:

.headlineContainer{
  display:flex;
  border: red 2px solid;
  justify-content: flex-start;
}

#myPhoto{
  width:10%;
  border: blue 2px solid;
  
}


#myPhoto>img{
  width: 100%;
}

#myHeadline{
  border: green 2px solid;
  list-style-type: none;
  margin-left:-2%;
}

#myHeadline li{
  display: flex;
  border: 2px solid gray;
  justify-content: center;
  
}

#myHeadline #name{
   color: #008080;
     font-size: 340%;
     font-weight: 300;
     font-style:normal;
}

#myHeadline #Tagline{
  font-size: 150%;
    color:#000000;
}

CODEPEN: https://codepen.io/chandeeparora19/pen/oNxBNqL

Rebooting
  • 2,762
  • 11
  • 47
  • 70

3 Answers3

3

In the css, I marked the grafts. There is no space and vertical alignment is set.

Option with borders:

.headlineContainer{
  display:flex;
  border: red 2px solid;
  justify-content: flex-start;
  align-items: center; /*add - vertical center*/
}

#myPhoto{
  width:10%;
  border: blue 2px solid;
  
}


#myPhoto>img{
  width: 100%;
}

#myHeadline{
  border: green 2px solid;
  list-style-type: none;
  padding: 0; /*add - remove space*/
}

#myHeadline li{
  display: flex;
  border: 2px solid gray;
  justify-content: center;
  
}

#myHeadline #name{
   color: #008080;
     font-size: 340%;
     font-weight: 300;
     font-style:normal;
}

#myHeadline #Tagline{
  font-size: 150%;
    color:#000000;
}
<div id="mainContainer" class="headlineContainer" > 
    <div id="myPhoto">
            <img src="https://cdn3.iconfinder.com/data/icons/complete-set-icons/512/googleplus512x512.png"/>

    </div>
    
    <ul id="myHeadline">
      <li id="name">This is google</li>
      <li id="Tagline">Helps you search everything</li>
  </ul>
</div>

Option without borders:

.headlineContainer{
  display:flex;
  border: red 2px solid;
  justify-content: flex-start;
  align-items: center; /*add - vertical center*/
}

#myPhoto{
  width:10%;
  /*border: blue 2px solid;*/
  
}


#myPhoto>img{
  width: 100%;
}

#myHeadline{
  /*border: green 2px solid;*/
  list-style-type: none;
  padding: 0; /*add - remove space*/
}

#myHeadline li{
  display: flex;
  /*border: 2px solid gray;*/
  justify-content: center;
  
}

#myHeadline #name{
   color: #008080;
     font-size: 340%;
     font-weight: 300;
     font-style:normal;
}

#myHeadline #Tagline{
  font-size: 150%;
    color:#000000;
}
<div id="mainContainer" class="headlineContainer" > 
    <div id="myPhoto">
            <img src="https://cdn3.iconfinder.com/data/icons/complete-set-icons/512/googleplus512x512.png"/>

    </div>
    
    <ul id="myHeadline">
      <li id="name">This is google</li>
      <li id="Tagline">Helps you search everything</li>
  </ul>
</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
0

You should reset your padding in this situation.

#myHeadline{
  border: green 2px solid;
  list-style-type: none;
  padding-left: 0;
}
Ahmad Dalao
  • 1,968
  • 1
  • 8
  • 14
0

I added display:table instead of flex for the the headlineContainer and for myphoto and text I added display:table-cell and verical-align:middle

.headlineContainer{
  display:table;
  border: red 2px solid;
  justify-content: flex-start;
}

#myPhoto{
  width:10%;
  border: blue 2px solid;
  display:table-cell;
  vertical-align:middle;
}

ul {
  padding:0px
}
#myPhoto>img{
  width: 100%;
  display:inline-block;
  vertical-align:middle;
}

#myHeadline{
  border: green 2px solid;
  list-style-type: none;
   display:inline-block;
  vertical-align:middle;
}

#myHeadline li{
  display: flex;
  border: 2px solid gray;
  justify-content: center;
  
}

#myHeadline #name{
   color: #008080;
     font-size: 340%;
     font-weight: 300;
     font-style:normal;
}

#myHeadline #Tagline{
  font-size: 150%;
    color:#000000;
}
<div id="mainContainer" class="headlineContainer" > 
    <div id="myPhoto">
            <img src="https://cdn3.iconfinder.com/data/icons/complete-set-icons/512/googleplus512x512.png"/>

    </div>
    
    <ul id="myHeadline">
      <li id="name">This is google</li>
      <li id="Tagline">Helps you search everything</li>
  </ul>
</div>
John
  • 73
  • 8