Home > Software engineering >  Problems with HTML and CSS text-align is in center
Problems with HTML and CSS text-align is in center

Time:06-11

This is my website:

enter image description here

How to convert it like this example:

enter image description here

I tried:

text-align: start;

and nothing happens, maybe is because of an error in grammar or something like that

Does are an HTML or CSS issue? I already have text-alight:center, but for some reason it doesn't works, how to solve this issue?

This is my code:

CSS

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'poppins', sans-serif;
}
.topbar{
    position: fixed;
    background: #fff;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
    width: 100%;
    height: 60px;
    padding: 0 20px;
    display: grid;
    grid-template-columns: 2fr 10fr 0.4fr 1fr;
    align-items: center;
    z-index: 1;
}

.logo h2{
    color: #df7f27;
}

.search{
    position: relative;
    width: 60%;
    justify-self: center;
}

.search input {
    width: 100%;
    height: 40px;
    padding: 0 40px;
    font-size: 16px;
    outline: none;
    border: none;
    border-radius: 10px;
    background: #f5f5f5;
    
}
.search label {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
}
.search i {
    position: absolute;
    right: 15px;
    top: 15px; 
    cursor: pointer;
}

.user{
    position: relative;
    width: 50px;
    height: 50px;
}

.user img{
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    object-fit: cover; 
}

/* sidebar starts here*/
.sidebar {
    position: fixed;
    top: 60px;
    width: 260px;
    height: calc(100% - 60px);
    background: #df7f27;
    overflow-x: hidden;
}

.sidebar ul {
    margin-top: 20px;
}
.sidebar ul li {
    width: 100%;
    list-style: none;
}
.sidebar ul li a {
    width: 100%;
    text-decoration: none;
    color: rgb(255, 255, 255);
    height: 60px;
    display: flex;
    align-items: center;
}

.sidebar ul li a i {
    min-width: 60px;
    font-size: 24px;
    text-align: center;
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
    <title>pomodone.app</title>
</head>
<body>
    <div >
        <div >
            <div >
                <h2>Pomodone</h2>
            </div>
            <div >
                <input type="text" id="search" placeholder="search here">
                <label for="search"><i ></i></label>
            </div>
            <i ></i>
            <div >
                <img src="img/user.jpg" alt="">
            </div>
        </div>
        <div >
            <ul>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Dashboard</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Students</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Teachers</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Employees</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Analytics</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Earnings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Settings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Help</div>
                    </a>
                </li>
            </ul>
        </div>
        <div ></div>
    </div>
</body>
</html>

Thank you in advance, I really appreciate your help

CodePudding user response:

You can try using flex box and then doing justify-content to be space-between or space-evenly

CodePudding user response:

You should do two things:

  1. Add a new class to every icon in your sidebar, for instance:
<i ></i>
  1. Style that class by adding horizontal margin. Adjust the pixels as you see fit.
.sidebar-icon {
  width: 1.5em !important; /* Prevent uneven width of icons */
  margin-left: 16px;
  margin-right: 16px;
}

This is the result

Example result

CodePudding user response:

i see your problem you just simply need to add some css justify-content:center; , you use display flex that;s why text-align is not working,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
    <title>pomodone.app</title>
</head>
<body>
    <div >
        <div >
            <div >
                <h2>Pomodone</h2>
            </div>
            <div >
                <input type="text" id="search" placeholder="search here">
                <label for="search"><i ></i></label>
            </div>
            <i ></i>
            <div >
                <img src="img/user.jpg" alt="">
            </div>
        </div>
        <div >
            <ul>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Dashboard</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Students</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Teachers</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Employees</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Analytics</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Earnings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Settings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Help</div>
                    </a>
                </li>
            </ul>
        </div>
        <div ></div>
    </div>
</body>
</html>


*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'poppins', sans-serif;
}
.topbar{
    position: fixed;
    background: #fff;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
    width: 100%;
    height: 60px;
    padding: 0 20px;
    display: grid;
    grid-template-columns: 2fr 10fr 0.4fr 1fr;
    align-items: center;
    z-index: 1;
}

.logo h2{
    color: #df7f27;
}

.search{
    position: relative;
    width: 60%;
    justify-self: center;
}

.search input {
    width: 100%;
    height: 40px;
    padding: 0 40px;
    font-size: 16px;
    outline: none;
    border: none;
    border-radius: 10px;
    background: #f5f5f5;
    
}
.search label {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
}
.search i {
    position: absolute;
    right: 15px;
    top: 15px; 
    cursor: pointer;
}

.user{
    position: relative;
    width: 50px;
    height: 50px;
}

.user img{
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    object-fit: cover; 
}

/* sidebar starts here*/
.sidebar {
    position: fixed;
    top: 60px;
    width: 260px;
    height: calc(100% - 60px);
    background: #df7f27;
    overflow-x: hidden;
}

.sidebar ul {
    margin-top: 20px;
}
.sidebar ul li {
    width: 100%;
    list-style: none;
   text-align: center;
}
.sidebar ul li a {
    width: 100%;
    text-decoration: none;
    color: rgb(255, 255, 255);
    height: 60px;
    display: flex;
    align-items: center;
    /* my chnages here */
   justify-content:center;
}

.sidebar ul li a i {
    min-width: 60px;
    font-size: 24px;
    text-align: center;
    margin-right: 5px;
}

.sidebar ul li a i {
    min-width: 60px;
    font-size: 24px;
    text-align: center;
    display: inline-block;
    margin-right: 5px;
}

Checkout my code, Hope it will help you. :)

CodePudding user response:

Before anything first understand how the icons www.fontawesome.com works.

There's only one simple thing to understand that is:

That the < i > tag you used here is getting commented out and a svg is been loaded to your code above the tag
<i ></i>

Solution

Just add these Properties to your html

  • Padding-left: 30px or as per your requirement;
  • Justify-content: flex-start;
.sidebar ul li a {
  width: 100%;
    text-decoration: none;
    color: rgb(255, 255, 255);
    height: 60px;
    display: flex;
    padding-left: 30px;
    justify-content: flex-start;
  align-items: center
}

.sidebar ul li a i {
    min-width: 60px;
    font-size: 24px;
    text-align: center;
}

New Class to be Added for svg

  • Added a fixed width for svg.
  • Margin-right to align the test in the same line
.sidebar ul li a svg{
  margin-right: 15px;
  width: 25px !important;
}

Run this Code

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'poppins', sans-serif;
}
.topbar{
    position: fixed;
    background: #fff;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
    width: 100%;
    height: 60px;
    padding: 0 20px;
    display: grid;
    grid-template-columns: 2fr 10fr 0.4fr 1fr;
    align-items: center;
    z-index: 1;
}

.logo h2{
    color: #df7f27;
}

.search{
    position: relative;
    width: 60%;
    justify-self: center;
}

.search input {
    width: 100%;
    height: 40px;
    padding: 0 40px;
    font-size: 16px;
    outline: none;
    border: none;
    border-radius: 10px;
    background: #f5f5f5;
    
}
.search label {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
}
.search i {
    position: absolute;
    right: 15px;
    top: 15px; 
    cursor: pointer;
}

.user{
    position: relative;
    width: 50px;
    height: 50px;
}

.user img{
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    object-fit: cover; 
}

/* sidebar starts here*/
.sidebar {
    position: fixed;
    top: 60px;
    width: 260px;
    height: calc(100% - 60px);
    background: #df7f27;
    overflow-x: hidden;
}

.sidebar ul {
    margin-top: 20px;
}
.sidebar ul li {
    width: 100%;
    list-style: none;
    display:  flex;
  justify-content: center;
}
.sidebar ul li a {
  width: 100%;
    text-decoration: none;
    color: rgb(255, 255, 255);
    height: 60px;
    display: flex;
    padding-left: 30px;
    justify-content: flex-start;
  align-items: center
}

.sidebar ul li a i {
    min-width: 60px;
    font-size: 24px;
    text-align: center;
    align-self: flex-start;
}
.sidebar ul li a svg{
  margin-right: 15px;
  width: 25px !important;
}
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
<body>
    <div >
        <div >
            <div >
                <h2>Pomodone</h2>
            </div>
            <div >
                <input type="text" id="search" placeholder="search here">
                <label for="search"><i ></i></label>
            </div>
            <i ></i>
            <div >
                <img src="img/user.jpg" alt="">
            </div>
        </div>
        <div >
            <ul>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Dashboard</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Students</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Teachers</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Employees</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Analytics</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Earnings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Settings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i ></i>
                        <div>Help</div>
                    </a>
                </li>
            </ul>
        </div>
        <div ></div>
    </div>
</body>
</html>

CodePudding user response:

.menuitem {
  display: flex;
  align-items: center;
  justify-content: center;  
}

change the div display to flex and try this.
please inform that I put 'menuitem' to all the div class names.
  • Related