遮阳卷帘价格:Vertical Centering With CSS
来源:百度文库 编辑:九乡新闻网 时间:2024/04/25 15:59:20
Centering With CSS
There are a few different ways to vertically centre objectswith CSS, but it can be difficult to choose the right one. I’ll show youall the best ways I’ve seen and also how to create a nice littlecentered website.
Vertical centering with CSS isn’t an easy thing to do. There are manydifferent ways that may not work in some browsers. Let’s review 5different ways to vertically centering objects, as well as the pros andcons of each method. (You can see my test page briefly explaining all of them.)
Method 1
This method sets some This method will use an absolutely positioned div, which has the topset to 50% and the top margin set to negative half the height of thecontent. This means the object must have a fixed height, that is definedby CSS. Because it has a fixed height, you may want to set In this method, we will insert a div above the content element. This will be set to This method uses a This method will only centre a single line of text. Simply set the This method is very useful on small elements, such as to centre the text inside a button or single-line text field. My favourite method is number 3 — using a floater and clearing thecontent. It doesn’t have any major downsides. Because the content will Now you know how it works, let’s start creating a simple butinteresting website! The final product will look something like this: It’s always good to start with semantic markup. This is how our page will be structured: Here is the xhtml code I will be using: Holisticly re-engineer value-added outsourcing after process-centric collaboration and idea-sharing.Energistically simplify impactful niche markets via enabled imperatives.Holisticly predominate premium innovation after compelling scenarios.Seamlessly recaptiualize high standards in human capital with leading-edge manufactured products.Distinctively syndicate standards compliant schemas before robust vortals.Uniquely recaptiualize leveraged web-readiness vis-a-vis out-of-the-box information. Efficiently embrace customized web-readiness rather than customer directed processes.Assertively grow cross-platform imperatives vis-a-vis proactive technologies.Conveniently empower multidisciplinary meta-services without enterprise-wide interfaces.Conveniently streamline competitive strategic theme areas with focused e-markets.Phosfluorescently syndicate world-class communities vis-a-vis value-added markets.Appropriately reinvent holistic services before robust e-services. Copyright notice goes here Now we will start with some basic CSS to lay out the page. You should put this in Before we can make our content vertically centred, the The floater’s You should now have something that looks like this: The width of Because The last thing to do is add some more styles to make it look a bit nicer. Lets start with the menu. The first thing to do when turning a list into a menu kind of thing is to remove the dot points with The next thing to notice is that the links are set to display as ablock element. This makes them fill the entire line and gives you morecontrol over them. If you want to make your menu go horizontally(doesn’t work in this design), then you can make them float as well. The other interesting thing to notice about the menu is the The last thing to do is add some more CSS to make the page look a bit nicer. A thing to notice is the rounded corners on As you might have guessed, Internet Explorer is the only main browser which gives you trouble: There are many interesting things you can do with centered websites. I used this idea in my redesign of the SWFObject Generator 2.0 (to generate code to use SWFObject 2.0). Here is also another idea. I did not discover all this myself. Here are some of the articles Ihave read which describe some of these techniques: (If you areinterested, I recommend you read them)vertical-align
property (which works very differently for other elements).#wrapper {display:table;}#cell {display:table-cell; vertical-align:middle;}
The Goods
The Bads
Method 2
overflow:auto;
to the content div, so if there is too much content to fit in, ascrollbar will appear, instead of the content continuing on outside thediv!<div id="content">Content Here</div>
#content {position:absolute; top:50%; height:240px; margin-top:-120px; /* negative half of the height */}
The Goods
The Bads
div
is inside the body
and the user shrinks the browser window, a scrollbar will not appear)Method 3
height:50%;
and margin-bottom:-contentheight;
. The content will then clear the float and end up in the middle.#floater{float:left; height:50%; margin-bottom:-120px;}#content{clear:both; height:240px; position:relative;}
The Goods
The Bads
Method 4
position:absolute;
div with a fixed width and height. The div is then told to stretch to top:0; bottom:0;
. It can’t because of the fixed height, so margin:auto;
will make it sit in the middle. This is similar to using the very common margin:0 auto;
to horizontally centre block elements.#content {position:absolute; top:0; bottom:0; left:0; right:0;margin:auto; height:240px; width:70%;}
The Goods
The Bads
Method 5
line-height
to the height of the object, and the text sits in the middle#content {height:100px; line-height:100px;}
The Goods
The Bads
Which Method?
clear:both;
, you can also put other elements above it, and when the windows collapses, the centered content will not cover them up. See the demo.Title
#floater{float:left; height:50%; margin-bottom:-120px;}#top{float:right; width:100%; text-align:center;}#content{clear:both; height:240px; position:relative;}
Step 1
)Page Title
Heading 2
Step 2
styles.css
, which is linked to at the top of our html.html, body {margin:0; padding:0;height:100%;} body {background:url('page_bg.jpg') 50% 50% no-repeat #FC3;font-family:Georgia, Times, serifs;} #floater {position:relative; float:left;height:50%;margin-bottom:-200px;width:1px;} #centered {position:relative; clear:left;height:400px; width:80%; max-width:800px; min-width:400px;margin:0 auto;background:#fff;border:4px solid #666;} #bottom {position:absolute;bottom:0; right:0;} #nav {position:absolute; left:0; top:0; bottom:0; right:70%;padding:20px; margin:10px;} #content {position:absolute; left:30%; right:0; top:0; bottom:0;overflow:auto; height:340px;padding:20px; margin:10px;}
body
and html
must be stretched to 100% height. Because the height is inside thepadding and margin, we have to make them 0 so a scrollbar doesn’t appearjust to show you a little margin.margin-bottom
is half of the content’s height (400px), which is -200px.#centered
is 80%. This is to make your sitesmall on small screens and wide on bigger screens (on my medium-largescreen, many old websites are small in the top left corner, and it is alittle annoying). This is known as having a liquid layout. min-width
and max-width
are also set to stop it getting too big or too small. Internet Explorerdoen’t support min & max widtgh though. Obviously, you may chooseto have a fixed width instead.#centered
is position:relative
, we can use use absolute positioning inside it to position the elements. overflow:auto;
was used on #content
,so a scrollbar will appear when the content doesn’t fit inside it.Internet Explorer doesn’t like overflow:auto; unless we tell it theheight (not just top
and bottom
position, and not in %) so we did that as well.Step 3
#nav ul {list-style:none;padding:0; margin:20px 0 0 0; text-indent:0;} #nav li {padding:0; margin:3px;} #nav li a {display:block; background-color:#e8e8e8;padding:7px; margin:0;text-decoration:none; color:#000;border-bottom:1px solid #bbb;text-align:right;} #nav li a::after {content:'?'; color:#aaa; font-weight:bold;display:inline; float:right;margin:0 2px 0 5px;} #nav li a:hover, #nav li a:focus {background:#f8f8f8;border-bottom-color:#777;} #nav li a:hover::after {margin:0 0 0 7px; color:#f93;} #nav li a:active {padding:8px 7px 6px 7px;}
list-style:none
and all the margin and padding. If you want it to have a margin orpadding, make sure you specify exactly what, don’t leave it to the webbrowsers defaults because they can vary.:before
and :after
CSS pseudo-element let you insert content before and after elements.This is a good way to include little icons or characters such as thearrow at the end of each link. This doesn’t work in Internet Explorerbefore version 8 though.Step 4
#centered {-webkit-border-radius:8px; -moz-border-radius:8px; border-radius:8px;} h1, h2, h3, h4, h5, h6 {font-family:Helvetica, Arial, sans-serif;font-weight:normal; color:#666;} h1 {color:#f93; border-bottom:1px solid #ddd;letter-spacing:-0.05em; font-weight:bold;margin-top:0; padding-top:0;} #bottom {padding:10px;font-size:0.7em;color:#f03;} #logo {font-size:2em; text-align:center;color:#999;} #logo strong {font-weight:normal;} #logo span {display:block;font-size:4em; line-height:0.7em;color:#666;} p, h2, h3 {line-height:1.6em;} a {color:#f03;}
#centered
. In CSS3, there should be a border-radius
property to set the radius of rounded corners. This is not implemented by any major browsers yet, unless you use the -moz
or -webkit
prefixes (for Mozilla Firefox and Safari/Webkit)Compatibility Notes
#floater
must have a width defined, or it doesn’t do anything in any version of IEMore Ideas
Sources