Bootstrap — Display Flex with margin - html

I have this HTML page, where I have an gallery of divs with texts inside it. I want the text to fit and come in the center of the div, but still want to keep the d-flex flex-wrap class for it to adjust based on the window size.
div.container-fluid {
padding: 5px !important;
margin: 0px !important;
}
div.word-card {
display: table-cell;
width: 300px;
height: 350px;
border: 1px solid black;
vertical-align: middle;
}
span.word-text {
font-size: 100px;
margin:0px 50px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid d-flex flex-wrap">
<div class="word-card">
<span class="word-text">squander</span>
</div>
<div class="word-card">
<span class="word-text">lead</span>
</div>
<div class="word-card">
<span class="word-text">accurate</span>
</div>
<div class="word-card">
<span class="word-text">Interdisciplinary</span>
</div>
</div>
Right now, the texts in the divs are not fitting it, which I want it to fit with margin of 50px on left and right. I also want the texts to vertically align, which stopped working after I added the d-flex flex-wrap class. Can someone please help me? I'm a total beginner in CSS.

The text is obviously to big for the word-card's height. Other than that, you could just match the line-height of the text with the height of the word-card, like so:
.word-card {
display: table-cell;
width: 300px;
height: 350px;
border: 1px solid black;
text-align: center;
}
.word-text {
font-size: 40px;
line-height: 350px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid d-flex flex-wrap">
<div class="word-card">
<span class="word-text">squander</span>
</div>
<div class="word-card">
<span class="word-text">lead</span>
</div>
<div class="word-card">
<span class="word-text">accurate</span>
</div>
<div class="word-card">
<span class="word-text">Interdisciplinary</span>
</div>
</div>
Or for a more flexible solution, you could use flexbox:
.word-card {
width: 300px;
height: 350px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
}
.word-text {
font-size: 40px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid d-flex flex-wrap">
<div class="word-card">
<span class="word-text">squander</span>
</div>
<div class="word-card">
<span class="word-text">lead</span>
</div>
<div class="word-card">
<span class="word-text">accurate</span>
</div>
<div class="word-card">
<span class="word-text">Interdisciplinary</span>
</div>
</div>

Firstly, you can work by lowering the font size of your text within the divs, since it seems a bit too large to fit within the div.
To center the text, try this:
New CSS Code
div.container-fluid {
padding: 5px !important;
margin: 0px !important;
text-align: center;
}
div.word-card {
display: table-cell;
width: 300px;
height: 350px;
border: 1px solid black;
}
span.word-text {
font-size: 20px;
}

Related

How to center a button while creating it responsive with CSS

I'm using basic HTML, CSS and Javascript to create a simple web app. As I tried text-align: center and other CSS elements to center the button, I'm still having trouble.. I'm also trying to center a text inside the box center, but doesn't work for unknown reason. So far, I have used margin-left and margin-right to adjust the button but willing to know how to center it without going through all these hassle.
.image {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50%;
margin-top: 40px;
margin-left: 50px;
}
.image img {
cursor: pointer;
}
.image img:hover {
border: solid 2px #1e88e5;
border-radius: 2px;
}
.image #mother img:hover {
border: solid 2px #1e88e5;
border-radius: 2px;
}
.image img {
width: 100px;
height: 100px;
text-align: center;
padding: 10px;
margin-left: 30%;
}
.button{
width: 80px;
height: 40px;
text-align: center;
margin: auto;
display: flex;
padding-top: 8px;
padding-bottom: 8px;
background-color: rgb(43, 43, 219);
cursor: pointer;
border-radius: 2px;
}
.button:hover{
background-color: #242424;
}
.button:active{
background-color: #121212;
}
.button > span{
color: #eeeeee;
text-align: center;
}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<title>Good Cop, Bad Cop</title>
<style>
.view-image img {
margin-left: 90px;
}
</style>
<link rel="stylesheet" href="../CSS/landing.css" />
<link rel="stylesheet" href="../CSS/coach_selection.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.6.1/firebase-ui-auth.css" />
</head>
<body>
<nav class="navbar navbar-custom">
<div class="container-fluid">
<a class="navbar-brand" href="">
<img src="../IMAGES/Hamburger_icon.svg" alt="Character" height="35vh" />
</a>
<a href="settings.html">
<ul class="navbar-nav">
<img src="../IMAGES/bootstrap-icons-1.3.0/person-circle.svg" alt="Profile" height="45vh" />
</ul>
</a>
</div>
</nav>
<div class="image">
<div>
<button type="button" id="dog-button" class="btn btn-light">
<img src="../images/dog.png" id="dog" alt="dog">
</button>
</div>
<div>
<button type="button" id="mother-button" class="btn btn-light">
<img src="../images/mother.png" id="mother" alt="mother">
</button>
</div>
<div>
<button type="button" id="soldier-button" class="btn btn-light">
<img src="../images/military.png" id="soldier" alt="soldier">
</button>
</div>
<div>
<button type="button" id="teacher-button" class="btn btn-light">
<img src="../images/teacher.png" id="teacher" alt="teacher">
</button>
</div>
<div class="button" id="button-confirm">
<span>Confirm</span>
</div>
<div class="view-image" style="display:none;">
<img src="" width="150" height="150" />
<div class="button" id="button-other"><span>Other</span></div>
</div>
</div>
<div class="fixed-bottom">
<div class="navbar navbar-custom">
<div class="container-fluid">
<div id="grid">
<div class="bot-nav-img">
<a href="Journal/journal_main.html">
<img src="../IMAGES/bootstrap-icons-1.3.0/book-fill.svg" alt="Journal" height="40vh" />
</a>
</div>
<div class="bot-nav-img">
<a href="landing.html">
<img src="../IMAGES/bootstrap-icons-1.3.0/house-door-fill.svg" alt="Home" height="40vh" />
</a>
</div>
<div class="bot-nav-img">
<a href="goals.html">
<img src="../IMAGES/bootstrap-icons-1.3.0/table.svg" alt="Goals" height="40vh" />
</a>
</div>
</div>
</div>
</div>
</body>
</html>
The quickest and easiest way to create a perfect centre alignment in css is with flexbox.
The general pattern is, give the parent element that needs it's children centered a display: flex;, a justify-content: center;, and a align-items: center;
For your code, get rid of the all the text-align. Then to the .button class, which is already display: flex, add justify-content: center; and align-items: center;
The span element with your text is a child of the button, so it will now become centered. The span element itself will not need any CSS properties to help it center.
.button {
width: 80px;
height: 40px;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
padding-top: 8px;
padding-bottom: 8px;
background-color: rgb(43, 43, 219);
cursor: pointer;
border-radius: 2px;
}
Can you please check the below code? Hope it will work for you. You have to just add your button code outside the .image class and add margin to the button like
.button {
margin: 10px auto 0px;
}
Please refer to this link: https://jsfiddle.net/yudizsolutions/av9fn6tp/
This solution is working for me.
.button {
display : flex;
justify-content: center !important;
align-items: center !important;
text-align: center !important;
margin: 0px auto 0px;
}

Align content in div: absolute to bottom AND relative to center

I have been trying to align content in a div to the bottom (which seems only to work with absolute) AND at the same time to position the content in the center.
In my code below, the problem refers to the images, which are either centered and at top OR left and bottom, but never both.
I have tried different CSS approaches as well as just using an old fashioned table for around the image, but nothing seems to work to get both at the same time.
How to position the images center and at bottom?
jsfiddle
.x-offer:nth-of-type(1) {
background-color: #c5d7d9;
}
.x-offer:nth-of-type(2) {
background-color: #e6e0cf;
}
.x-offer:nth-of-type(3) {
background-color: #debfb5;
}
.x-offer {
padding: 20px 0 0 0 !important;
}
.x-offer.nails > .flex-column {
width: 90%;
}
.x-offer > .flex-column {
width: 100%;
}
.x-offer > .flex-column > div:nth-of-type(2) {
bottom: 0px;
width: 75%;
margin: 0px auto;
position: absolute;
bottom: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.14.0/css/all.css" integrity="sha384-HzLeBuhoNPvSl5KYnjx0BT+WB0QEEqLprO+NBkkk5gbc67FTaL7XIGa2w1L0Xbgc" crossorigin="anonymous">
<div class="container">
<div class="row" style="height: 250px;">
<!-- STYLING -->
<div class="col-sm text-center d-flex align-items-top justify-content-center x-offer styling">
<div class="flex-column justify-content-center" style="border: 1px solid green;">
<div style="border: 1px solid;">
STYLING<p> </p>
</div>
<div style="border: 1px solid;">
<img src="http://davids134.sg-host.com/wp-content/uploads/2020/08/service1.gif" style="max-height: 50%; max-width: 100%">
</div>
</div>
</div>
<!-- HAIR -->
<div class="col-sm text-center d-flex align-items-top justify-content-center x-offer hair">
<div class="flex-column" style="border: 1px solid green;">
<div style="border: 1px solid;">
HAIR<p> </p>
</div>
<div style="border: 1px solid;">
<img src="http://davids134.sg-host.com/wp-content/uploads/2020/08/service3.gif" style="max-height: 50%; max-width: 100%">
</div>
</div>
</div>
<!-- NAILS -->
<div class="col-sm text-center d-flex align-items-top justify-content-center x-offer nails ">
<div class="flex-column" style="border: 1px solid green;">
<div style="border: 1px solid;">
NAILS<p> </p>
</div>
<div style="border: 1px solid;">
<img src="http://davids134.sg-host.com/wp-content/uploads/2020/08/service2.gif" style="max-height: 50%; max-width: 100%">
</div>
</div>
</div>
</div> </div>
Modify style of your .x-offer > .flex-column > div:nth-of-type(2)
Add left and right parameters, and set them to 0:
.x-offer > .flex-column > div:nth-of-type(2) {
bottom: 0px;
width: 75%;
margin: 0px auto;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
Here's fiddle
You should add the classes d-flex and align-items-center to the divs that currently have the flex-column class applied:
<div class="d-flex align-items-center flex-column" style="border: 1px solid green;">
You can see more about flex in bootstrap 4 documentation here: https://getbootstrap.com/docs/4.0/utilities/flex/
There are 2 things you need (I mark them in code).
A flex container
An item, which expands within this container (to push last element down)
.x-offer {
border: 1px solid green;
height: 250px;
}
.image-bottom-wrapper {
text-align: center;
}
.image-bottom-wrapper img {
max-width: 100px;
max-height: 50px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" />
<div class="container">
<div class="row">
<div class="col col-4 x-offer styling d-flex flex-column"><!-- [1] -->
<div class="headline">
STYLING
</div>
<div class="flex-grow-1 text"><!-- [2] -->
Some Text Here...
</div>
<div class="image-bottom-wrapper">
<img src="https://dummyimage.com/300x200/CCC/000" />
</div>
</div>
</div>
</div>

CSS: Icon / span in div prevents text from being aligned vertical center. How to fix this?

I encountered a strange behavior. Let's say you have a div that contains text and you want the text to be vertically centered. To achieve this you would set the height and line-height of the container to the same value. Everything works splendidly.
But now you decide to add an icon (via an icon font) to the mix. And you want this icon to be much larger than the text. Should be no problem. Just add a span or i and set the font-size to whatever you want. Should work.
But it doesn't. The text now suddenly hangs somewhere close to the bottom. Until you change the icon font-size to be the same as the container elements.
What is going on here?
#container {
line-height: 30px;
height: 30px;
font-size: 15px;
border: 1px solid black;
}
#icon {
font-size: 30px;
}
#icon2 {
font-size: 15px;
}
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet"/>
<div id="container">
Centered Text
</div>
<div id="container">
<span id="icon"><i class="fas fa-arrows-alt-v"></i></span>
Centered Text
</div>
<div id="container">
<span id="icon2"><i class="fas fa-arrows-alt-v"></i></span>
Centered Text
</div>
Below I'm using a flexbox for alignment. This way I don't have to take care of height and/or line-height.
#container {
font-size: 15px;
border: 1px solid black;
display: flex;
align-items: center; /* Vertical alignment */
}
#icon {
font-size: 60px;
}
#icon2 {
font-size: 30px;
}
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet"/>
<div id="container">
Centered Text
</div>
<div id="container">
<span id="icon"><i class="fas fa-american-sign-language-interpreting"></i></span> Centered Text
</div>
<div id="container">
<span id="icon2"><i class="fas fa-american-sign-language-interpreting"></i></span> Centered Text
</div>
All text is aligned to the bottom of the container. As your icon got larger it made the container larger and the rest of the text stayed aligned to the bottom.
What you want to do is set the icon to be vertically aligned to the middle of the container:
#icon {
vertical-align: middle;
}
#container {
line-height: 30px;
height: 30px;
font-size: 15px;
border: 1px solid black;
}
#icon {
font-size: 30px;
vertical-align: middle;
}
#icon2 {
font-size: 15px;
}
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet" />
<div id="container">
Centered Text
</div>
<div id="container">
<span id="icon"><i class="fas fa-arrows-alt-v"></i></span> Centered Text
</div>
<div id="container">
<span id="icon2"><i class="fas fa-arrows-alt-v"></i></span> Centered Text
</div>
The more generic solution:
#container > span {
vertical-align: middle;
}
#container {
line-height: 30px;
height: 30px;
font-size: 15px;
border: 1px solid black;
}
#container > span {
vertical-align: middle;
}
#icon {
font-size: 30px;
}
#icon2 {
font-size: 15px;
}
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet" />
<div id="container">
Centered Text
</div>
<div id="container">
<span id="icon"><i class="fas fa-arrows-alt-v"></i></span> Centered Text
</div>
<div id="container">
<span id="icon2"><i class="fas fa-arrows-alt-v"></i></span> Centered Text
</div>

Aligning items with flexbox

I have the mobile version of a static page looking good (although I am not sure if the HTML setup is ideal). I took the button out of the the .product-info class so I could use flexbox's order property and move it to below the product image on mobile. However, this leaves me unsure of how to go about styling the desktop version. I'm trying to get .product-info (blue), the 'get' button, and .store-info (green) each on their own separate lines. What is the best way to structure and style for the desktop version using flexbox without affecting the mobile layout? Thanks!
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Snapshelf</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="product-page.css" type="text/css">
</head>
<body>
<!-- Navigation -->
<nav class="header navbar navbar-light bg-faded">
<div class="nav navbar-nav container">
<a class="nav-item nav-link active" href="#">Snapshelf <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="#" style="float: right">Store Account</a>
</div>
</nav>
<!-- Holds product image and information -->
<div class="product container">
<!-- Displays front or back of product -->
<div class="product-image">
<img src="front.jpg" class="main-image img-fluid" alt="" />
<img src="back.jpg" class="secondary-image" alt=""/>
</div>
<!-- Displays product information -->
<div class="product-info">
<p class="brand-name"><b>Brand Name</b></p>
<p class="product-desc">Product Name</p>
<p class="price">$100</p>
</div>
<!-- Get button -->
<div>
<button class="get-button btn btn-default btn-lg">Get</button>
</div>
<!-- Store information -->
<div class="store-info">
<div>
<b>From</b><br>
Store Name
</div>
<div style="margin-top: 10px">
<b>Where</b><br>
Store Location<br>
www.storeurl.com
</div>
</div>
</div>
</body>
</html>
CSS
/* Large screens */
#media only screen and (min-width: 768px) {
.body {
font-family: 'Roboto', sans-serif;
}
.header {
margin-bottom: 40px;
border: 1px solid green;
}
.product {
display: flex;
border: 1px solid red;
}
.product-info {
border: 1px solid blue;
}
.get-button {
margin-top: 20px;
border-radius: 2px;
}
.product-image {
display: flex;
flex-direction: column;
}
.main-image {
height: 500px;
width: 500px;
margin-right: 20px;
}
.secondary-image {
height: 150px;
width: 150px;
margin-top: 20px;
}
.price {
margin-top: 10px
}
.store-info {
border: 1px solid green;
border-radius: 2px;
padding: 10px;
margin-top: 40px;
text-align: center
}
}
Just set your flexbox container to flex-direction: column in your desktop media query.
#media only screen and (min-width: 768px) {
.product {
display: flex;
flex-direction: column;
border: 1px solid red;
}
}

Display inline block text inside issue

I have been trying out display inline-block, everything worked out great if I didn't add anything into the divs but when I do the div collapsed and I don't know exactly why.
Any idea?
https://jsfiddle.net/giancorzo/ebqoptbd/
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>8 cajas</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div class="caja"></div>
<div class="caja-grande">
<div class="caja inline-block">
<span>Hola mundo</span>
</div>
<div class="caja inline-block">
</div>
<div class="caja inline-block">
CSS: </div>
</div>
<div class="caja inline-block"></div>
<div class="caja inline-block"></div>
<div class="caja"></div>
</body>
</html>
CSS:
.caja{
background-color: gray;
width: 100px;
height: 100px;
border: 1px solid black;
margin: 5px;
}
.inline-block{
display: inline-block;
}
.caja-grande{
background-color: gray;
border: 1px solid black;
padding: 5px;
width: 350px;
margin: 5px;
}
The issue is due to default vertical alignment of inline elements – baseline, the text inside element affects it and pushes div to the bottom. Use vertical-align: top, for example, to suppress this behavior.
JSFiddle
display:inline-block creates a small gap, so add font-size:0 to parent. plus add vertical-align:top since by default inline-block is baseline
with all of this you have fixed your issue.
here is the snippet:
.caja {
background-color: gray;
width: 100px;
height: 100px;
border: 1px solid black;
margin: 5px;
}
.caja-grande {
background-color: gray;
border: 1px solid black;
padding: 5px;
width: 350px;
margin: 5px;
font-size: 0
}
.inline-block {
display: inline-block;
vertical-align: top;
font-size: 16px;
}
<div class="caja"></div>
<div class="caja-grande">
<div class="caja inline-block">
<span>Hola mundo</span>
</div>
<div class="caja inline-block">
</div>
<div class="caja inline-block">
</div>
</div>
<div class="caja inline-block"></div>
<div class="caja inline-block"></div>
<div class="caja"></div>
you can find more info (and options on how to solve this in other ways) in this article Fighting the Space Between Inline Block Elements
Use overflow: hidden at .inline-block to make this work.
.inline-block{
display: inline-block;
overflow: hidden;
}