Achieve vertical alignment in unordered lists with Bootstrap 4

Bootstrap4 documentation states that the predefined classes align-items-center and justify-content-center can be used to align contents vertically and horizontally. While testing the example provided in the Bootstrap4 document, it worked correctly. However, when using these classes in the following code, the alignment of the contents within the ul tag is not as desired. The ul tag has been set to display:flex using Bootstrap classes, but the contents (li tags) are not aligned both horizontally and vertically centered. I also inspected the ul property in Chrome DevTools and found that it had margin-top: 0 and margin-bottom: value. What could be causing this issue?

HTML:

<body>
        <div id="top-ribbon" class="container-fluid">
            <div class="row align-items-center">
                <div id="socials" class="col-6 col-md-2 offset-md-2">
                    <ul class="d-flex align-items-center justify-content-center">
                        <li class="list-inline-item color-white"><i">1</i></li>
                        <li class="list-inline-item color-white">2<i"></i></li>
                    </ul>
                </div>
            </div>
        </div>

Css:

color-white{
    color: white;
}
#top-ribbon{
    background-color: rgb(168, 8, 133);
    border: 1px solid;
    height: 200px;
}
.row{
  height: 200px;
}
#top-ribbon div{
    border: 1px solid;
}
#socials ul{
  border: 1px solid;
}

JSFiddle

Answer №1

First and foremost, it appears that your HTML syntax is causing some issues. Additionally, it's important to note that Bootstrap includes default padding and margin settings for ul elements. By removing these, you should achieve the desired outcome.

#socials ul {
    border: 1px solid;
    margin: 0; padding: 0;
}

Check out this link on jsFiddle for reference.

I have provided two examples for your review.

Answer №2

To start, include the Universal Class i.e. *{margin:0px; padding:0px;} This will remove any extra space.
For vertical alignment, add display:flex; and align-flex:center;
In the program, a margin-bottom is added so it should be specified as margin-bottom:0px;

* {
  margin: 0px;
  padding: 0px;
}

.color-white {
  color: white;
}

#top-ribbon {
  background-color: rgb(168, 8, 133);
  border: 1px solid;
  height: 200px;
  Width:100%;float:left;
}

.row {
  height: 300px;
}

#top-ribbon div {
  border: 1px solid;
  height: 100%;
}

#socials {
  display: flex;
  align-items:center;
}

#socials ul {
 
  margin-bottom: 0px;

  width: 100%;
  float:left;text-align:center;
}

#socials ul li{float:left;}
<div id="top-ribbon" class="container-fluid">
  <div class="row align-items-center justify-content-center">
    <div id="socials" class="col-6 col-md-2 offset-md-2">
      <ul class="d-flex align-items-center justify-content-center">
        <li class="list-inline-item color-white">1</li>
        <li class="list-inline-item color-white">2</li>
      </ul>
    </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The variable "require" has not been declared in npm.js file at line

Apologies if this is a simple issue, but I have not been able to find a solution in my research. I am using the latest release of Bootstrap and have noticed a new file called npm.js compared to previous versions. All Bootstrap files are hosted locally on m ...

The form inputs within the modal are unresponsive to clicks

I recently began using bootstrap modals and had a separate register/login page from the index. I thought incorporating the login/register form into modals would be a good idea. However, inside the modal, all the inputs are not clickable - only accessible v ...

Calls to the function are piling up

There is a bootstrap modal that displays a confirmation message. If the 'accept' button is clicked, a function is called with an id. If the 'cancel' or 'close' button is clicked, the modal closes. The problem arises when the m ...

What is the best way to verify HTML using RSS?

Looking to improve my skills in HTML/CSS/PHP development, I'm faced with a challenge when it comes to validating code that contains content beyond my control, such as an RSS feed. For instance, on my home page, which is a .php document containing bot ...

Toggle class on element based on presence of class on another element

If I have 4 boxes and the user is required to choose one. I aim to assign an active class to the box that the user clicks on. However, if the user selects another box, I want to remove the class from the first clicked box and apply it to the newly clicked ...

What is the best way to implement rounded borders using CSS?

Can we use the latest CSS standards to create rounded borders? Unfortunately, this feature is only available in CSS level 3 and above. ...

How can you extract information from a text document and seamlessly incorporate it into an HTML webpage?

I am currently working with an HTML file structured like this.. <html> <body> <table border="1" cellpadding="5"> <tr> <td>Some Fixed text here</td> <td id="data">---here data as per values in external text file--- ...

What is the best way to apply CSS to my HTML code?

I have the files stored within my Django project. My directory structure for templates looks like this: |base.html | |rules | |style_base.css In my base.html file, I link to the stylesheet like this: <link type="text/css" href="/rules/style_b ...

Troublesome button appearance

I set up two sections to gather user information using buttons. My goal is for the button styles to change when clicked, making it clear to users which option they have selected, even if they switch between sections. However, I encountered an issue where ...

I am having trouble getting my bootstrap codes to run on my Django website. Can anyone help me figure

Hey there! Struggling with incorporating bootstrap into my django website. I've added the bootstrap link in the HTML page and inserted the navbar code in the body section, but upon running the server, the website remains unchanged! What could be ca ...

Steps for aligning an image in the center above two divs

I've been attempting to accomplish something without success. The image below should speak a thousand words. My goal is to position div 3, located in div 2, perfectly centered between div 1 and 2 to achieve the desired outcome: Here's my HTML a ...

socket.io not defined and rejecting the connection

https://i.sstatic.net/B4bqV.pnghttps://i.sstatic.net/QfJqv.pngMy current code utilizes socket.io to display a message. However, when I execute node server.js, none of the output messages are being logged to the console. Below are snippets of my chat.html, ...

What steps can be taken to prompt the layout to transition?

I have an element that sticks to the top based on the current scroll offset. The issue is that the layout doesn't update when there is space available after the stuck element. This creates a ghost gap where the stuck element used to be... http://fidd ...

Creating a new image by extracting a specific HTML div tag that contains an image and its layers

I currently have a div element that includes an image element along with multiple other div elements containing small text displayed above the image. I am looking to save the image along with its content to a new image file. Any suggestions or ideas are ...

Images that dynamically adjust to different screen sizes with captions that are perfectly

For a while now, I have been utilizing <picture> to display images with more design control. I typically use CSS to show or hide specific images within the <div> block. However, I am now considering using <figure> with <figcaption> ...

Accordion - Conceal Previously Revealed Items

I'm currently working on finding a solution to hide all child sections that are open in my accordion when a new header is clicked. I've included a jsfiddle link to show what I have so far. Any suggestions on how to close the opened sections would ...

What is the best way to alternate between two CSS stylesheets when using jQuery-UI?

Just getting started with jQuery and javascript and decided to test out 2 different jQuery-ui stylesheets in a simple .html file. The goal is to have the example dialog open with the "flick" stylesheet and the datepicker within the dialog open with the "u ...

Is there a way to eliminate the directional tag from the base css by utilizing a separate css file?

css, body, forms { text-align: right; } I need to remove this part of the code because it is causing alignment issues with my slider in IE7. ...

Can the <article> tag and all its content be positioned in the center of the webpage?

Can the <article> and its content remain centrally aligned on the page? Check out my website here: Typically, when you visit a website, the article is positioned in the center of the page. However, in my case, the article only remains centered when ...

what methods do you use to recall codes?

Hey there! I've recently started diving into the world of HTML, CSS, and Php. I'm curious, how exactly do experienced coders manage to remember so many functions? Is it something that comes with time and practice? As a beginner myself, this quest ...