I have the following CSS. It needs to be centered on any screen but currently it is only centered on a certain kind of screen:
#import url(http://fonts.googleapis.com/css?family=Roboto+Condensed);
html{
/* font-family: 'Roboto Condensed', sans-serif;*/
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
font-weight: 20;
}
#description{
position: absolute;
top: 200px;
left: 400px;
}
#content{
margin: 0 auto;
height: 100%;
width: 100%;
text-align: center;
line-height: 100%;
display:table-cell;
vertical-align: middle;
}
h1{
color:#4A4A4A;
margin: 0 auto;
text-align: center;
}
h1:hover{
color:#4A4A4A;
}
h2{
color:#4A4A4A;
margin: 0 auto;
text-align: center;
}
h2:hover{
color:#4A4A4A;
}
a{
color:black;
text-decoration: none;
margin: 0px auto;
width: 400px;
}
.circular{
display: block;
margin-left: auto;
margin-right: auto;
width: 150px;
height: 150px;
border-radius: 75px;
-webkit-border-radius: 75px;
-moz-border-radius: 75px;
background: url(./images/profile_picture.jpg) no-repeat;
box-shadow: 0 0 8px rgba(0, 0, 0, .8);
-webkit-box-shadow: 0 0 8px rgba(0, 0, 0, .8);
-moz-box-shadow: 0 0 8px rgba(0, 0, 0, .8);
}
ul{
padding: 0;
text-align:center;
padding-top: 5
}
ul a img {
padding-top: 20px;
}
li{
list-style: none;
text-align: center;
font-size: 30px;
display: inline-block;
margin-right: 10px;
}
#back{
position:absolute;
top:0;
left:0;
bottom:0;
right: 0;
z-index: -99999;
background-color: #f7f7f7;
background-size: cover;
background-position: center center;
opacity: .2;
}
Currently my website only centers on a 4x3 screen. How can I adjust my CSS to center on any size screen?
Edit: Same question is here with HTML.
You have not posted your HTML code (dont know why (?))!
Well the trick to center an element with CSS is to give the element a left and right margin value of auto
#wrapper
{
width: 500px;
margin-left: auto;
margin-right: auto;
}
This is also similar to margin:0 auto which is widely used !
Wrap your content using a <div id="wrapper">...</div> and set a width !
To center an image, add display:block to it since by default the images have display:inline property.
#myImage
{
width: 50px;
height: 50px;
margin-left: auto;
margin-right: auto;
display: block;
}
Update: The answer from Magesh Kumaar is better, but this still works.
Without seeing the code on your page, it is a little difficult. But to center a DIV on any screen you should use the following code:
#divID {
position: absolute;
width:400px;
left:50%;
margin-left:-200px; /*This should always be 50% of your width*/
}
You should then wrap the rest of your website in the DIV.
<div id="divID">
YOUR WEBSITES CONTENT GOES HERE
</div>
Wrap your content, set width, and add margin: 0 auto to the wrap's css
<div id="body">
<div id="wrap">
<div id="center">Center</div>
</div>
</div>
#wrap {
margin:0 auto;
width:50%;
}
Fiddle
Firstly define the Div id in HTML as:
<div id="page">
website content
</div>
Then in CSS: set width, and add margin: 0 #page {
margin:0 auto;
width:50%;
}
Related
I am trying to size video containers height but for some reason, its height is unnecessarily big.
CSS:
.indexVideoContainer{
display:flex;
flex-direction: column;
background-color: #fff;
background-image: url(https://bellroy.imgix.net/cms_images/1259/background-texture-repeat.jpg?auto=format&fit=max);
width: 100%;
}
.indexVideoText{
position: relative;
font-family: "PT Serif", serif;
-webkit-font-smoothing: antialiased;
text-align: center;
margin: 5px auto 20px auto;
font-weight: 400;
/* line-height: 32px; */
color: #666;
font-weight: 500;
font-size: 25px;
line-height: 40px;
margin: 0 auto 40px auto;
max-width: 55%;
top:50px;
}
.videoContainer{
position:relative;
padding-bottom:60.25%;
padding-top:30px;
height:0;
overflow:hidden;
border:1px solid black;
}
.videoContainer iframe{
position:absolute;
margin: auto;
top: 35%;
left: 50%;
transform: translate(-50%, -50%);
width:70%;
height:60%;
}
Codepen:
https://codepen.io/anon/pen/MZNgYa
Check the .videoContainer class. I added a border for a better demonstration. Its height is unnecessarily big. Expands too much for some reason. How do I make it shorter in a responsive way?
problem image
Edit:
I still have the problem guys.I have managed to fix it with some workaround but i guess i am missing something with video frame and flexbox.
html:
<div class="indexVideoText">
sometext
</div>
<div class="videoContainer">
<iframe title="hQ6TSIFM8I0" width="80%" height="80%" frameBorder="0" src="https://www.youtube-nocookie.com/embed/hQ6TSIFM8I0?autoplay=1&loop=1&playlist=hQ6TSIFM8I0&origin=bellroy.com&modestbranding=1&showinfo=0&rel=0&version=3&mute=1"></iframe>
</div>
CSS
.indexVideoText{
position: relative;
font-family: "PT Serif", serif;
-webkit-font-smoothing: antialiased;
text-align: center;
margin: 5px auto 20px auto;
font-weight: 400;
/* line-height: 32px; */
color: #666;
font-weight: 500;
font-size: 25px;
line-height: 40px;
margin: 0 auto 40px auto;
max-width: 55%;
top:50px;
}
.videoContainer{
position:relative;
height:500px;
overflow:hidden;
border:1px solid black;
background-color:red;
padding-top:20px;
padding-bottom:20px;
}
iframe{
position:absolute;
margin: auto;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width:70%;
}
I'm trying to create a template for some rotating spotlights we create here at work, and what I'm trying to do should be rather simple.
Here is my fiddle: http://jsfiddle.net/aeromax/2t4k4cf9/
What I'm trying to do, is to get #spotlightCTA to float right (easily doable) and align to the bottom of the #spotlightTitle, so that the call to action moves up or down depending on how tall the title is.
Here is the styles associated with these two divs:
#spotlightTitle {
width: auto;
height: auto;
display: inline-block;
bottom: 0px;
}
#spotlightCTA {
position: relative;
display: inline-block;
width: 300px;
text-align: right;
cursor: pointer;
right: 0px;
float: right;
}
And my HTML
<div id="spotlightContent">
<div id="spotlightTitle">
<div class="title">This is a test title.<br>Let's make it longer.
</div>
</div>
<div id="spotlightCTA" class="animate">
<span>This is a call to action.</span>
<button type="">Test button</button>
</div>
</div>
If I float right, it aligns to the top. If I remove the float, it aligns to the bottom but I can't get it to align to the right.
Thanks in advance!
Flexbox can do that.
div {
background-color: rgba(10, 10, 10, .2);
}
/*Containers*/
#spotlightContainer {
position: relative;
height: 200px;
width: 646px;
padding: 10px 20px 0px 20px;
background-color: rgba(0, 0, 0, .4);
}
#spotlightContent {
width: 646px;
height: auto;
padding: 30px 0px 0px 0px;
display: flex;
justify-content: space-between;
align-items: flex-end;
}
#spotlightTitle {
width: auto;
height: auto;
display: inline-block;
bottom: 0px;
}
/*CTA*/
#spotlightCTA {
position: relative;
display: inline-block;
width: 300px;
text-align: right;
cursor: pointer;
right: 0px;
float: right;
}
#spotlightCTA span {
font-family: "Open Sans", "Helvetica Neue", helvetica, arial, sans-serif;
font-weight: 300;
color: #fff;
display: inline-block;
padding: 5px 0px 0px 0px;
}
#spotlightCTA button {
background-color: #ffd200;
border-radius: 0px;
border: none;
margin: 0px 0px 0px 15px;
padding: 5px;
vertical-align: text-bottom;
}
.title {
font-family: "Oswald", "Helvetica Neue", helvetica, arial, sans-serif;
color: #fff;
font-weight: 600;
max-width: 400px;
font-size: 32px;
line-height: 1.2;
display: inline-block;
}
<div id="spotlightContainer">
<div id="spotlightContent">
<div id="spotlightTitle">
<div class="title">This is a test title.
<br>Let's make it longer.
</div>
</div>
<div id="spotlightCTA" class="animate">
<span>This is a call to action.</span>
<button type="">Test button</button>
</div>
</div>
</div>
Why is the "Input Status" div block got offset in its parents.
I did try toggle all its css attributes in the web inspector, it seems nothing fix the offset. Where did it come from?
Please have a look this demo: http://jsfiddle.net/pengyanb/Lfvnr9y1/
.containing_tab {
width: 100%;
height: 40pt;
margin-bottom: 20pt;
border: 1px red solid;
}
.status_tab {
display: inline-block;
width: 48%;
height: 100%;
margin-left: auto;
margin-right: auto;
border-radius: 10px;
box-shadow: 10px 10px 10px black;
font-family:'MS sans serif, Fallback, sans-serif';
text-shadow:0 0 0 !important;
text-align: center;
line-height: 40pt;
background-color:green;
border:1px blue solid;
}
.middle_padding {
display: inline-block;
width: 1%;
height: 100%;
margin-left: auto;
margin-right: auto;
border:1px blue solid;
}
<div class="containing_tab">
<div class="status_tab">Input 1 Status</div>
<div class="middle_padding"></div>
<div class="status_tab">Input 2 Status</div>
</div>
I toggled all its css attribute in the web inspector,
Because the vertical-align property for inline elements defaults to baseline. Just change that to top:
.status_tab {
display: inline-block;
width: 48%;
height: 100%;
margin-left: auto;
margin-right: auto;
border-radius: 10px;
box-shadow: 10px 10px 10px black;
font-family:'MS sans serif, Fallback, sans-serif';
text-shadow:0 0 0 !important;
text-align: center;
line-height: 40pt;
background-color:green;
border:1px blue solid;
vertical-align:top;
}
jsFiddle example
remove the height from the .containing_tab
.containing_tab {
width: 100%;
margin-bottom: 20pt;
border: 1px red solid;
}
.status_tab {
display: inline-block;
width: 48%;
height: 100%;
margin-left: auto;
margin-right: auto;
border-radius: 10px;
box-shadow: 10px 10px 10px black;
font-family:'MS sans serif, Fallback, sans-serif';
text-shadow:0 0 0 !important;
text-align: center;
line-height: 40pt;
background-color:green;
border:1px blue solid;
}
.middle_padding {
display: inline-block;
width: 1%;
height: 100%;
margin-left: auto;
margin-right: auto;
border:1px blue solid;
}
<div class="containing_tab">
<div class="status_tab">Input 1 Status</div>
<div class="middle_padding"></div>
<div class="status_tab">Input 2 Status</div>
</div>
I am trying to keep my ul and li elements that are inside of a div to respect the div's width with margin-left: auto. I am also not wanting ul/li to extend out of the div. I want the ul and li elements to stay inside the div container. From what I have read, this code is correct because I have the child elements contain a width which should be according to div's width.
#phonequeue {
display: block;
width: 400px;
height: 800px;
border-width: 5px;
border-color: white;
border-style: solid;
float: right;
margin-top: -650px;
}
li {
display: block;
padding: 15px;
text-align: left;
height: 1.2 em
padding-left: 15px;
padding-right: 15px;
margin: 0px 0px 4px 0px;
background-color #555
-webkit-border-radius: 15px;
border-radius: 40px;
background: -webkit-gradient(linear, 0% 0%, 0% 30%, from(#444), to(#111));
font-family: verdana, arial, helvetica, sans-serif;
font-size: 22px;
width: 100%;
margin: auto;
}
ul {
list-style-type: none;
width: 100%;
margin: auto;
}
index.html:
<div id="phonequeue">
<ul>
<li class="avail">
<span class="pname">John Doe</span>
<span class="ptime">03:00</span>
</li>
</ul>
</div>
UPDATE: Here is a working example. The element width is 430px; when the parent div is 400px;. I would the child elements to center themselves in div and have their width respecting the parents width. http://jsfiddle.net/FBvQJ/4/
I believe the problem was with the use of width: 100%
Check this jsFiddle and let me know if this answers your question.
This is the updated CSS to solve the overlapping width:
phonequeue {
display: block;
width: 200px;
border: 1pt solid green;
}
ul{
border: 1pt solid red;
list-style-type: none;
margin: auto;
padding: 0;
}
li {
display: block;
padding: 15px;
text-align: left;
height: 1.2em;
margin: 0px 0px 4px 0px;
-webkit-border-radius: 15px;
font-size: 22px;
margin: auto;
border: 1pt solid #ccc;
}
I don't think you need to define the full width for each element.
If you'd like the text centered in the DIV, remove the width on the LI tag and make the margin:
margin: 0px auto 4px auto;
That will center the text within the div.
If I'm understanding your question correctly (which is dubious) I believe you can achieve the centering you require by removing the 100% widths and using text-align:center:
li {
display: block;
padding: 15px;
text-align: left;
height: 1.2 em;
padding-left: 15px;
padding-right: 15px;
margin: 0px auto 4px auto;
background-color #555;
-webkit-border-radius: 15px;
border-radius: 40px;
background: -webkit-gradient(linear, 0% 0%, 0% 30%, from(#444), to(#111));
font-family: verdana, arial, helvetica, sans-serif;
font-size: 22px;
text-align:center; /* < this (or not, if you don't actually want it centred after all) */
}
ul {
list-style-type: none;
padding:0; /* < and this */
margin: 0;
}
Demo fiddle
And later, when you decide that you want the time on the right use
.ptime {float:right;}
example
I have some CSS that looks fine in Chrome and Firefox, but is turning up a specific problem in Safari. The three divs/ul items are aligning properly in Chrome and Firefox, but are stacking directly on top of each other in Safari. To make it look right in Sf, I can make the left margin progressively larger between the three, but the result in FF and Chrome is that the divs go way off the page. I have tried a few hacks including this one:
#media screen and (-webkit-min-device-pixel-ratio:0){}
This didn't change anything. So here's the relevant HTML:
<ul id="nav">
<li class="nav_item"><div id= "work"> </div></li>
<li class="nav_item"><div id="about"> </div></li>
<li class="nav_item"><div id="contact"> </div></li>
</ul>
...And the CSS:
#work{ height: 50px;
width: 68px;
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
background-color: none;
background-image: url(work_1.png);
background-repeat: no-repeat;
background-size: 120%;
position: fixed;
text-align: center;
z-index: 201;
padding: 0px 0px 0px 0px;
margin: 1px 1px 1px 40px;
display: block;
clear: both;
float:right;}
#about{ height: 50px;
width: 68px;
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
background-color: none;
background-image: url(about.png);
background-repeat: no-repeat;
background-size: 120%;
position: fixed;
text-align: center;
z-index: 201;
padding: 0px 0px 0px 0px;
margin: 1px 1px 1px 40px;
display: block;
clear: both;
float: right;}
#contact{ height: 50px;
width: 84px;
font-family: Helvetica, Arial, sans-serif;
font-size: 24px;
background-color: none;
background-image: url(contact.png);
background-repeat: no-repeat;
background-size: 100%;
position: fixed;
text-align: center;
z-index: 200;
padding: 0px 0px 0px 0px;
margin: 1px 1px 1px 40px;
display: block;
clear: both;
float:right;}
#nav {
padding: 0px;
display:inline;
list-style-type:none;
margin: 30px 0px 0px 700px;
position:fixed;
width:600px;
}
.nav_item {
display:inline !important;
position:relative !important;
margin: 5px 5px 5px 150px;
width:100px;
display: inline !important;
}