I’m learning Bootstrap and I wanted to make a theme from sratch but now at the starter points I’m stuck with a weird problem–icons are stick to logo at the right side of page however float:left
with !important
declaration must align them to the left while it does not do that!
What is going on here? How can I properly align the icons to left side and the logo to the right side?
* {
font-family: aviny;
font-size: 22px;
text-align: right;
direction: rtl;
}
.top-header {
margin-top: 20px;
}
.instagram-icon img {
float: left !important;
/* does not stick the icons to left side!!! */
}
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
</head>
<body>
<div class="container top-header">
<div class="row">
<img src="https://via.placeholder.com/80" alt="logo header" width="80">
<div class="instagram-icon">
<img src="https://via.placeholder.com/50" alt="instagram">
<img src="https://via.placeholder.com/50" alt="telegram">
<img src="https://via.placeholder.com/50" alt="youtube">
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
</body>
2
Answers
I found a solution:
row
div.row
created ad-flex justify-content-between
.instagram-icon
.This is the code:
On css always try to avoid
!important
and dont usefloat
always try to useflex
orgrid
instead to position things.This is Stackblitz I used: https://stackblitz.com/edit/web-platform-tqdq6r?file=index.html
Bootstrap rows are flex elements, so the
justify-content-between
flex layout class spreads the two flex children to the sides. Learn more about flexbox.Don’t use floats. They’re a dated layout technique and have very few legitimate uses in 2023.
Also consider using Bootstrap’s margin classes instead of writing your own CSS. The
mt-3
class gets you two times the default spacing unit (2 x 0.5rem = 1.0rem), which is close to your 20px.