1, left and right layout
If you have the following HTML structure, set the left and right two column layouts
<div class="parent"> <div class="leftChild"></div> <div class="rightChild"></div></div>
Set float : The usual method for left and right layouts is to set a float for a child element and then use the Clearfix class on its parent element to clear the float. The sample code is as follows:
.clearfix::after{ content:""; display:block; clear:both;}.leftChild,.rightChild{ float:left;}
Set position absolute positioning to set the parent element position:relative;
as child element position:absolute
. The sample code is as follows:
.parent{ position:relative;}.leftChild{ position:absolute; left:0; top:0;}.rightChild{ position:absolute; left:200px; top:0;}
2. Left and right layout
Left and right layout of the main method is also floating or absolute positioning, but can be selected by the case of one use or even combined use.
3, the use of floating or absolute positioning in specific cases
- Use floating: Do not need to calculate a particularly precise position, but not easy to control, the width of the floating element needs to be noted, otherwise it will be a line display, applicable to the navigation bar and other places.
- Use absolute positioning: the exact location of the element to be calculated, but more accurate and easy to manipulate.
- When the position of some elements is adaptive to the size of the parent element and the content width cannot be determined, it is recommended to use a floating
- Floating is recommended when the position of a particular element is fixed relative to the parent element, or if the content is determined to be precise, or even when the location is to be transformed with a JS operation.
5. Other Tips
- Use pseudo-elements to clear the float, and the code is shown in the next article.
When you want to reach an element hover state has a border, normally without a border, if you add a border directly in the hover state, the element will cause a slight change in position because of the extra border width. Tip: You can add a transparent border to normal situations and change the color when you hover. Like what
HTML code:
<nav class="clearfix"> <li><a href="#">link1</a></li> <li><a href="#">link2</a></li> <li><a href="#">link3</a></li> <li><a href="#">link4</a></li> <li><a href="#">link5</a></li> <li><a href="#">link6</a></li></nav>
CSS Code
.clearfix::after{ content: ""; display: block; clear: both;}nav{ border: 1px solid red; float: right;}nav > li{ float: left; list-style: none;}nav > li > a{ text-decoration: none; color:inherit; display: inline-block; margin:5px; padding:10px; border: 3px solid transparent; transition:0.3s; }nav > li > a:hover{ border:3px solid blue;}
:
- Horizontal Line style settings
code example:
hr{ height:0; border:none; border-top:1px solid red;}
- DD, DT set left and right structure in DL
HTML code
<body><main> <dl class="clearfix"> <dt>num</dt> <dd>1</dd> <dt>num</dt> <dd>2</dd> <dt>num</dt> <dd>3</dd> <dt>num</dt> <dd>4</dd> </dl></main>
CSS Code
main > dl{ width:300px; border: 1px solid;}main > dl >dt,main > dl >dd{ float: left; width:30%; box-sizing:border-box; border:1px solid blue;}main > dl >dd{ width:70%;}
:
- Some icon footage can be found using the Iconfont Web site.
- Rational use of pseudo-elements (e.g.
::after
, ::before
)
- Block-level element width self-use, reasonable use of
max-width
attributes
- When a label is stripped of its default style, the color can be set to inherit the parent element
a{color:inherit;}
2018-05-22 CSS layout, left and right layout, and some tips