Centering Image and Text - html

I am creating a web page with 2 <div>s side by side. Each <div> will have 2 sections.
I want to center them (bring them to the middle of the <div>). I am trying to make this <div> responsive. In the website, 2 <div>s will be in one line, while in mobile one <div> will appear on one line and the other <div> will appear on a second line. I am trying to center the image and text of each section.
How can I accomplish that?
.wrapper {
width: 100%;
overflow: hidden;
}
.wrapper div {
min-height: 45px;
padding: 1px;
}
#one {
background-color: gray;
float: left;
width: 50%;
}
#two {
background-color: red;
overflow: hidden;
min-height: 45px;
}
#media screen and (max-width: 400px) {
#one {
float: none;
margin-right: 0;
width: auto;
border: 0;
}
}
<div class="wrapper">
<div id="one">
<img src="http://livebodybuilding.com/images/fast-delivery.png" height="26" width="55" style="float:left; margin-top: 6px;" />
<p style=" font-size:13px; color:#fff; line-height:1.5; font-family: 'Montserrat',sans-serif;"><strong> FREE DELIVERY </strong>ORDERS OVER $100</p>
</div>
<div id="two">
<img src="http://livebodybuilding.com/images/free-gift.png" height="26" width="31" style="float:left; margin-top: 6px;" />
<p style="font-size:13px; color:#fff; line-height:1.5; font-family: 'Montserrat',sans-serif;"><strong> FREE GIFT</strong> ORDERS OVER $100</p>
</div>
</div>
My fiddle: https://jsfiddle.net/4okxw32v/

First of all, the use of floats in layout design is discouraged. It is generally not a good way of doing things, and usually if you're having a layout issue it comes down to a float problem. Instead you should use the display: inline-block setting. When using inline-block there are a couple of things to take into consideration.
Any white space between elements will be shown. To combat this you can set font-size: 0 on the wrapper and then set font-size: 1rem on the children. This will set the font size in the content to the same size as that of the html selector.
You can prevent line-breaking if you set white-space: nowrap on the parent and then set white-space: initial on the children.
Next instead of adding an image and floating it inside the child you can use the css pseudo element ::before (or css2 :before) on the text container inside the element.
Finally to center the contents use text-align: center on the parent
*, *::before, *::after {
box-sizing: border-box;
}
html,
body {
padding: 0px;
margin: 0px;
}
.wrapper {
font-size: 0;
}
.wrapper div {
font-size: 1rem;
min-height: 45px;
padding: 1px;
text-align: center;
font-size: 13px;
color: #fff;
line-height: 1.5;
font-family: 'Montserrat', sans-serif;
overflow: hidden;
display: inline-block;
width: 50%;
}
#one {
background-color: gray;
}
#one p:before {
content: "";
display: inline-block;
width: 4em;
height: 2em;
background-image: url(http://livebodybuilding.com/images/fast-delivery.png);
background-size: cover;
vertical-align: middle;
margin-right: 5px;
}
#two {
background-color: red;
overflow: hidden;
min-height: 45px;
}
#two p:before {
content: "";
display: inline-block;
width: 2.5em;
height: 2em;
background-image: url(http://livebodybuilding.com/images/free-gift.png);
background-size: cover;
vertical-align: middle;
margin-right: 5px;
}
#media screen and (max-width: 620px) {
.wrapper div {
width: 100%;
}
}
<div class="wrapper">
<div id="one">
<p><strong>FREE DELIVERY</strong> ORDERS OVER $100</p>
</div>
<div id="two">
<p><strong>FREE GIFT</strong> ORDERS OVER $100</p>
</div>
</div>

Related

HTML/CSS - Dealing With Contents That Refuse To Vertically Center

I've been messing with a bunch of code that refuses to center within my divs that are floated left and right. For my left one, a span and an image are stuck in the bottom left, while on the right, normal text is stuck on the top-right.
Here's what it currently looks like.
As for my code, here it is. If I can get help on what I could maybe do to fix it, I'd appreicate it.
HTML:
<div class="header">
<div class="allSelector">
<!-- <span (click)="showAll()"><img src={{imgSource}} /> {{expandContractStatement}}</span> -->
<img src={{imgSource}} (click)="showAll()"/><span (click)="showAll()" class="selectorInsides">{{expandContractStatement}}</span>
</div>
<div class="legend">
Incidents
Tasks
CRs
Problems
</div>
</div>
CSS:
.header {
background-color: gray;
color:black;
font-size: 24pt;
line-height: 24pt;
height: 6%;
max-height: 6%;
overflow: hidden;
vertical-align: middle;
}
.allSelector {
width: 50%;
max-height: 100%;
height: 100%;
float: left;}
.selectorInsides { /*Threw this in because of some issues before*/
overflow: hidden;
}
.allSelector > img {
height: 80%;
object-fit: contain;
}
.legend {
line-height: 24pt;
max-height: 100%;
height: 100%;
width: 50%;
float: right;
margin: 0;
text-align: right;
vertical-align: middle;
}
It's hard to tell if this answer is sufficient since a minimal, reproducible example wasn't provided, but you use Flexbox to vertically align elements.
/* Vertically align elements */
.header,
.allSelector,
.legend {
display: flex;
align-items: center;
}
.selectorInsides {
/* Adjust the spacing between image and text as needed */
margin-left: 8pt;
}
/* Vertically align elements */
.header,
.allSelector,
.legend {
display: flex;
align-items: center;
}
.header {
background-color: gray;
color: black;
font-size: 24pt;
line-height: 24pt;
height: 6%;
max-height: 6%;
overflow: hidden;
vertical-align: middle;
}
.allSelector {
width: 50%;
max-height: 100%;
height: 100%;
float: left;
}
.selectorInsides {
/*Threw this in because of some issues before*/
overflow: hidden;
/* Adjust the spacing between image and text as needed */
margin-left: 8pt;
}
.allSelector>img {
height: 80%;
object-fit: contain;
}
.legend {
line-height: 24pt;
max-height: 100%;
height: 100%;
width: 50%;
float: right;
margin: 0;
text-align: right;
vertical-align: middle;
}
<div class="header">
<div class="allSelector">
<!-- <span (click)="showAll()"><img src={{imgSource}} /> {{expandContractStatement}}</span> -->
<img src="https://via.placeholder.com/32" (click)="showAll()" /><span (click)="showAll()" class="selectorInsides">Expand All</span>
</div>
<div class="legend">
Incidents Tasks CRs Problems
</div>
</div>

Why does block with text shift to bottom?

Why does block with text shift to the bottom? I know how to fix this issue (need to add "overflow: hidden" to the box), but I don't understand why it shift to the bottom, text inside the box is short, margins in browser-inspector are same as margins of example without text.
Example of the problem
HTML:
<div class="with-text">
<div class="box1">
SIMPLE TEXT
</div>
<div class="box2">
</div>
</div>
<div class="without-text">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
CSS:
html, body {
font-size: 10px;
margin: 0;
height: 100%;
}
.box1 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: blue;
/* Fix the problem */
/* overflow: hidden; */
color: white;
}
.box2 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: red;
}
.with-text:before {
display: block;
content: "with-text";
text-transform: uppercase;
margin: 1rem;
}
.with-text {
box-sizing: border-box;
height: 50%;
border: 1px solid;
}
.without-text:before {
display: block;
content: "without text";
text-transform: uppercase;
margin: 1rem;
}
.without-text {
box-sizing: border-box;
height: 50%;
border: 2px solid black;
}
The problem is that by default vertical alignment of inline elements – baseline,
The text inside element affects it and pushes div to the bottom.
Use vertical-align: top to solve issue.
You can try to add vertical-align:
.box1 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: blue;
/* overflow: hidden; */
color: white;
vertical-align:top;
}

How to perfectly center text on an inline-display block with no definitive height correctly in this very standard yet unanswered way?

I've managed to use the preceding <span> method of centering items, which is all well, except for one little tiny issue illustrated in this image: http://imgur.com/UoKFW6P
how do i fix this? here is my css and html:
* {
padding: 0px 0px 0px 0px;
margin: 0px 0px 0px 0px;
text-align: center;
}
#font-face {
font-family: myfirstfont;
src: url(century-gothic.ttf);
}
body,
html {
height: 100%;
white-space: -0.125em;
background-color: rgb(0, 0, 0);
}
#wrapper {
height: inherit;
min-height: 100%;
}
.O1-3 {
color: white;
width: 100%;
height: 33.5%;
display: inline-block;
text-align: center;
font-family: myfirstfont;
}
div span {
height: 100%;
vertical-align: middle;
display: inline-block;
}
div a {
font-size: 35px;
display: inline-block;
font-style: italic;
text-decoration: none;
}
#media screen and (min-width:600px) {
.O1-3 {
height: inherit;
width: 33.3%;
}
}
</style>
</head>
<body>
<div id="wrapper">
<div class="O1-3" id="one">
<span></span>
<a id="n-textonly">Luis Rojas</a>
</div>
<div class="O1-3" id="two">
<span></span>
<a id="c-textonly">Contact</a>
</div>
<div class="O1-3" id="three">
<span></span>
<a id="rw-textonly">Recent Work</a>
</div>
</div>
</body>
</html>
There are multiple methods of centering in css, but without a definitive height, you're confined to a just few methods. Here are a few techniques I put together and commented out for you to take a look at: https://jsfiddle.net/73mtxgcc/3/
There are other techniques such as using flex or using display:table, but I find those, personally, either sloppy or not quite supported enough. As well, if you have something relative to the window, then you can always also use
.element{
display: inline-block;
margin: 0 auto;
}
with .element being the parent of the content you'd like to center.
If you have any more questions, or if you need any further clarifications, then please don't hesitate to ask!
You want to give all three columns the same padding with auto width, to get the text to be evenly spaced, then the wrapper centers the entire group. You get same amount of space on each side of the group, and then equal spacing in-between, so the center column ends up not being in the exact center of the page, but it has a very balanced visual effect for the page as a whole. You still need some adjustments with break-points though, I added one.
#wrapper {
width: auto;
height: inherit;
min-height: 100%;
}
.O1-3 {
padding: 1em;
color: white;
width: auto;
height: 33.5%;
display: inline-block;
text-align: center;
font-family: myfirstfont;
}
#media screen and (min-width:600px) {
.O1-3 {
padding: 2em;
height: inherit;
}
}
#media screen and (min-width:1000px) {
.O1-3 {
padding: 5em;
}
}
When it comes to formatting HTML content with CSS, you should really use Flexbox. It is becoming really popular and is widely supported. In the code below, I have added a display: flex; property to your #wrapper. This creates a flex-box out of the wrapper. From there, I added a flex-direction of row, which will automatically format it's three child elements horizontally. In the media query, I have changed the flex-direction property so that the wrapper's child elements are formatted vertically.
-- Hope this helps.
<html>
<head>
<style>
* {
padding: 0px 0px 0px 0px;
margin: 0px 0px 0px 0px;
text-align: center;
}
#font-face {
font-family: myfirstfont;
src: url(century-gothic.ttf);
}
body,
html {
height: 100%;
white-space: -0.125em;
background-color: rgb(0, 0, 0);
}
#wrapper {
height: inherit;
min-height: 100%;
display: flex;
flex-direction: row;
}
.O1-3 {
color: white;
width: 100%;
height: 33.5%;
text-align: center;
font-family: myfirstfont;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
div span {
height: 100%;
vertical-align: middle;
display: inline-block;
}
div a {
font-size: 35px;
display: inline-block;
font-style: italic;
text-decoration: none;
}
#media screen and (max-width:600px) {
.O1-3 {
height: inherit;
width: 100%
margin: auto;
}
#wrapper {
flex-direction: column;
}
}
</style>
</head>
<body>
<div id="wrapper">
<div class="O1-3" id="one">
<span></span>
<a id="n-textonly">Luis Rojas</a>
</div>
<div class="O1-3" id="two">
<span></span>
<a id="c-textonly">Contact</a>
</div>
<div class="O1-3" id="three">
<span></span>
<a id="rw-textonly">Recent Work</a>
</div>
</div>
</body>
</html>

float element to right of div appears outside

Well, I'm back to doing front-end stuff -- which is really not my forté -- and I'm stumped.
I have a div with a logo, and I need to put some text in-line with it, vertically centered within the div. Every time I try to float the element right, it seems to end up outside of the div (somehow?).
Relevant HTML:
<div id="wrapper-header">
<div id="wrapper-nav-header">
<div id="header-logo">
<span id="header-logo-span"><img src="mylogo.png" /></span>
<span id="header-customer-name">Customer Name</span>
</div>
</div>
...
</div>
Relevant CSS:
#wrapper-header {
background-color: #fff;
position: fixed;
z-index: 999;
width: 100%;
padding: 0;
white-space: nowrap;
font-size: 13px;
line-height: 1
}
#wrapper-header #header-logo {
margin-left: 20px;
padding: 10px 0;
}
#wrapper-header #header-logo img {
max-height: 40px;
vertical-align: middle;
}
#wrapper-header #header-customer-name {
vertical-align: middle;
}
What do I need to do to get this header-customer-name element to float right?
You can try using flex box:
html, body{
margin: 0;
padding: 0;
}
#wrapper-header {
background-color: #fff;
position: fixed;
z-index: 999;
width: 100%;
padding: 0;
white-space: nowrap;
font-size: 13px;
line-height: 1
}
#header-logo {
margin-left: 20px;
padding: 10px 0;
display: flex;
align-items: center;
}
#header-logo img {
max-height: 40px;
vertical-align: middle;
}
#header-customer-name {
flex-grow: 1;
text-align: right;
}
<div id="wrapper-header">
<div id="wrapper-nav-header">
<div id="header-logo">
<span id="header-logo-span"><img src=http://i.stack.imgur.com/gijdH.jpg?s=328&g=1" /></span>
<span id="header-customer-name">Customer Name</span>
</div>
</div>
</div>
or you can calculate an appropriate padding to your header-customer-name like this:
html, body{
margin: 0;
padding: 0;
}
#wrapper-header {
background-color: #fff;
position: fixed;
z-index: 999;
width: 100%;
padding: 0;
white-space: nowrap;
font-size: 13px;
line-height: 1
}
#header-logo {
margin-left: 20px;
padding: 10px 0;
}
#header-logo img {
max-height: 40px;
vertical-align: middle;
}
#header-customer-name {
float: right;
padding: 12px 0; /*you can change for a more precise value*/
}
<div id="wrapper-header">
<div id="wrapper-nav-header">
<div id="header-logo">
<span id="header-logo-span"><img src=http://i.stack.imgur.com/gijdH.jpg?s=328&g=1" /></span>
<span id="header-customer-name">Customer Name</span>
</div>
</div>
</div>
Anyway if you're referring that the header-customer-name is outing on the right of the #wrapper-header that is probably because it has position: fixed and width: 100%, that's why I add the style:
html, body{
margin: 0;
padding: 0;
}
Check my answer here to better understanding White Space Appears Right of Browser Window When at Full Screen
This has no relation with the question but can help you
Don’t Overqualify
As a general rule, don’t supply more information than is necessary.
// bad
ul#someid {..}
.menu#otherid{..}
#id1 #id2{...}
// good
#someid {..}
#otherid {..}
#id2{...}
Try in fiddle
Change style to this way..
#wrapper-header #header-customer-name {
vertical-align: middle;
float: right;
padding-top: 15px; /*value suits to your logo */
}
Do you have CSS reset code in CSS files which is linked with this page. After the is below css reset added, the problem was solved. Demo
html, body {
margin:0;
padding:0;
}

Margin: 0 auto; not working on a div the needs to inherit a width from 2 enclosed elements

I am trying to center the flame and the heading to the middle of the white box.
HTML
<div class="contentheading">
<div class="floatmiddle">
<img src="images/flame45x45.png">
<h3>Receive only the email you want.</h3>
</div>
</div>
CSS
.contentheading {
position: relative;
height: 45px;
margin-top: 30px;
width: 636px; //this is the full width of the white box//
}
.floatmiddle {
margin: 0 auto;
height: 45px;
display: block;
}
.contentheading img {
position: absolute;
}
.floatmiddle > h3 {
font-family: "signika";
font-size: 22px;
font-weight: 500;
color: #37434f;
height: 45px;
line-height: 45px;
margin: 0 0 0 60px;
text-align: center;
vertical-align: top;
position: absolute;
}
I need the .float middle to inherit the width of the two enclosing elements - the image (45 x 45px) and the text (which will be different length for each chapter i have) so i need one class/formula so i can just go through and pop in the headings and no matter the headings length the heading and the fireball will be centered within the white div.
You can use display: inline-block; to center this div.
http://jsfiddle.net/d8gyd9gu/
HTML
<div class="contentheading">
<div class="floatmiddle">
<img src="http://www.neatimage.com/im/lin_logo.gif" alt="">
<h3>Receive only the email you want.</h3>
</div>
</div>
CSS
.contentheading {
height: 45px;
margin-top: 30px;
width: 636px;
text-align: center;
}
.floatmiddle {
height: 45px;
display: inline-block;
}
.contentheading img {
float: left;
margin: 20px 10px 0px 0px;
}
.floatmiddle > h3 {
font-family: "signika";
font-size: 22px;
font-weight: 500;
color: #37434f;
height: 45px;
line-height: 45px;
padding: 0px 0px 0px 60px;
}
If you can use flexbox you can do it really simply like this:
.contentheading {
border: 1px dashed #ff0000;
margin-top: 30px;
width: 636px;
display: flex;
justify-content: center;
align-items: center;
}
.contentheading h3 {
font-family: "signika";
font-size: 22px;
font-weight: 500;
color: #37434f;
}
<div class="contentheading">
<img src="images/flame45x45.png" width="45" height="45" />
<h3>Receive only the email you want.</h3>
</div>
If you need to support older browsers make sure you add the prefixed versions.
You can definitely pare your markup and styling down. If you only need to center the text and the image in a div of a fixed width, you can simply use text-align: center on the parent container, and display: inline-block on the two elements within. The following markup and styling is about as little as you need:
HTML
<div class="content-heading">
<img src="images/flame45x45.png">
<h3>Receive only the email you want.</h3>
</div>
CSS
.content-heading {
background-color: #ccc;
height: 45px;
margin: 0 auto; /** Centers on the page **/
text-align: center;
width: 636px;
}
h3 {
display: inline-block;
line-height: 45px; /** Only really works if you can rely on only displaying one line of text **/
margin: 0;
overflow: hidden; /** Need this to keep inline-block elements from staggering **/
padding: 0;
}
img {
background-color: black; /** Purely so we can see this **/
display: inline-block;
height: 45px;
width: 45px;
}
That's really all you need.
Codepen sketch