Centering text and buttons under images - css

I am trying to center the text underneath each image and underneath each image I want the button to be centered. I've been using paddings and margins to try to fix my problem but it's not working. Also, am I using the button and figcaption tags correctly?
figure img {
border: 10px solid #a8eb6c;
height: 30%;
width: 30%;
float: left;
display: inline-block;
margin: 0 16px 0 0;
}
figure figcaption {
display: inline-block;
float: left;
}
figcaption {
font-size: 17px;
margin: 30px 110px 30px 60px;
}
figure button {
background-color: #a8eb6c;
display: inline-block;
margin: 0 110px 30px 60px;
}
<figure>
<img src="img/drawstring-bag.jpg" alt="drawstring-bag">
<img src="img/charm-necklace.jpg" alt="icecream-necklace">
<img src="img/phone-case.jpg" alt="phone-case">
<figcaption>Purses • Tops • Skirts</figcaption>
<figcaption>Earrings • Pendants • Clay Necklaces</figcaption>
<figcaption>Scarves • Mittens • Plushies</figcaption>
<button role="button">Sensational Sewing</button>
<button role="button">Creative Charms</button>
<button role="button">Knockout Knitting</button>
</figure>
</div>
</main>

You were very close originally, but you need to make each "card" have it's own container. Therefore, you would want 3 figure tags, with each one containing an img, a figcaption, and a button. You could just as easily use div tags to accomplish this, but figure and figcaption give you semantic code. You could always put this group of figures inside of a div container later on, depending on how you are using it.
figure {
text-align: center;
width: 20%;
float: left;
}
img {
max-width: 100px;
border: 10px solid #a8eb6c
}
<figure>
<img src="https://www.totallypromotional.com/media/totallypromotional/images/items/TDB100/product/jpg/Polypropylene-Drawstring-Bag-TDB100-red.jpg" alt="drawstring-bag">
<figcaption>Purses • Tops • Skirts</figcaption>
<button role="button">Sensational Sewing</button>
</figure>
<figure>
<img src="https://cdn11.bigcommerce.com/s-zt9cwiz411/images/stencil/1280x1280/products/2076/5316/avalon_personalized_charm_necklace__39075.1508947216.jpg?c=2&imbypass=on" alt="icecream-necklace">
<figcaption>Earrings • Pendants • Clay Necklaces</figcaption>
<button role="button">Creative Charms</button>
</figure>
<figure>
<img src="https://dimg.dillards.com/is/image/DillardsZoom/zoom/kate-spade-new-york-glitter-ombre-dot-iphone-case/05482185_zi_pink_multicolor.jpg" alt="phone-case">
<figcaption>Scarves • Mittens • Plushies</figcaption>
<button role="button">Knockout Knitting</button>
</figure>

Wrap your content that you would like aligned. I've wrapped them in <div>'s and applied text-align:center the the <div>'s.
.centerContent {
text-align: center;
vertical-align: top;
}
figure {
display: inline-block;
}
figure .centerContent {
width: 28%;
display: inline-block;
}
figure div img {
border: 10px solid #a8eb6c;
height: 90%;
width: 90%;
}
figcaption {
}
figure div button {
background-color: #a8eb6c;
}
<div class="centerContent">
<figure>
<div class="centerContent">
<img src="https://www.w3schools.com/w3css/img_lights.jpg" alt="drawstring-bag">
<figcaption>Purses • Tops • Skirts</figcaption>
<button role="button">Sensational Sewing</button>
</div>
<div class="centerContent">
<img src="https://www.w3schools.com/w3css/img_lights.jpg" alt="drawstring-bag">
<figcaption>Earrings • Pendants • Clay Necklaces</figcaption>
<button role="button">Creative Charms</button>
</div>
<div class="centerContent">
<img src="https://www.w3schools.com/w3css/img_lights.jpg" alt="drawstring-bag">
<figcaption>Scarves • Mittens • Plushies</figcaption>
<button role="button">Knockout Knitting</button>
</div>
</figure>
</div>
Also, I would like to recomend that you take a look at Bootstrap. You may find it helpful.
Hope this helps,

I hope this helps! I have used flex and wrapped a div around each element
figure img {
border: 10px solid #a8eb6c;
height: 30%;
width: 30%;
display: inline-block;
margin: 0 16px 0 0;
text-align: center;
}
figure figcaption {
display: inline-block;
text-align: center;
}
figcaption {
font-size: 17px;
margin: 30px 110px 30px 60px;
}
figure button {
background-color: #a8eb6c;
display: inline-block;
margin: 0 110px 30px 60px;
}
.container{
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
}
.center{
text-align: center;
}
.main_container{
display: flex;
justify-content: center;
align-content: center;
}
<figure>
<div class="main_container">
<div class="container">
<div class="center"><img src="img/drawstring-bag.jpg" alt="drawstring-bag"></div>
<div class="center"><figcaption>Purses • Tops • Skirts</figcaption></div>
<div class="center"><button role="button">Sensational Sewing</button></div>
</div>
<div class="container">
<div class="center"><img src="img/charm-necklace.jpg" alt="icecream-necklace"></div>
<div class="center"><figcaption>Earrings • Pendants • Clay Necklaces</figcaption></div>
<div class="center"><button role="button">Creative Charms</button></div>
</div>
<div class="container">
<div class="center"><img src="img/phone-case.jpg" alt="phone-case"></div>
<div class="center"><figcaption>Scarves • Mittens • Plushies</figcaption></div>
<div class="center"><button role="button">Knockout Knitting</button></div>
</div>
</div>
</figure>
</main>

Related

How to fix div width using scss (with react)

I want to fix div width (image) and get scroll-bar. But the more add 'div', the smaller width of div in wrapper div area. It doesn't appear scroll-bar.
This is image-wrapper below.
<div className="image-list-wrapper">{imageList}</div>
{imageList} are added by clicking button with 'div'
let imageList;
if(images){
imageList = images
.map((image, i) => {
return(
<div key={i} className="image-list-item">
<img className="image-resize" src={`http://localhost:5000`+image}/>
</div>
)
})
}
and CSS (SCSS)
.image-list-wrapper {
margin: auto;
margin-top: 1.5rem;
width: 95%;
height: 12rem;
border: 1px solid #bcbaba;
overflow-x: auto;
display: flex;
flex-direction: row;
align-items: center;
}
.image-list-item {
float: left;
width: 9rem;
height: 9rem;
margin: 0.5rem;
margin-top: -0.5rem;
}
.image-resize {
width: 100px;
height: 160px;
}
I got result below (screenshot)(please ignore other buttons.)
screen shot
screen shot2
The more images, the smaller images.
I want scroll-bar and fixed width of div items in wrapper div.
Your code will generate something like this:
<div class="image-list-wrapper">
<div key="0" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
<div key="1" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
<div key="2" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
<div key="3" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
<div key="4" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
<div key="5" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
<div key="6" class="image-list-item">
<img class="image-resize" src="https://via.placeholder.com/200x160">
</div>
</div>
and you should remove float left because you either use floated elements or you use flexbox.
.image-list-wrapper {
margin: auto;
margin-top: 1.5rem;
width: 95%;
height: 12rem;
border: 1px solid #bcbaba;
overflow-x: auto;
display: flex;
flex-direction: row;
align-items: center;
}
.image-list-item {
width: 9rem;
height: 9rem;
margin: 0.5rem;
margin-top: -0.5rem;
}
.image-resize {
width: 100px;
height: 160px;
}
which results into this: Demo here

Center images while maintaining responsive design

I'd like to push the three icons below towards the center of the page while still retaining a responsive layout.
Is display: grid; or display: row; more suitable?
And depending on your answer, what are the cleanest properties to apply?
<html>
<div id="contact">
<h1>Let's connect.</h1>
<div id="image-holder">
<div id="github-div">
<a href="https://github.com/klin-nj-97" target="_blank" id="profile-link">
<img src="https://image.flaticon.com/icons/svg/25/25231.svg" alt="github" class="contact-img">
</a>
</div>
<div id="linkedin-div">
<a href="https://www.linkedin.com/in/kevin-lin-33085a133/" target="_blank">
<img src="http://simpleicon.com/wp-content/uploads/linkedin.png" alt="linkedin" class="contact-img">
</a>
</div>
<div id="email-div">
<a href="mailto:kevin_lin#brown.edu">
<img src="https://image.freepik.com/free-icon/email-envelope-outline-shape-with-rounded-corners_318-49938.jpg" alt="email" class="contact-img">
</a>
</div>
</div>
</div>
</html>
<style>
#contact h1 {
text-align: center;
padding-top: 75px;
padding-bottom: 55px;
}
#image-holder {
display: flex;
flex-flow: row;
margin-left:
}
#contact a{
color: white;
}
.contact-img {
height: 35px;
width: 35px;
padding: 30px;
border-radius: 15px;
border: 2px solid black;
}
</style>
You should be use this simple trick.
Please give align-items: center; justify-content: center; into #image-holder
For more details Go to display:flex
Hope this help.
Let me know further clarifications.
#contact h1 {
text-align: center;
padding-top: 75px;
padding-bottom: 55px;
}
#image-holder {
display: flex;
flex-flow: row;
align-items: center;
justify-content: center;
}
#contact a{
color: white;
}
.contact-img {
height: 35px;
width: 35px;
padding: 30px;
border-radius: 15px;
border: 2px solid black;
}
<html>
<div id="contact">
<h1>Let's connect.</h1>
<div id="image-holder">
<div id="github-div">
<a href="https://github.com/klin-nj-97" target="_blank" id="profile-link">
<img src="https://image.flaticon.com/icons/svg/25/25231.svg" alt="github" class="contact-img">
</a>
</div>
<div id="linkedin-div">
<a href="https://www.linkedin.com/in/kevin-lin-33085a133/" target="_blank">
<img src="http://simpleicon.com/wp-content/uploads/linkedin.png" alt="linkedin" class="contact-img">
</a>
</div>
<div id="email-div">
<a href="mailto:kevin_lin#brown.edu">
<img src="https://image.freepik.com/free-icon/email-envelope-outline-shape-with-rounded-corners_318-49938.jpg" alt="email" class="contact-img">
</a>
</div>
</div>
</div>
</html>
Add the property justify-content: center; to the #image-holder id.
The buttons will be centered.
Code below
JSFiddle
The cleanest way to do this is to:
Use class-based selectors instead of ID selectors
Use flexbox to centre the layout (and text) horizontally, and centre the layout vertically
I've changed your HTML to use class-based selectors instead of IDs, e.g. class="contact" instead of id="contact":
<div class="contact">
<h1 class="contact__title">Let's connect.</h1>
<div class="contact__images">
<a class="contact__link" href="https://github.com/klin-nj-97" target="_blank">
<img src="https://image.flaticon.com/icons/svg/25/25231.svg" alt="github" class="contact__icon">
</a>
<a class="contact__link" href="https://www.linkedin.com/in/kevin-lin-33085a133/" target="_blank">
<img src="http://simpleicon.com/wp-content/uploads/linkedin.png" alt="linkedin" class="contact__icon">
</a>
<a class="contact__link" href="mailto:kevin_lin#brown.edu">
<img src="https://image.freepik.com/free-icon/email-envelope-outline-shape-with-rounded-corners_318-49938.jpg" alt="email" class="contact__icon">
</a>
</div>
</div>
For the cleanest CSS, it's ideal that all your selectors have the same level of specificity, and the best way to do that is use only class-based selectors. This will let you override styles more easily. You can read more about CSS specificity here.
The following CSS uses flexbox to position your content accordingly, assuming you are trying to centre everything vertically within the page:
body {
margin: 0; /* browser adds margins by default */
}
.contact {
display: flex;
flex-direction: column;
justify-content: center; /* centers your content horizontally */
align-items: center; /* centers your content vertically */
height: 100vh;
}
.contact__title {
margin: 0 0 55px; /* if you have a header you'd like to account for, the first value can be the header height */
}
.contact__images {
/* you don't even need anything here but the wrapping div of this classname is required to keep your icons aligned */
}
.contact__link {
text-decoration: none; /* proper way to hide the text link underline */
}
.contact__icon {
height: 35px;
width: 35px;
padding: 30px;
border-radius: 15px;
border: 2px solid black;
}
The CSS class naming convention I used is called BEM. I recommend reading more about it if you are interested in writing clean CSS. You can do so here or here.
I have a working example here on CodePen. You can change the height of the page to see it's centred vertically.

flexbox css centering and layout not working horizontally

I am trying to center the content of a navbar for mobile devices.
It should have 3 divs, the far left div should be the 3 bars for menu expansion (hamburger), the middle div should contain the logo, and the far right div should contain 3 inputs.
The problem is it is centering horizontally based on the left edge of the far right div. [= LOGO input input input] but it looks like this
[= LOGO input input input] if I take out all but 2 inputs
[= LOGO [input] it works perfectly but with 3 inputs it does not.
I have tried everything can anyone give me a clue as to why this won't work?
thanks
#media screen and (max-width: 1023px) and (min-width: 300px) {
#hidden-nav {
justify-content: space-between;
height: 8vh;
background-color: rgb(101, 0, 0);
display: flex;
padding: 8px;
min-width: 80px;
position: fixed;
width: 100%;
z-index: 10002;
}
}
#hidden-nav input {
max-width: 15vw;
}
body {
}
#hidden-nav:first-child {
/* padding-left: 2em;
border: 20px solid blue;*/
}
.bar {
width: 25px;
height: 2px;
background-color: white;
margin: 6px 0;
}
#container-for-right-hidden-nav {
border: 2px solid blue;
display: flex;
}
<div id="hidden-nav" style="border-bottom: 1px solid white; align-items:">
<div class="" onclick="toggleSidebar()">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<div style=""> <img height="35" width="50" src="https://seohackercdn-seohacker.netdna-ssl.com/wp-content/uploads/2011/07/Link-Searching.jpg?x68951" alt="logo" /> </div>
<img height="35" width="50" src="https://seohackercdn-seohacker.netdna-ssl.com/wp-content/uploads/2011/07/Link-Searching.jpg?x68951" alt="logo" />
</div>
<div id="container-for-right-hidden-nav">
<a href="/">
<input type="button" style="background-color: dodgerBlue;" value="floorplans">
</a>
<a href="/">
<input type="button" style="background-color: green;" value="apply">
</a>
<a href="/">
<input type="button" style="background-color: #003059;" value="contact">
</a>
</div>
Try justify Content center and align-items center.
Hope it Helps.
#hidden-nav {
display: flex;
justify-content: center;
align-items:center;
height: 8vh;
background-color: rgb(101, 0, 0);
padding: 8px;
min-width: 80px;
position: fixed;
width: 100%;
z-index: 10002; }
}

Why my text wont align with avatar?

I am using angular-material and materializecss. I'm not really strong with css but i tried to play with it and not much of results ...
I am trying to inline the avatar with the text so far i tried to use span with class to try style the text together with img but no success..
Best result i got so far is , text after avatar but slightly below avatar.
my HTML and custom css.
.gallery{
margin-bottom: 10px;
}
.responsive-img{
width: 100%;
}
.img-info{
text-align: center;
background: #b3e5fc;
color: black;
width: 100%;
margin-bottom: 10px;
padding: 10px 0;
}
.circle{
width: 60;
margin: 10px 5%;
}
.black-text{
text-align: center;
}
.circle .black-text{
display: inline;
}
<div class="row gallery">
<div class="col l3 col m4 col s12" ng-repeat="image in images">
<md-card class="z-depth-3">
<!--INSTAGRAM PHOTOS-->
<a>
<img class="responsive-img" ng-src="{{image.images.standard_resolution.url}}" alt="">
</a>
<!--USER AND AVATAR-->
<div class="img-info">
<img class="circle" ng-src="{{image.user.profile_picture}}">{{image.user.username}}
</div>
</md-card>
</div>
</div>
Use this:
.img-info img.circle{
vertical-align: middle;
}
And next time, share rendered HTML, the html you was shared is not useful.
snippet:
.img-info img.circle{
vertical-align: middle;
}
.gallery{
margin-bottom: 10px;
}
.responsive-img{
width: 100%;
}
.img-info{
text-align: center;
background: #b3e5fc;
color: black;
width: 100%;
margin-bottom: 10px;
padding: 10px 0;
}
.circle{
width: 60;
margin: 10px 5%;
}
.black-text{
text-align: center;
}
.circle .black-text{
display: inline;
}
<div class="row gallery">
<div class="col l3 col m4 col s12" ng-repeat="image in images">
<md-card class="z-depth-3">
<!--INSTAGRAM PHOTOS-->
<a>
<img class="responsive-img" ng-src="{{image.images.standard_resolution.url}}" alt="">
</a>
<!--USER AND AVATAR-->
<div class="img-info">
<img class="circle" src="http://dummyimage.com/50x50/000655/fff"> The text
</div>
</md-card>
</div>
</div>

Vertically align a row from bootstrap inside a div width issue

Trying to center a bootstrap row and it's contents inside a div. The code is below:
HTML:
<div class="horizontal-layout">
<div class="row">
<div class="col-md-3">
<div class="packet-icon">
<img src="~/Content/Images/Icons/ic_coversheet_blue.png" style="height:100px; width:100px; cursor:pointer;">
<h4>Add</h4>
</div>
</div>
<div class="col-md-3">
<div class="packet-icon">
<img src="~/Content/Images/Icons/ic_onepager_pink.png" style="height:100px; width:100px;cursor:pointer ">
<h4>Add</h4>
</div>
</div>
<div class="col-md-3">
<div class="packet-icon">
<img src="~/Content/Images/Icons/ic_user_profile_green.png" style="height:100px;width:100px;cursor:pointer ">
<h4>Add</h4>
</div>
</div>
<div class="col-md-3">
<div class="packet-icon">
<img src="~/Content/Images/Icons/ic_create_packet.png" style="height:100px; width:100px;cursor:pointer">
<h4>View</h4>
</div>
</div>
</div>
CSS:
.horizontal-layout {
/*background-color: blue;*/
min-height: 700px;
border: 1px solid;
border-radius: 5px;
border-color: #F26631;
padding: 30px;
}
.packet-icon {
text-align: center;
border: 1px solid;
border-radius: 5px;
border-color: #F26631;
min-height: 140px;
min-width: 140px;
}
RESULT:
But When I go and make changes to parent div(horizontal-layout) be displayed flex and the child div to align-middle, my width of row changes.
After Css Change:
.horizontal-layout {
/*background-color: blue;*/
min-height: 700px;
border: 1px solid;
border-radius: 5px;
border-color: #F26631;
padding: 30px;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.horizontal-layout > div {
display: inline-block;
vertical-align: middle;
}
OUTPUT:
Can anyone fix the row with issue?
By default, div was display: block, so that the width was automatically 100%.
Specifying .horizontal-layout > div to display: inline-block changed that behavior, so if you want to keep that, you have to set width manually:
.horizontal-layout > div {
...
width: 100%;
}

Resources