HTML CODE
CSS CODE
I want the 2 "product" div's to be next to eachother.but them both in the middle
i tired float left, and text align center (in all kinds of combinations and under every .class related but noting worked
text align center puts them both in the center but underneath eachother
text align center, followed by float left, does the same, or puts them both left.
.articles{
font-family: Arial, Helvetica, sans-serif;
}
.buy{
padding: 5px 5px 5px 5px;
font-size: 25px;
font-weight: bold;
text-decoration: none;
}
.product{
margin: 60px 0 60px 60px;
float: left;
}
.product img{
width: 200px;
height: 400px;
border: 10px solid tan;
}
I tied the combo:
" text-align: center;
float: left; "
both under .articles and under .product
also tried splitting them
(e.g. text-align under articles, and float under product. and other ways around)
Ok, you can do this with flexbox. Try this code right here:
<style>
.leftside {
background: #000599;
}
.rightside {
background: #006999;
}
.leftside, .rightside {
color: rgba(255,255,255,.4);
padding: 2%;
text-align: left;
}
#media ( min-width : 600px ){
body {
background: linear-gradient(90deg, #000599 50%, #006999 50%);
}
.flexbox {
width: 100%;
margin: 0 auto;
display: -webkit-flex;
display: flex;
}
.leftside,
.rightside {
-webkit-flex: 1;
flex: 1;
background: none;
}
}
</style>
<div class="flexbox">
<div class="leftside">
<p>
left
</p>
</div>
<div class="rightside">
<p>
right
</p>
</div>
</div>
Related
I have a wrapper div, a navigation menu bar (floated to the left) and a button (floated to the right). How do I center the navigation bar (floated to the left) within the wrapper minus the area the button (floated to the right) takes up?
So instead of it being centered directly in the middle of the wrapper, it will be centered to the the left a bit more because the area the button takes up is not within the (center) calculation; if you will. Here's a quick graphic:
<div class="wrapper">
<div class="nav">
Center Me
</div>
<div class="cta">
Book Now
</div>
</div>
.wrapper {
max-width: 1200px;
margin: 0 auto;
background: #000;
color: #fff;
overflow: hidden;
}
div:not(.wrapper) {
display: inline-block;
}
div.cta {
float: right;
width: 100px;
background: #444;
padding: 20px 0;
text-align: center;
}
div.nav {
float: left;
background: #777;
text-align: center;
padding: 20px 0;
margin: 0 auto;
}
Use flexbox. Floats can be difficult and behave inconsistently. Flexbox is a great utility.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox
.wrapper {
max-width: 1200px;
margin: 0 auto;
background: #000;
color: #fff;
overflow: hidden;
display:flex;
flex-wrap: nowrap;
}
div:not(.wrapper) {
/* display: inline-block; */
}
div.cta {
/* float: right; */
/* width: 100px; */
background: #444;
padding: 20px 0;
text-align: center;
flex: 0 0 100px;
}
div.nav {
/* float: left; */
background: #777;
text-align: center;
padding: 20px 0;
margin: 0 auto;
flex: 1 0 auto;
}
I removed float from your .nav and added width (totalling 100%) to each container to get this result.
.wrapper {
max-width: 1200px;
margin: 0 auto;
background: #000;
color: #fff;
overflow: hidden;
}
div:not(.wrapper) {
display: inline-block;
}
div.cta {
float: right;
width: 20%;
background: #444;
padding: 20px 0;
text-align: center;
}
div.nav {
width: 80%;
text-align: center;
padding: 20px 0;
margin: 0 auto;
}
<div class="wrapper">
<div class="nav">
Center Me
</div>
<div class="cta">
Book Now
</div>
</div>
I'm making a Christmas calendar for my girlfriend. I have some issues with centering the boxes and header. Header its not centered and the boxes starts more to the left than right. I have set the wrapper to margin auto 0. Didnt solve my problem. Any suggestions?
body {
background: url("http://hamawandi.com/images/bg4.png") no-repeat center center fixed;
background-size: cover;
font-size:30;
margin: 0;
color: #666;
}
.wrapper {
width:70%;
margin:0 auto;
margin-bottom:10px;
margin-top:50px;
}
.calender-box {
width:130px;
height:130px;
background-image: url('/images/background-xmas3.png');
float:left;
border: 5px dotted red;
margin-left:10px;
margin-bottom:10px;
text-align:center;
font-size:45px;
color:white;
line-height:140px;
text-decoration: none;
}
.header{
font-size:40px;
color:white;
margin:0 auto;
margin-top:-10px;
}
<div class="wrapper">
<div class="header">
❄ Julekalender 2016 ❄
</div>
1
2
3
4
5
6
7
8
</div>
My approach would be using flexbox:
body {
background: url("http://hamawandi.com/images/bg4.png") no-repeat center center fixed;
background-size: cover;
font-size:30;
margin: 0;
color: #666;
}
.wrapper {
width: 70%;
margin: 0 auto;
margin-bottom: 10px;
margin-top: 50px;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
/* you must add vendor prefixes to flexbox properties for cross-browser compatibility */
}
.header {
font-size: 40px;
color: white;
margin: 0 auto;
margin-top: -10px;
text-align: center;
width: 100%;
}
.calender-box {
width: 130px;
height: 130px;
background-image: url(/images/background-xmas3.png);
float: none;
border: 5px dotted red;
margin-left: 0;
margin-bottom: 10px;
text-align: center;
font-size: 45px;
color: white;
line-height: 140px;
text-decoration: none;
}
a {
width: 130px;
height: auto;
display: inline-block;
border: 5px solid transparent;
}
<div class="wrapper">
<div class="header">❄ Julekalender 2016 ❄</div>
1
2
3
4
5
6
7
8
<a></a>
<a></a>
<a></a>
<a></a>
</div>
The wrapper is actually perfectly centered in your example. It's just that the 70% area could only fit so many boxes and those boxes float left.
Any extra room is to the right of the last box which fits.
Notice the blue lines on both sides of the boxes which show where your wrapper div starts and ends.
You could make those boxes centered instead of float left or make the wrapper bigger like I do below.
if you go and reduce the width of the window to view the screen as if it were a mobile device you can see that the orange "badges" may not be entered (especially when only one badge fits per line) I want it to fit more badges in if possible whilst always keeping the badge, or group of badges on that line entered horizontally. The class is badge that isn't being centered Thank you in advance!! :)
jsfiddle: http://jsfiddle.net/avg24wrk/
This is the HTML
<div class="container">
<div class="sidebar">
<div class="sidebar-inner">
<p class="badge"><span class="vertical-align">Book a Free Consultation!</span></p>
<p class="badge"><span class="vertical-align">Second Point</span></p>
<p class="badge"><span class="vertical-align">Third Point</span></p>
</div>
</div>
and this is the CSS
* {
border: 0;
margin: 0;
padding: 0;
}
.sidebar {
float: left;
width: 25%;
color: #505050;
}
.sidebar-inner {
margin: 0 30px 0 35px;
}
.badge {
margin: 10px auto;
font-size: 24px;
text-align: center;
border-radius: 7px;
padding: 20px 20px;
background-color: #ed9727;
cursor: pointer;
}
#media screen and (max-width: 490px) {
.sidebar {
width: 100%;
}
.sidebar-inner {
padding-bottom: 20px;
border-bottom: 2px solid #505050;
margin: 0 30px;
overflow: hidden;
}
.badge {
float: left;
margin: 15px 10px;
max-width: 150px;
min-height: 50px;
display: table;
}
}
have you tried adding text-align: center; to class you want to center
since i you didn't mention which class you want to center so i will give you a simple rule try this
please mention class you want to center
I have a problem creating a decent header in CSS. What I want is a <h1> header that aligns its content in the center of its parent <div>. Sometimes though there might be an additional logo displayed as a regular <img /> which should be aligned to the left.
This is my example code:
<div class="container">
<div class="logo">
<img src="http://www.oldfirestation.co.uk/logo_brand_example_86.jpg" />
<h1>Not center?</h1>
</div>
<div class="more">
This is the center
</div>
</div>
And my CSS:
body {
background-color: #161616;
}
div.container {
background-color: #fff;
width: 70%;
margin: auto;
}
div.logo img {
width: 200px;
float: left;
}
h1 {
text-align: center;
font-size: 1.4em;
margin: 0px auto;
padding: 0px 0px 5px 0px;
width: 50%;
}
div.more {
text-align: center;
margin-top: 50px;
clear: left;
}
The problem is that when I show an <img />, my <h1> text is NOT centered. If I remove this <img /> it is... How can I fix it??
I have made an example on JSFiddle here: http://jsfiddle.net/8B9ZF/
You do like this:
div.logo img {
width: 200px;
vertical-align:middle;
}
h1 {
text-align: center;
font-size: 1.4em;
margin: 0px auto;
padding: 0px 0px 5px 0px;
width: 50%;
display:inline-block;
}
http://jsfiddle.net/8B9ZF/8/
May be you can change your mark-up
http://jsfiddle.net/8B9ZF/24/
If you make the image absolutely positioned at 0,0 instead of floating it then it won't push the H1 out of center alingment. But you then run the danger of the image overlapping the text if the image is too wide, or the container of the heading too small. To counter this, you probably want to add some padding to the left/right of the container
http://jsfiddle.net/8B9ZF/27/
this should always work as far as i know! basically this just adds overflow hidden, which makes the h1 aware of the space taken by the floated element so it takes up the remaining area!
body {
background-color: #161616;
}
div.container {
background-color: #fff;
width: 70%;
margin: auto;
}
div.logo{
overflow:hidden
}
div.logo img {
width: 200px;
float: left;
}
h1 {
text-align: center;
font-size: 1.4em;
padding: 0px 0px 5px 0px;
}
div.more {
text-align: center;
margin-top: 50px;
clear: left;
}
I'm still new in CSS, sorry for the long post. I have the following code
<style type="text/css">
.btn {
float: left;
clear: both;
background: url(images/btn_left.png) no-repeat;
padding: 0 0 0 10px;
margin: 5px 0;
}
.btn a{
float: left;
height: 40px;
background: url(images/btn_stretch.png) repeat-x left top;
line-height: 40px;
padding: 0 10px;
color: #fff;
font-size: 1em;
text-decoration: none;
}
.btn span {
background: url(images/btn_right.png) no-repeat;
float: left;
width: 10px;
height: 40px;
}
.btn_addtocart { background-color: green; }
.btn_checkout { background-color: red; }
</style>
</head>
<body>
<div class="btn btn_addtocart">Add to Cart<span></span></div>
<div class="btn btn_checkout">Check Out<span></span></div>
</body>
</html>
I'm trying to center each button in the middle of the page (horizontal alignment), how can I accomplish that? I tried playing with the padding and the margin but it messes my background image.
Here is jsFiddle
try margin auto, text-align center, fixed width for middle part..
oh ..and get rid of the float, and dont forget the ';'
edit code..
.btn {
clear: both;
background: url(images/btn_left.png) no-repeat;
padding: 0 0 0 10px;
display: block;
margin: 5px auto;
text-align: center;
width: 120px;
}
.btn a {
height: 40px;
background: url(images/btn_stretch.png) repeat-x left top;
line-height: 40px;
padding: 0 10px;
color: #fff;
font-size: 1em;
text-decoration: none;
}
.btn span {
background: url(images/btn_right.png) no-repeat;
width: 10px;
height: 40px;
}
.btn_addtocart { background-color: green; }
.btn_checkout { background-color: red; }
You can text-align:center the links inside the divs (which are block-level elements) to center them inside their containers but you will have to make a couple of tweaks. Try this:
.btn {
clear: both;
background: url(images/btn_left.png) no-repeat;
padding: 0 0 0 10px;
margin: 5px 0;
text-align:center;
}
.btn a {
height: 40px;
background: url(images/btn_stretch.png) repeat-x left top;
line-height: 40px;
padding: 10px;
color: #fff;
font-size: 1em;
text-decoration: none;
}
.btn span {
background: url(images/btn_right.png) no-repeat;
float: left;
width: 10px;
height: 40px;
display: block;
}
.btn_addtocart a { background-color: green; }
.btn_checkout a { background-color: red; }
Demo
http://jsfiddle.net/andresilich/UtXYY/1/
A couple things you can do
.btn {
display: block
margin-left: auto;
margin-right: auto;
}
By default a button is an inline element, so margins will no work. Setting display to block, will make it act like a
div.btnParent {
text-align:center
}
The other method is to have the button's containing element text-align center. The may not necessarily always work, as there may be more content in this container that you do not want to be centered.
I can't fully see from your code snippet but to centre somthing in the middle of its parent, you need to set its margin to auto.
margin: auto
and its width
width: 100px:
EDIT:
Also remove any float: styles you have on the element.