I can’t get bootstrap star icons to center next to review text in a website I am designing. I imbedded the svgs in my code. I am using some bootstrap 4 classes here as well for context. Here’s how it looks right now:
Appearance of review star icons and text:
My code:
.reviews {
color: black;
}
.review-name {
font-weight: 700;
margin-bottom: 2px;
margin-top: 40px;
}
.review-location {
font-weight: 400;
}
.stars-container {
vertical-align: center;
}
.stars {}
.review-date {
font-weight: 400;
}
.review-content {
font-size: .9rem;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<section id="reviews" class="reviews container-md">
<div class="review-item">
<p class="review-name">Brian C.</p>
<p class="review-location">REVIEW LOCATION</p>
<p class="review-date">
<span class="stars"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-star-fill" viewBox="0 0 16 16">
<path
d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z" />
</svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-star-fill" viewBox="0 0 16 16">
<path
d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z" />
</svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-star-fill" viewBox="0 0 16 16">
<path
d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z" />
</svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-star-fill" viewBox="0 0 16 16">
<path
d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z" />
</svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-star-fill" viewBox="0 0 16 16">
<path
d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z" />
</svg></span> Jul 10, 2019
</p>
<p class="review-content">REVIEW TEXT</p>
<p class="blockquote-footer">Yelp</p>
</div>
</section>
I tried using the vertical-align property to no avail. It made no real difference in the stars-container class. Using vertical-align with the stars class (the span enclosing the svgs) made no difference either, so I didn’t even include it:
.stars { vertical-align: center; }
After reading about this problem I tried the mx-auto (in my case, my-auto) bootstrap class suggested by another poster, but it didn’t work for me. Not sure what else to try.
3
Answers
Wrap the star rating in a native JavaScript Web Component
<star-rating
Encapsulating HTML, CSS and JS
With shadowDOM you won’t have to worry about any other CSS in the page.
Then HTML:
Outputs:
I simplified that SVG star with: https://yqnn.github.io/svg-path-editor/#
Use Flex. That’s all you need here. No custom CSS. You only need 3 classes provided by Bootstrap.
Here are 2 snippets with the icon font and with the SVGs. Note that the HTML used around the elements is exactly the same and the display is exactly the same too.
See https://getbootstrap.com/docs/5.3/utilities/flex/
First version with icon font :
Second version with SVG :
Add this CSS:
vertical-align
takes care of the alignment, but it only works for block and inline-block elements, therefore thedisplay
setting.